简体   繁体   English

如何在ListView中使用onItemClickListener获取其他列

[英]How to get other column with onItemClickListener in ListView

I am working on a Shopping list where you can click on the item in the ListView and a dialog comes up.我正在处理购物清单,您可以在其中单击 ListView 中的项目,然后出现一个对话框。 There you can modify the product name.您可以在那里修改产品名称。 It works fine, but now i am trying to add details.它工作正常,但现在我正在尝试添加细节。 Adding to database already works.添加到数据库已经有效。 It is in the third column but now I cant get it out with the onItemClickListener.它在第三列中,但现在我无法使用 onItemClickListener 将其取出。 Get Data from the second column works fine, so the the product is working fine with ItemOnClick.从第二列获取数据工作正常,因此该产品与 ItemOnClick 一起工作正常。

I already tried a couple of codes but didnt find a solution.我已经尝试了几个代码,但没有找到解决方案。

OnItemClick: 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();
            }
        }
    });

DatabaseHelper:数据库助手:

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);

    }
}

Try :-尝试 :-

            int itemID = -1;
            String item3 = ""; //<<<<<<<<<< ADDED
            while (data.moveToNext()){
                itemID = data.getInt(0);
                item3 = data.getString(data.getColumnIndex(DatabaseHelper.COL3)); //<<<<<<<<<< ADDED
            }
  • Note using getColumnIndex and the column name is more flexible and less prone to mis-calculation of offsets.请注意,使用 getColumnIndex 和列名更灵活,更不容易错误计算偏移量。 You may wish to consider changing itemID = data.getInt(0);您可能希望考虑更改itemID = data.getInt(0); to itemID = data.getInt(data.getColumnIndex(DatabaseHelper.COL1));itemID = data.getInt(data.getColumnIndex(DatabaseHelper.COL1));

Your getItemId method will, as it stands get all rows not just the row that has the name.您的getItemId方法将获取所有行,而不仅仅是具有名称的行。 You may wish to change from :-您可能希望从 :-

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;
}

to :-到 :-

public Cursor getItemID(String name){
    SQLiteDatabase db = this.getWritableDatabase();
    return db.query(TABLE_NAME,null,COL2+"=?",new String[]{name},null,null,null);
}
  • Note the above is in-principle code.请注意,以上是原则上的代码。 It has not been tested or run and may therefore have some errors.它尚未经过测试或运行,因此可能存在一些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM