Displaying the database in a list in Android
I want to display the database in a list in Android. I am using cursors for that. I am getting the co开发者_Python百科lumncount and using for
loop I am moving the cursor. But the database is not displayed. Please help. Here is my code:
List<String> s;
Cursor c = db.query(Geo_Create_Table, new String[] { lat + "", lon + "", result1 }, null, null, null, null, null);
c.moveToFirst();
int count = c.getColumnCount();
for (int i = 0; i < count; i++)
{
c.moveToNext();
}
Please let me know if there is any other simpler approach.
You seem to have lost some of your code there. You should edit your post, I don't have the privilages. The column count will give you the number of columns (fields) in each row and shouldn't be used to loop through the cursor's records if that is what you're trying to do, you should probably use the getCount()
method instead.
List<String> s;
Cursor c=db.query(Geo_Create_Table, new String[]{lat+"",lon+"",result1}, null,null, null, null, null);
c.moveToFirst();
int count=c.getCount();
for(int i=0; i < count; i++)
{
....
c.moveToNext();
}
Alternatively, in my code below you query moveToFirst(), if this returns true it means there are records to be traversed. The I use a do/while loop to move through each record (note: c.moveToNext()).
List s;
Cursor c=db.query(Geo_Create_Table, new String[]{lat+"",lon+"",result1}, null,null, null, null, null);
if (c.moveToFirst()) {
do
{
...
c.moveToNext();
}
while (!c.isAfterLast());
}
精彩评论