How do I get the cursor value of a ListView's item onItemClick?
Solution 1
Instead of
int cat_id = cur.getInt(cur.getColumnIndex("_id"));
String cattitle = cur.getString(cur.getColumnIndex("title"));
int has_sub = cur.getInt(cur.getColumnIndex("has_sub"));
results.add(cat_id + cattitle + has_sub);
create a Category class to contain these values, and use a parameterized ArrayList. Something like
class Category {
public int cat_id;
public String cattitle;
public int has_sub;
public Category(int cat_id, ...) {
// constructor logic here
}
}
and
results.add(new Category(cat_id, cattitle, has_sub));
With this, you can set the onItemClickListener as such:
catlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
Category clickedCategory = results.get(position);
int id = clickedCategory.cat_id;
// do something with id
}
});
Your ArrayList is the data source of your ArrayAdapter, and position
corresponds to the index of the clicked Category in it.
Solution 2
Use CursorAdapter(http://developer.android.com/reference/android/widget/CursorAdapter.html) instead, when you are using ArrayAdapter it's exceed memory allocation, and also you wouldn't get notifications if db changed.
Moreover in case of using CursorAdapter you will have
public void onItemClick(AdapterView<?> parent, View v, final int position, long id)
the "id" param will concur with table's _id field
to get the whole row just do
adapter.getItem(int position)
Related videos on Youtube
Cloud
Updated on June 04, 2022Comments
-
Cloud almost 2 years
I've created a database with tables (categories) with columns (_id, title etc). I want to read these categories' data from my database and list them in a
ListView
.Here is my code:
public class MainActivity extends listActivity{ private ArrayAdapter arrayAdapter; ArrayList results = new ArrayList(); ListView catlist; Cursor cur; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); catlist = getListView(); int parentid = getIntent().getIntExtra("catid", 0); openAndQueryDatabase(parentid); displayCatListView(); } private void displayCatListView() { setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); catlist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, final int position, long id) { Toast.makeText(MainActivity.this, "List View Clicked:" + position, Toast.LENGTH_LONG) .show(); } }); } private void openAndQueryDatabase(int parentid) { DataBaseHelper db = new DataBaseHelper(this); SQLiteDatabase dbr = db.getReadableDatabase(); cur = dbr.rawQuery( "SELECT _id, title, has_sub FROM categories where parent_id=?", new String[] { String.valueOf(parentid) }); if (cur != null) { while (cur.moveToNext()) { int cat_id = cur.getInt(cur.getColumnIndex("_id")); String cattitle = cur.getString(cur.getColumnIndex("title")); int has_sub = cur.getInt(cur.getColumnIndex("has_sub")); results.add(cat_id + cattitle + has_sub); } cur.close(); } db.close(); } }
- I can get the item's position onItemClick, but I want to get their row's _id onItemClick. How do I do this?
- I'm new to this. Are there any mistakes in my code?
Thanks a lot.
-
Admin almost 11 yearsthank you for your answer. could you explain this more this part please:
class Category { public int cat_id; public String cattitle; public int has_sub; public Category(int cat_id, ...) { // constructor logic here } }
or link a complete example? -
numediaweb over 10 years@ErikR Could you please point to a full tutorial on using object instead of arraylist?
-
ErikR over 10 years@numediaweb My code above uses the original ArrayList in Ji.Mack's code to hold instances of the Category class. So we're using objects with an ArrayList, not instead of.