【发布时间】:2017-01-11 04:23:21
【问题描述】:
我有一个 SQLite database 将数据推送到 CardView 以获得 RecyclerView 列表。我希望新添加的CardViews 插入到列表顶部,但它们被添加到底部,也就是列表的末尾。
MainActivity 中的这段代码似乎没有受到尊重,应该将新的 CardView 放在列表顶部:
contactList.add(0, contact);
我在这里错过了什么?
MainActivity.java
@Override
protected void onStart() {
super.onStart();
loadData();
}
void loadData(){
sqLiteDB = new SQLiteDB(this);
List<Contact> contactList = new ArrayList<>();
Cursor cursor = sqLiteDB.retrieve();
Contact contact;
// iterate over the db cursor by using a new cursor for the ArrayList.
try {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) { // to avoid an infinite loop iteration.
do {
contact = new Contact();
contact.setId(cursor.getInt(0));
contact.setTodo(cursor.getString(1));
**contactList.add(0, contact);** // add the new item to top of R. list.
} while (cursor.moveToNext());
}
}
} finally {
if(cursor !=null && !cursor.isClosed()){
cursor.close();
}
}
contactListAdapter.clear();
contactListAdapter.addAll(contactList);
SQLiteDB.java
public Cursor retrieve(){
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
ContactField.COLUMN_ID,
ContactField.COLUMN_TODO
};
Cursor cursor = db.query(
ContactField.TABLE_NAME,projection,
null,
null,
null,
null,
null
);
if (cursor == null) {
return null;
}
return cursor;
}
ContactListAdapter.java
public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder>{
private List<Contact> contactList;
private Context context;
private RecyclerItemClickListener recyclerItemClickListener;
// Setting to -1 keeps the first CardView (position 0) from having its
// BackGroundColor mistakenly switch from the default to the highlighted/selected
// color which is red.
private int selectedPos = -1;
public ContactListAdapter(Context context) {
this.context = context;
this.contactList = new ArrayList<>();
}
public void clear() {
while (getItemCount() > 0) {
remove(getItem(0));
}
}
public void addAll(List<Contact> contactList) {
for (Contact contact : contactList) {
// add(contact);
contactList.add(0, contact); }
}
// Get the Item's position.
public Contact getItem(int position) {
return contactList.get(position);
}
// Get the Item's Id.
public long getItemId(int position) {
return contactList.get(position).getId();
}
...
【问题讨论】:
-
您应该删除内部的 do/while 并离开
cursor.moveToNext()。还要检查在您的addAll方法中,联系人的顺序是否正确。另外记得修改适配器列表后调用contactListAdapter.notifyDataSetChanged()。 -
好的,我会尝试删除内部的do/while。如何在 addAll 方法中检查联系人的顺序是否正确?
-
您可以设置断点并进行调试,或者您可以打印每个联系人以查看它们是否按顺序加载。
-
是的,好吧...工作时间过长...需要撤回并重新关注全局!
-
@AJW 如果您在应用程序的完整生命周期中调用一次
addAll()方法,我认为您的代码没有任何问题。是这样吗?还是在数据库发生更改时重复调用它?
标签: android android-recyclerview android-cursor