Sunny
Sunny

Reputation: 14838

SQLite getting data

I have a spinner widget and I want to populate some data when user clicks on one of the items from spinner. So I have the position number like 0,1,2,3... for spinner items.

Now I have a table which have three fields as

  _id    cid    relatedname

   1       1          A
   2       1          B
   3       2          C
   4       2          D
   5       2          E

I want when a user clicks position 1 from spinner then it will check for cid and all the relatedname will be displayed whose cid is 1.

How to do this ?

Upvotes: 0

Views: 110

Answers (2)

Jayabal
Jayabal

Reputation: 3619

setOnItemChangelistener() to spinner then do as follows in tat listener.

loadRelatedName(position + 1);    

void loadRelatedName(int cid) 
{
     sql = " SELECT * FROM table WHERE cid = " + cid;
     Cursor c = db.select(sql);
     if (c ! null)
        while(c.moveToNext()) {
            Syso("Related Name : " + c.getString[2]);
     }
}

Upvotes: 2

hage
hage

Reputation: 6163

Add a OnItemSelectedListener to the spinner to get the selected item.

See this

There you can get the position. With that you can query your database. Probably something like:

SELECT relatedname from mytable where cid=<selectedpos>

This will get you all entries where cid has the same value as the selected position. Note: It may be that the indexes of the spinner are zero based and cid is not. So maybe you need to add 1 to the selected position before querying the table

Upvotes: 0

Related Questions