简体   繁体   中英

Adding 2 Arraylists to 1 Recyclerview

private List<SQLiteHelper> messages = new ArrayList<>();

    mAdapter = new MessagesAdapter(messages);
    messageList.setAdapter(mAdapter);
    mAdapter.notifyDataSetChanged();

onStart

 while (csr.moveToNext()) {
                    String mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
                    String mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
                    long mTime = csr.getLong(csr.getColumnIndex(KEY_TIME));
                    String mSeen = csr.getString(csr.getColumnIndex(KEY_SEEN));
                    String mTimer = csr.getString(csr.getColumnIndex(KEY_TIMER));
                    String mType = csr.getString(csr.getColumnIndex(KEY_TYPE));

                    messages.add(new SQLiteHelper(mSender, mMessage, mType, mSeen, mTimer, mTime));
                }

onCreate

    FirebaseRecyclerOptions<SQLiteHelper> options =
            new FirebaseRecyclerOptions.Builder<SQLiteHelper>()
                    .setQuery(mFetchingMessages, SQLiteHelper.class)
                    .build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<SQLiteHelper, Chat.MessagesViewHolder>(options) {

            final DatabaseReference mTimeReference = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);
            Query messageQuery = mTimeReference.limitToLast(10);

            messageQuery.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        SQLiteHelper message = dataSnapshot.getValue(SQLiteHelper.class);



                        messages.add(message);
                        messageList.scrollToPosition(messagesList.size() - 1);
                    }
                }

I have just included the Main parts. Both Arraylists use the same Helper Class. One is a firebaserecycleradapter fetching data from firebase and the other is fetching data fSQLitelite. Only the firebase data is shown and not the SQLite. Can someone help me with this so that the SQLite data is shown in the top and firebase below that? Where have I gone wrong

The table and the data exists

Implement this method in your adapter to add element at the beginning of the data list:

This is data list inside your adapter:

private List<SQLiteHelper> data = new ArrayList<>();

Use this to add List of new items to adapter dataList:

public void addList(List<SQLiteHelper> newMessages) {

    data.addAll(0, newMessages);

    notifyDataSetChanged();
}

Use this to add one new element to adapter data list:

public void addElement(SQLiteHelper newMessage) {

    data.add(0, newMessage);

    notifyDataSetChanged(); 
}

Both of this methods adds new elements to the top of adapter list.

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