lucignolo
lucignolo

Reputation: 221

get position from listview

I retrieve data from sqlite database and I display it into listview. Here the resulting listview:

**Name**
Adam
Frank
James
Mike
.......

With this code clicking on a name I get his position in listview.  

public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id).

Result:

Adam = position 0

Frank = position 1

James = position 2

Mike = position 3

Now, if I retrieve data with WHERE clause

(e.g. SELECT * from my table WHERE name like "James")

The resulting listview is:

**Name**
James

and clicking on James I get position 0.

How can I get, instead, position 2?

Thank you.

Upvotes: 0

Views: 150

Answers (2)

noman404
noman404

Reputation: 940

you can again reQuery and get the row id which should 2. or while on click the list item(clickListener) you can move your cursor to that position cursor.moveToPosition(position) then you can also get the long row value by extracting the cursor.

Upvotes: 2

Jay Shah
Jay Shah

Reputation: 742

You will get position 0 because you have only one element in your resulting array.You need to take one hash map in which along with name bind its id and get value from name.

Example:HashMap names=new HashMap

When fetch results from database your hashmap will contain key value par like

<"James","2">.

Now extract value 2 by key James.

Upvotes: 0

Related Questions