簡體   English   中英

Android RecyclerView:如何將新項目添加到列表頂部?

[英]Android RecyclerView: how to add new item to top of list?

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

而不是在以下代碼段中添加要開始的元素(請參閱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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM