简体   繁体   中英

Data fetched from sqlite not showing in recyclerview

    messageList = (RecyclerView) findViewById(R.id.message_list);
    mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    linearLayoutManager = new LinearLayoutManager(getApplicationContext());

    messageList.setLayoutManager(linearLayoutManager);

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

final MainData mHelper = new MainData(this);
    final Cursor csr = mHelper.getAllQuestions3();
            sqlite = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId);
            valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {

//I add the data from firebase to SQLITE here 


                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));
                    mAdapter.notifyDataSetChanged();
                }
                    mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId).setValue(null);

        }
            @Override
            public void onCancelled(DatabaseError databaseError) {
        }
    };
            sqlite.addListenerForSingleValueEvent(valueEventListener);

Adapter

public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.MessageViewHolder>{
ChatData mHelper;
Cursor csr;
private List<SQLiteHelper> mMessagesHelperList;
private FirebaseAuth mAuth;


public MessagesAdapter(List<SQLiteHelper> MessagesHelperList) {
    this.mMessagesHelperList = MessagesHelperList;
}


public class MessageViewHolder extends RecyclerView.ViewHolder{
    public TextView messageText;


    public MessageViewHolder(View view) {
        super(view);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());


        mHelper = new ChatData(view.getContext(),"MessagePlus",null,1);
        csr = mHelper.getAllQuestions3();

        messageText = (TextView)view.findViewById(R.id.message_text_layout);
    }
}

@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_chat,parent,false);
    mAuth = FirebaseAuth.getInstance();
    return new MessageViewHolder(V);
}

@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
    SQLiteHelper messagesHelper = mMessagesHelperList.get(position);

       holder.messageText.setText(messagesHelper.getMessage());
    }

@Override
public  int getItemCount() {
    return mMessagesHelperList.size();
}

I have another activity where i fetch sqlite data and show it in recyclerview and it works and i had achieved that the same way as this. Here too it used to work but then i added some features and moved around some code and now its not working and i have checked the whole code numerous times but still not finding why its not working...

The table and rows exists with data and the size of list too isnt null

Just discovered that the Adapter isnt getting called when i added Logs inside it. Thats where the problem is but idk why

Your problem is that you are adding the extra messages to the list that you first created your adapter so

instead of

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

do

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

and in your adapter you have to add the add function like that

public void add(SQLiteHelper item){
    if(mMessagesHelperList!=null){
        mMessagesHelperList.add(item);
        notifyDataSetChanged();
    }
}

also

you have too much "trash" on your code, on your ValueEventListener you are trying to read the values of a cursor and not the ds (DataSnapshot). so probably your cursor there is empty. If the cursor is empty you don't add any item to the adapter, so the adapter don't have items to show, so onBindViewHolder correctly don't get called

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