Skip to content Skip to sidebar Skip to footer

How Can I Get The Row Id Of The A Table In The Sqlite Database When An Item Is Clicked In The Listview

My code goes here.............! when there is a row deleted from the list-view. The indexes of the rows of the listview is changed but the primary key id in the database is not cha

Solution 1:

First of all you have to write a query and retrieve id in your database.You must return the id in a cursor.

Refer the below code snippet:

publicCursorfetchAllNames() {

        Cursor mCursor = app.myDbHelper.MyDB().query("cardlist", newString[] {"cardid as _id","cardname","carddesc"},
                null, null, null, null, null);
        try{

            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }catch(Exception e)
        {
            return mCursor;
        }

    }

Then in your java page:

Cursorcursor= myDB.fetchAllNames();

Then you can get the database id by:

 lv.setOnItemClickListener(newOnItemClickListener() {

        @OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
        {
      if(cursor!=null){
                    System.out.println("position===>"+position);
                    if(cursor.moveToFirst()){
                        cursor.moveToPosition(position);   
                        Stringcardid=    cursor.getString(cursor.getColumnIndex("_id"));

                    }
                }
        }
    });

you will get the database id in the string cardid. You must use _id if you are using SimpleCursorAdapter else you must replace _id with that of the column name that represents your id in the sqlite database.

Solution 2:

There's an easy way to achieve so : First, create an object to represent one line in your database. Since I don't know what your database contains, let's just call it "Title".

publicclassTitle {
    privatelong id;
    private String title;
    // Constructor, setters and getters
}

Then, create a custom adapter. It will store a collection of Title which will allow you to get it when an item is clicked.

publicclassTitlesAdapterextendsArrayAdapter<Title> {
    private Context context;
    private List<Title> items;

    publicTitlesAdapter(Context context, List<Title> items) {
        super(context, R.layout.row_draweritem, items);

        this.context = context;
        this.items = items;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) { /* ... */ }
}

You can then set your adapter in your activity easily:

this.titlesAdapter = new TitlesAdapter(context, titles);
listView.setAdapter(this.titlesAdapter);

... and define your onItemClickListener so you can get back the row id in your database

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    publicvoidonItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        long id = titlesAdapter.getItems().get(i).getId();
    }
});

You can learn more about custom adapters in this article : http://www.vogella.com/tutorials/AndroidListView/article.html

Post a Comment for "How Can I Get The Row Id Of The A Table In The Sqlite Database When An Item Is Clicked In The Listview"