Domanda

I'm displaying data from SQLite using

ListAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_2,
            employees,
            new String[] {"FirstName","LastName"},
            new int[] {android.R.id.text1,android.R.id.text2},0);
    getListView().setAdapter(adapter);

empoyees - my Cursor.

Data is showing correctly, but how to get what row I used from SQLite duaring OnClick on my item from List? For example I have 2 tables

1)Category
_id Integer
Name Text

2)Articles
_id Integer
Name text
CategoryId (foreing key)

So on first screen I'm displaying all Categorys, then on list item click I want to display Articles that are from specified category, how to do that?

È stato utile?

Soluzione

Don't use your cursor in your adapter. Build up an array and send in the array instead. In that array, typically something like List<MyDataRecordHolder> (where MyDataRecordHolder is an object you've created and filled with data from the database), you will have to set a reference to a row id.

UPDATE:

Create your MyDataRecordHolder: (Pseudo code)

List<MyDataRecordHolder> list = new List<MyDataRecordHolder>();

in your loop where you fetch data:

MyDataRecordHolder record = new MyDataRecordHolder();
record.setId(cursor.get("rowid"))
//set any other pertinent data
list.add(record);

For your adapter:

ListAdapter adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_2,
        list,
        new String[] {"FirstName","LastName"},
        new int[] {android.R.id.text1,android.R.id.text2},0);

Note, you are now supplying the List (list) to the adapter.

In your OnClick for your row, in the adapter, you will get the current MyDataRecordHolder:

final MyRecordHolder record = getItem(position);
String id = record.getId();
//make new db query based on your id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top