简体   繁体   中英

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

I have a SQLite database that pushes data to a CardView for a RecyclerView list. I want newly added CardViews to insert at the top of the list but they are being added at the bottom, the end of the list.

It seems like this code in the MainActivity is not being respected that should put the new CardView at the top of the list:

contactList.add(0, contact);

What am I missing here?

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

Instead of adding element to start in following snippet(refer comment at contactList.add(contact); line)

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

just change your addAll() method as follows

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

Also in clearAll method just write youList.clear()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM