如何在 ListView 中使用 onItemClickListener 获取其他列
Posted
技术标签:
【中文标题】如何在 ListView 中使用 onItemClickListener 获取其他列【英文标题】:How to get other column with onItemClickListener in ListView 【发布时间】:2019-11-14 18:18:13 【问题描述】:我正在处理一个购物清单,您可以在其中单击 ListView 中的项目并出现一个对话框。在那里您可以修改产品名称。它工作正常,但现在我正在尝试添加细节。 添加到数据库已经有效。它在第三列,但现在我无法使用 onItemClickListener 将其取出。 从第二列获取数据工作正常,因此产品与 ItemOnClick 一起工作正常。
我已经尝试了几个代码,但没有找到解决方案。
OnItemClick:
lvProducts.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
try
//DB*********************************
String name = parent.getItemAtPosition(position).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = myDB.getItemID(name);
int itemID = -1;
while (data.moveToNext())
itemID = data.getInt(0);
if (itemID > -1)
Log.d(TAG, "onItemClick: The ID is " + itemID);
Intent editScreenIntent = new Intent();
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
selectedID = editScreenIntent.getIntExtra("id",-1);
selectedName = editScreenIntent.getStringExtra("name");
showInputBox(arrayList.get(position), position);
catch (Exception e)
Toast.makeText(ShoppingActivity.this, "Error#666", Toast.LENGTH_SHORT).show();
);
数据库助手:
public class DatabaseHelper extends SQLiteOpenHelper
public static final String TABLE_NAME = "myList_data";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public static final String COL3 = "ITEM2";
public DatabaseHelper(Context context)
super(context, TABLE_NAME, null, 1);
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT, " + COL3 + " TEXT )";
db.execSQL(createTable);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
public boolean addData(String item, String detail)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,item);
contentValues.put(COL3, detail);
long result = db.insert(TABLE_NAME,null,contentValues);
if (result == -1)
return false;
else
return true;
public Cursor getData()
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
public Cursor getItemID(String name)
SQLiteDatabase db = this.getWritableDatabase();
//String query = "SELECT "+ COL1 + " FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
//Cursor data = db.rawQuery(query, null);
return data;
/**
* Updates the name field
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName,int id,String oldName)
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 + " = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" + " AND " + COL2 + " = '" + oldName + "'";
db.execSQL(query);
public void deleteName(int id, String name)
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE " + COL1 + " = '" + id + "'" + " AND " + COL2 + " = '" + name + "'";
db.execSQL(query);
public void deleteAll()
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME,null,null);
String query =("DELETE FROM " + TABLE_NAME);
db.execSQL(query);
【问题讨论】:
【参考方案1】:试试:-
int itemID = -1;
String item3 = ""; //<<<<<<<<<< ADDED
while (data.moveToNext())
itemID = data.getInt(0);
item3 = data.getString(data.getColumnIndex(DatabaseHelper.COL3)); //<<<<<<<<<< ADDED
注意使用 getColumnIndex 和列名更灵活,更不容易误算偏移量。您不妨考虑将itemID = data.getInt(0);
更改为itemID = data.getInt(data.getColumnIndex(DatabaseHelper.COL1));
您的 getItemId 方法将获取所有行,而不仅仅是具有名称的行。您可能希望更改为:-
public Cursor getItemID(String name)
SQLiteDatabase db = this.getWritableDatabase();
//String query = "SELECT "+ COL1 + " FROM " + TABLE_NAME + " WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
//Cursor data = db.rawQuery(query, null);
return data;
到:-
public Cursor getItemID(String name)
SQLiteDatabase db = this.getWritableDatabase();
return db.query(TABLE_NAME,null,COL2+"=?",new String[]name,null,null,null);
请注意,以上是原则代码。它尚未经过测试或运行,因此可能存在一些错误。
【讨论】:
非常感谢.. 有时解决方案很简单,但我对 SQLite 没有太多经验,所以这对我来说是全新的!以上是关于如何在 ListView 中使用 onItemClickListener 获取其他列的主要内容,如果未能解决你的问题,请参考以下文章
WPF:如何在拖动 ListView 项目时允许在 ListView 中使用鼠标滚轮滚动?
如何在 ListView Widget 中使用 markdown 包(对于带有项目符号的文本)?在 ListView 中使用时没有显示
如何使用json解析android在listview中获取图像