ktos1234 ktos1234 - 12 days ago 5
Android Question

Extract data from ListView

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.

Answer
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?