为什么我的RecyclerView没有显示适配器中的项目?

huangapple go评论83阅读模式
英文:

Why isn't my RecyclerView displaying my adapter items?

问题

InboxActivity.java:

public class InboxActivity extends AppCompatActivity {

    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbox);

        // ...

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userLst.clear();

                for (DataSnapshot snp : dataSnapshot.getChildren()){
                    Chat chat = snp.getValue(Chat.class);
                    if (chat.getSender().equals(user.getUid())){
                        userLst.add(chat.getReceiver());
                    }
                    if (chat.getReceiver().equals(user.getUid())){
                        userLst.add(chat.getSender());
                    }
                }
                readChat();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
    
    private void readChat() {
        inboxLst = new ArrayList<>();

        reference = FirebaseDatabase.getInstance().getReference("Users");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                userLst.clear();
                for (DataSnapshot snp : dataSnapshot.getChildren()) {
                    User usr = snp.getValue(User.class);

                    //Display users that are currently in an open chat
                    for (String id : userLst){
                        if(usr.getId().equals(id)){
                            if(inboxLst.size() != 0){
                                for (User usrl : inboxLst){
                                    if(!usr.getId().equals(usrl.getId())){
                                        inboxLst.add(usr);
                                    }
                                }
                            }else{
                                inboxLst.add(usr);
                            }
                        }
                    }
                }
                //Set the adapter for the recycler view once using the chat information retrieved from firebase database
                usrAdpt = new UserAdapter(InboxActivity.this,inboxLst);
                recyclerView.setAdapter(usrAdpt);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) { }
        });
    }
}

UserAdapter.java:

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    // ...

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(cont).inflate(R.layout.lst_layout_inbox, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        User user = nUser.get(position);
        holder.username.setText(user.getUsername());

        holder.usrLst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(cont, MessageActivity.class);
                intent.putExtra("userid", user.getId());
                cont.startActivity(intent);
            }
        });

    }

    // ...

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView username;
        RelativeLayout usrLst;

        public ViewHolder(View itemView){
            super(itemView);

            username = itemView.findViewById(R.id.usrName);
            usrLst = itemView.findViewById(R.id.usrCard);
        }
    }
}
英文:

So i have a recycler view that is designed to show users sending messages to each other from my Firebase RTD. Originally it was working to show all users and the recycler items were clicked then it would show messages between the users. However, when modifying it to only view users that were in open messages with the current user, the items that populated my recycler view no longer displays and an error saying E/RecyclerView: No adapter attached; skipping layout is logged within the Run tab.

Unfortunately i can no longer undo the changes and have struggled to find the cause of my problem but have assume that it is either taking place in my adapted or activity classes.

InboxActivity.java:

public class InboxActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List&lt;User&gt; inboxLst;
FirebaseUser user;
DatabaseReference reference;
UserAdapter usrAdpt;
private List&lt;String&gt; userLst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inbox);
//Identify and setup recycler view
recyclerView = findViewById(R.id.rycInbox);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userLst = new ArrayList&lt;&gt;();
//Get all chats between current user and and other users
user = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference(&quot;Chats&quot;);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userLst.clear();
for (DataSnapshot snp : dataSnapshot.getChildren()){
Chat chat = snp.getValue(Chat.class);
if (chat.getSender().equals(user.getUid())){
userLst.add(chat.getReceiver());
}
if (chat.getReceiver().equals(user.getUid())){
userLst.add(chat.getSender());
}
}
readChat();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readChat() {
inboxLst = new ArrayList&lt;&gt;();
reference = FirebaseDatabase.getInstance().getReference(&quot;Users&quot;);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userLst.clear();
for (DataSnapshot snp : dataSnapshot.getChildren()) {
User usr = snp.getValue(User.class);
//Display users that are currently in an open chat
for (String id : userLst){
if(usr.getId().equals(id)){
if(inboxLst.size() != 0){
for (User usrl : inboxLst){
if(!usr.getId().equals(usrl.getId())){
inboxLst.add(usr);
}
}
}else{
inboxLst.add(usr);
}
}
}
}
//Set the adapter for the recycler view once using the chat information retrieved from firebase database
usrAdpt = new UserAdapter(InboxActivity.this,inboxLst);
recyclerView.setAdapter(usrAdpt);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
}
}

