Recycler View 没有更新和显示任何内容

Posted

技术标签:

【中文标题】Recycler View 没有更新和显示任何内容【英文标题】:Recycler View not updating and displaying anything 【发布时间】:2021-09-22 18:42:08 【问题描述】:

我正在创建一个简单的笔记应用程序,在该应用程序中,我在 Firestore 的回收站视图中显示内容。代码中有问题,它没有在 recylerview 中显示内容(尽管每次添加新注释时我都会得到一个新项目)。此外,它不会在我添加新注释后立即更新,它会在我销毁应用程序后更新。

主要活动

public class MainActivity extends AppCompatActivity 

    RecyclerView recyclerView;
    CustomAdapter customAdapter;
    ArrayList<Model> models;
    FirebaseFirestore firebaseFirestore;

    FloatingActionButton fab;

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

        recyclerView = findViewById(R.id.recyclerView);
        models = new ArrayList<>();
        firebaseFirestore = FirebaseFirestore.getInstance();

        customAdapter = new CustomAdapter(this, models);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(customAdapter);

        firebaseFirestore = FirebaseFirestore.getInstance();

        firebaseFirestore.collection("my_notes_collection").get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() 
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) 
                        if(!queryDocumentSnapshots.isEmpty())
                            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
                            for(DocumentSnapshot d : list)
                                Model model = d.toObject(Model.class);
                                models.add(model);
                            
                            customAdapter.notifyDataSetChanged();
                        
                        else
                            Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();
                    
                ).addOnFailureListener(new OnFailureListener() 
                    @Override
                    public void onFailure(@NonNull Exception e) 
                        Toast.makeText(getApplicationContext(), "Fail to get the data.", Toast.LENGTH_SHORT).show();
                    
                );


        fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> addData());
    

    private void addData() 
        Intent intent = new Intent(this, DataAdd.class);
        startActivity(intent);
    

自定义适配器

public class CustomAdapter  extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>

    Context context;
    ArrayList<Model> models;

    public CustomAdapter(Context context, ArrayList<Model> models)
        this.context = context;
        this.models = models;
    

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list_content, parent, false);

        return new MyViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) 
        holder.time_text.setText(models.get(holder.getBindingAdapterPosition()).getText());
        holder.string_text.setText(models.get(holder.getBindingAdapterPosition()).getNotes());
        holder.constraintLayout.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent intent = new Intent(context.getApplicationContext(), NotesDetails.class);
                intent.putExtra("position", String.valueOf(holder.getBindingAdapterPosition()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            
        );
    

    @Override
    public int getItemCount()  return models.size(); 

    public class MyViewHolder extends RecyclerView.ViewHolder 
        TextView string_text, time_text;
        ConstraintLayout constraintLayout;

        public MyViewHolder(View itemView) 
            super(itemView);
            string_text = itemView.findViewById(R.id.textView);
            time_text = itemView.findViewById(R.id.timeshow);
            constraintLayout = itemView.findViewById(R.id.constraintLayout);
        
    

【问题讨论】:

linearLayoutManager.setReverseLayout(true) 和 linearLayoutManager.setStackFromEnd(true);将以相反的顺序显示recyclerview。因此,请检查是否将新项目添加到列表的顶部而不是底部。 请编辑您的问题并将您的数据库结构添加为屏幕截图。请回复@AlexMam 【参考方案1】:

看起来,它不更新 RV 的原因是因为我们还没有要求代码这样做。

因此,当DataAdd 活动使用startActivityForResult() 完成添加任务时,请查看an example here

然后当我们知道添加成功时,我们会收到一个回调,onActivityResult() 然后我们从 Firebase 重新加载列表

firebaseFirestore.collection("my_notes_collection").get()
 ...

再次更新列表并通知数据集..

【讨论】:

嗨@Sum,这个方法对你有用吗?

以上是关于Recycler View 没有更新和显示任何内容的主要内容,如果未能解决你的问题,请参考以下文章

Recycler VIew 和 Adapter 打开内容不同的Activity

在使用 Recycler View 时尝试在空对象引用上调用虚拟方法

Recycler View Android中的Firestore数据[重复]

Recycler View项目onClick无法正常工作。(工作异常)

Recycler View 不显示数据

如何在 Fragment 中使用 Recycler View [重复]