【问题标题】:Merge recyclerview item合并回收站查看项目
【发布时间】:2020-04-25 23:20:10
【问题描述】:

我试图让同一类别下的所有项目都在同一个地方,但它会创建新的我尝试了很多选项我得到了同样的东西

This is what I'm getting

我希望所有正在运行的项目(例如这里)都在一起而不是分开

   if (y != null) {
        //y is the category
        for (int i = 0; i <keyList.size(); i++) {
            //keyList is a set of firebase key under y
            ArrayList<BookDetails> itemArrayList = new ArrayList<>();
            reference.child(keyList.get(i)).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    BookDetails bookDetails = dataSnapshot.getValue(BookDetails.class);
                    itemArrayList.add(bookDetails);

                    adapter.notifyDataSetChanged();



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

            sectionModelArrayList.add(new SectionBookModel(y, itemArrayList));

        }
    }

Here's an image of my database

这是结构

    {
  "Action" : {
    "-M5fYnj16773AVNYaMfE" : 
{
      "-M5fYqLScVzjMqw6xKsc" : true
    },
    "-M5jnhMoaXvnKt_Ide1y" : 
{
      "-M5jnitmW6pzFpZ6SKno" : true
    }
  },
  "Adventure" : {
    "-M5fYzWj5wX-TJdII0Ad" : 
{
      "-M5fZ-viqt0Dx4P1qeZR" : true
    }
  },
  "Biographies" : {
    "-M5n4DuadS8S_ENpwByp" : 
{
      "-M5n4GM89tADD980N1y8" : true
    }
  }
}

我真的很陌生,我不知道我做错了什么。谢谢

【问题讨论】:

  • 添加数据库的屏幕截图而不是链接。
  • @HasanBouTaam 我更新了我的代码

标签: android firebase-realtime-database arraylist android-recyclerview


【解决方案1】:

我不知道你为什么要重新创建列表,列表必须在 for 循环之外:

这一行:

//must be outside the for loop to prevent recreating it
ArrayList<BookDetails> itemArrayList = new ArrayList<>();

【讨论】:

  • 非常感谢,傻我。那是错误
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 2021-11-05
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
相关资源
最近更新 更多