ktos1234 ktos1234 - 2 months ago 20
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)
if (cursor.getCount() == 0)
textview.setText("No categories.");

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


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.

listCategories.setOnItemClickListener(new OnItemClickListener(){
    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?