检索SQLite数据时notifyDataSetChanged不起作用

时间:2018-12-14 05:37:06

标签: java android sql sqlite adapter

声明

private List<SQLiteHelper> messages = new ArrayList<>();
privare MessagesAdapter mAdapter;
private RecyclerView messageList;

初始化

messageList = (RecyclerView) findViewById(R.id.message_list);
messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
mAdapter = new MessagesAdapter(messages);
messageList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();

获取

    while (csr.moveToNext()) {
        String mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
        String mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
        String mTime = csr.getString(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, mTime, mSeen, mTimer, mType));
        mAdapter.notifyDataSetChanged();
    }

在获取数据时没有问题,但是在添加新数据时不会显示,直到我关闭活动并再次打开它。我知道stackoverflow有关于notifydatasetchanged不能正常工作的很多问题,但主要是关于listviews和recyclerviews的问题,而对于sqlite数据则没有,所以我不确定这是否是正确的方法。所以有人可以帮我吗

适配器的完整代码

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

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

public void updateData(List<SQLiteHelper> pMessages){
    if(pMessages== null || pMessages.size()==0)
        return;
    if (pMessages!= null && pMessages.size()>0)
        this.mMessagesHelperList.clear();
    this.mMessagesHelperList.addAll(pMessages);
    notifyDataSetChanged();
}

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

    public MessageViewHolder(View view) {
        super(view);
        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) {
    String mSender = null;
    String mMessage = null;
    String mTime;
    String mSeen = null;

    String mTimer;
    String mType;


    SQLiteHelper messagesHelper = mMessagesHelperList.get(position);

    if (mSender != null && mSender.equals("Me")) {
            holder.messageText.setBackgroundColor(Color.BLUE);
        holder.messageText.setTextColor(Color.WHITE);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
        params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        holder.messageText.setLayoutParams(params);

        if (mSeen.equals("Yes")){
            holder.messageText.setBackgroundColor(Color.CYAN);
        }else{
            holder.messageText.setBackgroundColor(Color.BLUE);
        }
    }else {
            holder.messageText.setBackgroundColor(Color.GREEN);
            holder.messageText.setTextColor(Color.BLACK);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
        params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        holder.messageText.setLayoutParams(params);
        }


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

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

}

2 个答案:

答案 0 :(得分:1)

您正在获取活动中的数据,但没有将新数据传递给Recyclerview适配器,当适配器列表更新时,notifyAdapter可以工作。 您可以在适配器中创建一个方法来获取新消息,每次添加新消息时都将调用该方法,并在那里通知适配器。

更新以下代码:

 while (csr.moveToNext()) {
    String mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
    String mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
    String mTime = csr.getString(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, mTime, mSeen, mTimer, mType));
    //mAdapter.notifyDataSetChanged();
    mAdapter.updateData(messages);
}

在您的适配器类中:

void updateData(List<SQLiteHelper> pMessages){
    if(pMessages== null || pMessages.size()==0)
           return;
    if (pMessages!= null && pMessages.size()>0) 
           this.messages.clear();  
    this.messages.addAll(pMessages);
        notifyDataSetChanged();    
     }

希望有帮助

答案 1 :(得分:0)

首先使用数据更新列表,然后像

一样在while循环外使用notifyItemRangeInserted
while (csr.moveToNext()) {
        String mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
        String mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
        String mTime = csr.getString(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, mTime, mSeen, mTimer, mType));
    }
    mAdapter.notifyItemRangeInserted(0,messages.size());