Search code examples
javasqlitefirebase-realtime-databaseonitemclicklisteneronitemclick

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. 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. Get Data from the second column works fine, so the the product is working fine with ItemOnClick.

I already tried a couple of codes but didnt find a solution.

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

    }
}

Solution

  • 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. You may wish to consider changing itemID = data.getInt(0); to 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. 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.