Android RecyclerView:如何将新项目添加到列表顶部?

时间:2016-09-03 02:47:42

标签: android android-recyclerview android-cursor

我有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();
}
...

1 个答案:

答案 0 :(得分:0)

而不是在以下代码段中添加元素(请参阅contactList.add(contact);行的评论)

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(contact); // just add the new item to list normally.
                } while (cursor.moveToNext());
            }
        }
    } finally {
        if(cursor !=null && !cursor.isClosed()){
            cursor.close();
        }
    }

只需按以下方式更改addAll()方法

public void addAll(List<Contact> contactList) {
    for (Contact contact : contactList) {
        //add(contact);
        yourList.add(0, contact);
    }
}

同样在clearAll方法中,只需撰写youList.clear()

即可