[英]I am getting only last row while loading data from sqlite to arraylist. How do I get all records?
I am trying to populate the sqlite data to recyclerview by adding the sqlite data to arraylist. 我正在尝试通过将sqlite数据添加到arraylist来填充sqlite数据到recyclerview。 I am not getting all records but only last row of the database table. 我没有得到所有记录,而只有数据库表的最后一行。
cursor = sqLiteHelper.getData("SELECT * FROM news");
while (cursor.moveToNext()) {
String image = cursor.getString(cursor.getColumnIndex("image"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String category = cursor.getString(cursor.getColumnIndex("category"));
mExampleList.add(new ExampleItem(image, title, category));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
Did you try this? 你有尝试过吗?
// Move first to be sure
cursor.moveToFirst()
while(!cursor.isAfterLast()) { // Dont jump to the next before first iteration
String image = cursor.getString(cursor.getColumnIndex("image"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String category = cursor.getString(cursor.getColumnIndex("category"));
mExampleList.add(new ExampleItem(image, title, category));
// And then
cursor.moveToNext()
}
cursor.close()
How many records are in your storage? 您的存储中有多少条记录? Because if there are 2 the first call to cursor.moveToNext()
of the while might jump to the last. 因为如果有2,则while的第一次调用cursor.moveToNext()
可能会跳到最后一个。
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex(countyname));
list.add(name);
cursor.moveToNext();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.