Reputation: 207
I populated my ListView with data from my database table.
How can I get data from selected item?
This is how I populate ListView:
SimpleCursorAdapter SimpleCursorAdapter;
ListView listCategories = (ListView) findViewById(R.id.categoryList);
categoryRepo categoryRepo = new categoryRepo(this);
TextView textview= (TextView) findViewById(R.id.textView);
private void displayCategories()
{
Cursor cursor = categoryRepo.getCategories();
if (cursor == null)
{
textview.setText("Error");
return;
}
if (cursor.getCount() == 0)
{
textview.setText("No categories.");
return;
}
String[] from = new String[] {category.KEY_CATEGORY_NAME};
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cursor,from,to,0);
listCategories.setAdapter(SimpleCursorAdapter);
}
I know that I can do this by using listCategories.setOnItemClickListener , but I do not know how. Thank you for help.
I want to get CATEGORY_NAME value.
Upvotes: 0
Views: 687
Reputation: 1367
Try this :
listCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
if (cursor.moveToFirst()){
String selectedValue = cursor.getString(cursor.getColumnIndex(category.KEY_CATEGORY_NAME));
// do what ever you want here
}
cursor.close();
}
});
Upvotes: 1
Reputation: 4667
listCategories.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) SimpleCursorAdapter.getItem(position);
}
});
How to get string from selected item of SimpleCursorAdapter?
Upvotes: 1