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);
适配器
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();
}
我还有另一个活动,我获取sqlite数据并将其显示在recyclerview中,并且可以正常工作,并且我已经达到了相同的方式。在这里它也可以正常工作,但是后来我添加了一些功能并移动了一些代码,现在它不起作用了,我已经多次检查了整个代码,但是仍然找不到为什么它不起作用...
表和行与数据一起存在,并且列表的大小也不为空
当我在其中添加Logs时,才发现该适配器没有被调用。那就是问题所在,但idk为什么如此
答案 0 :(得分:0)
您的问题是,您要将多余的消息添加到首次创建适配器的列表中,因此
代替
messages.add(new SQLiteHelper(mSender, mMessage, mType, mSeen, mTimer, mTime));
mAdapter.notifyDataSetChanged();
做
mAdapter.add(new SQLiteHelper(mSender, mMessage, mType, mSeen, mTimer, mTime));
在适配器中,您必须添加类似的添加功能
public void add(SQLiteHelper item){
if(mMessagesHelperList!=null){
mMessagesHelperList.add(item);
notifyDataSetChanged();
}
}
也
您的代码上有太多“垃圾”,在ValueEventListener上,您试图读取游标而不是ds(DataSnapshot)的值。所以可能您的光标是空的。如果光标为空,则不会在适配器中添加任何项目,因此适配器中没有要显示的项目,因此不会正确调用onBindViewHolder