UserAdapter.java:

public class UserAdapter extends RecyclerView.Adapter&lt;UserAdapter.ViewHolder&gt; {
private Context cont;
private List&lt;User&gt; nUser;
public UserAdapter(Context cont, List&lt;User&gt; nUser){
this.cont = cont;
this.nUser = nUser;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(cont).inflate(R.layout.lst_layout_inbox, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
User user = nUser.get(position);
holder.username.setText(user.getUsername());
holder.usrLst.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(cont, MessageActivity.class);
intent.putExtra(&quot;userid&quot;,user.getId());
cont.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return nUser.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
RelativeLayout usrLst;
public ViewHolder(View itemView){
super(itemView);
username = itemView.findViewById(R.id.usrName);
usrLst = itemView.findViewById(R.id.usrCard);
}
}
}

答案1

得分: 1

在你的问题中,唯一需要注意的是在以下方法中:

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userLst.clear(); // &lt;------ 为什么在这里清空列表???注释掉这一行----------
for (DataSnapshot snp : dataSnapshot.getChildren()) {
User usr = snp.getValue(User.class);
// 显示当前正在进行开放聊天的用户
for (String id : userLst){
if(usr.getId().equals(id)){
if(inboxLst.size() != 0){
for (User usrl : inboxLst){
if(!usr.getId().equals(usrl.getId())){
inboxLst.add(usr);
}
}
}else{
inboxLst.add(usr);
}
}
}
}
// 一次使用从 Firebase 数据库检索到的聊天信息设置回收站视图的适配器
usrAdpt = new UserAdapter(InboxActivity.this, inboxLst);
recyclerView.setAdapter(usrAdpt);
}

为什么你要执行 userLst.clear();

因为在 OnCreate() 方法中,你填充了 userLst 并调用了 readChat()。然后在 onDataChange() 中,你 清空userLst,然后之后你尝试迭代 userLst 来显示当前正在进行开放聊天的用户。

我建议你将这个 userLst.clear(); 注释掉,并在设置完项目后调用 notifyDataSetChanged()

英文:

The only catchup in your question is, in the following method:

     @Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
userLst.clear(); // &lt;------ WHY YOU CLEAR LIST HERE??? COMMENT THIS ----------
for (DataSnapshot snp : dataSnapshot.getChildren()) {
User usr = snp.getValue(User.class);
//Display users that are currently in an open chat
for (String id : userLst){
if(usr.getId().equals(id)){
if(inboxLst.size() != 0){
for (User usrl : inboxLst){
if(!usr.getId().equals(usrl.getId())){
inboxLst.add(usr);
}
}
}else{
inboxLst.add(usr);
}
}
}
}
//Set the adapter for the recycler view once using the chat information retrieved from firebase database
usrAdpt = new UserAdapter(InboxActivity.this,inboxLst);
recyclerView.setAdapter(usrAdpt);
}

why you do userLst.clear(); ?

Since in OnCreate() method you populate userLst and call readChat(). And in onDataChange() you clear the userLst and then afterwards you try to iterate over the userLst to Display users that are currently in an open chat.

I would suggest to comment this userLst.clear(); and after setting items call notifyDataSetChanged()

答案2

得分: 0

试试这些方法:

  • 在onCreate中设置适配器。
  • 并且在onDataChanged监听器中,更新ArrayList并调用notifyDataSetChanged()。

这应该会有帮助。

英文:

Try these things:

  • Set the adapter in onCreate.
  • and in the onDataChanged listener, update the ArrayList and call notifyDataSetChanged().

This should help

huangapple
  • 本文由 发表于 2020年4月9日 02:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定