如何在添加值时控制firebase中的重复数据?

时间:2018-01-17 05:55:38

标签: android firebase firebase-realtime-database

我构建了两个聊天应用程序,一个用于用户,另一个用于管理员以使用Firebase添加组。当我从用户端添加组时,它在用户端显示重复的组。如何检查数据添加时间并仅选择我添加的最后一个组?有没有办法只添加最新的组?以下是我的代码:

 private DatabaseReference mDatabaseRef;
    private List<ImageUpload> imgList;
    private List<groupuserstoken> imgList1;
    private List<groupuserstoken> imgList11;
    RecyclerView listView;
    ImageListAdapter adapter;
    DatabaseReference ref;
    int c=1;
    private DatabaseReference databaseReference;
    ProgressDialog progressDialog;
    ArrayList<String> name,description,image,members,check,topic,uploadid,users;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allgroups);
        listView=(RecyclerView) findViewById(R.id.allgroups11);
        mDatabaseRef = FirebaseDatabase.getInstance().getReference();
        imgList=new ArrayList<>();
        name=new ArrayList<>();
        description=new ArrayList<>();
        members=new ArrayList<>();
        users=new ArrayList<>();
        image=new ArrayList<>();
        check=new ArrayList<>();
        topic=new ArrayList<>();
        uploadid=new ArrayList<>();
        imgList11=new ArrayList<>();
        ref=FirebaseDatabase.getInstance().getReference();
        final Query query = mDatabaseRef.child("Groups");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ImageUpload img = snapshot.getValue(ImageUpload.class);
                    imgList.add(img);
                }
                for (int i = 0; i < imgList.size(); i++) {
                    name.add(imgList.get(i).getName());
                    description.add(imgList.get(i).getDescription());
                    image.add(imgList.get(i).getUrl());
                    members.add(imgList.get(i).getMembers());
                    topic.add(imgList.get(i).getTopic());
                    uploadid.add(imgList.get(i).uploadId);
                }

                listView.setLayoutManager(new LinearLayoutManager(Allgroups.this));
                adapter = new ImageListAdapter(Allgroups.this, name, description, image, members,  topic, uploadid);
                listView.setItemAnimator(new DefaultItemAnimator());
                listView.setAdapter(adapter);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
    @Override
    public void onItemClick(View view, int position) {

    }
}

1 个答案:

答案 0 :(得分:1)

您需要做的就是清除列表并尝试Firebase Tutorial尝试Firebase Helper Class

query.addListenerForSingleValueEvent(new ValueEventListener() 

用于一次性通话addListenerForSingleValueEvent

  for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                  if(imgList.size()>0){
    imgList.clear();
                  }

                    ImageUpload img = snapshot.getValue(ImageUpload.class);
                    imgList.add(img);
                }
                   name.clear();
                    description.clear();
                    image.clear();
                    members.clear();
                    topic.clear();
                    uploadid.clear();
                 for (int i = 0; i < imgList.size(); i++) {
                    name.add(imgList.get(i).getName());
                    description.add(imgList.get(i).getDescription());
                    image.add(imgList.get(i).getUrl());
                    members.add(imgList.get(i).getMembers());
                    topic.add(imgList.get(i).getTopic());
                    uploadid.add(imgList.get(i).uploadId);
                }