Move the cursor to a row in one of the columns

After querying the data that I wanted, now I have a cursor containing all the data presented to the user in Listview.

When a user clicks on an item to edit it, I move the cursor to the desired position cursor.moveToPosition(pos) , from which I can get all the necessary data for the item: identifier, title, folder or not, parent folder.

Now that I have the parent folder identifier, how can I use it to get the title of the folder with the items so that I can show the user in which folder the given item is in? I cannot use the move to position, because I do not know the parent position, but only its identifier.

Here is an example db.

FOLDER column β†’ 0 = not folder (false), 1 = folder (true)

PARENT β†’ contains its folder identifier

 ID TITLE FOLDER PARENT 1 folder1 1 0 2 item1 0 1 3 item2 0 1 4 folder2 1 1 5 item1 0 4 6 item2 0 4 7 folder3 1 4 8 item1 0 7 9 item2 0 7 

Example: A user edits item3. I move the cursor to position 2 (starting from 0). I get the parent element3, which is equal to ID = 1. How can I get TITLE ID = 1.

Hope this is clear enough :) Thanks!

+4
source share
3 answers

You can either scroll the cursor again, checking each identifier to see if it is equal to 1, and then get the title.

 cur.moveToFirst(); while (cur.isAfterLast() == false) { if (cur.getInt(cur.columnIndex("ID") == 1) { String title = cur.getString(cur.columnIndex("TITLE")); } cur.moveToNext(); } 

Or you can issue a new request, where id = 1.

+4
source

You can use:

 yourCursor.getString(yourCursor.columnIndex("TITLE")); 

This will give you the value in the "TITLE" column of the row pointed to by the current cursor.

0
source

In your query, you can enable JOIN to load the parent header as another column in the ResultSet and then get it without having to make another query or repeat Rs again.

0
source

Source: https://habr.com/ru/post/1379816/


All Articles