Is there a way to get a row by its index/position in table, rather than by its id?
For example I have 3 rows with IDs:
Record1
Record2
Record3
Now if I delete the second row using its id, the remaining rows will be like this:
Record1
Record3
Because I'm using ListView
position to determine which row in the database to delete, the next time I try to delete second row, I will get exception because row with id 2 doesn't exist.
So is there a method that can retrieve row by its index in table?
The better route to go will be to fetch the id of the record from the object represented by ListView
item and then use that to get the correct record in the database. In your ListView
's OnItemClickListener
, the onItemClick
event takes the AdapterView
as the first argument and the index of the selected item as the second. Get that item from the adapter and cast it to the type it represents.
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
YourClass c = (YourClass)arg0.getItemAtPosition(arg2);
//index of the record to delete can now be accessed at c.id
}
However, if you really want to get the nth record, I believe you can do the following:
SELECT * FROM TableName LIMIT 1 OFFSET n;
Where n is the index you're after. This also assumes that your results are ordered in the same fashion as they are in your ListView
.