RecyclerView项目未被删除

时间:2017-02-01 14:47:33

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

我正在使用RecyclerView来显示卡片列表。按下卡上的某个按钮时,应该从数据库和RecyclerView中删除与其关联的项目,因此会显示一个新更新的RecyclerView以及其余项目。

现在的问题是,当我按下按钮删除某个项目时,会触发Firebase ValueListener并生成一个新的RecyclerView,但相反,每个剩余的项目都会重复。这给了我一个RecyclerView,它有两个项目。

ValueEventListener代码块

usersRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            discoverPeopleList = new ArrayList<>();
            discoverPeopleRecyclerAdapter = new DiscoverPeopleRecyclerAdapter(getContext(), discoverPeopleList);
            discoverPeople.setAdapter(discoverPeopleRecyclerAdapter);

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                final java.lang.String
                        key = snapshot.getKey();
                DatabaseReference ref = database.getReference().child("users").child(key).child("followers");
                ref.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()){
                            boolean isBeingFollowed = false;
                            for (DataSnapshot ss : dataSnapshot.getChildren()){
                                if (ss.getKey().equals(mAuth.getCurrentUser().getUid())){
                                    isBeingFollowed = true;
                                }
                            }
                            if (!isBeingFollowed && !key.equals(mAuth.getCurrentUser().getUid())){
                                Log.v("Add", "One key added");
                                discoverPeopleList.add(key);
                                discoverPeopleRecyclerAdapter.notifyDataSetChanged();
                            }
                        }else if (!key.equals(mAuth.getCurrentUser().getUid())){
                            Log.v("Add", "One key added");
                            discoverPeopleList.add(key);
                            discoverPeopleRecyclerAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
            discoverPeopleProgressBar.setVisibility(View.GONE);
            discoverSessionsProgressBar.setVisibility(View.GONE);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

适配器类中的OnClickListener代码块(这是从数据库中删除项目的位置)

holder.followBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (holder.followBtn.getText().equals("follow")){
                DatabaseReference followers = FirebaseDatabase.getInstance().getReference().child("users").child(userKey).child("followers");
                followers.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(FirebaseAuth.getInstance().getCurrentUser().getUid());

                FirebaseDatabase.getInstance().getReference().child("notificationRequests").child("username").setValue(userKey);
                FirebaseDatabase.getInstance().getReference().child("notificationRequests").child("message").setValue(FirebaseAuth.getInstance().getCurrentUser().getUid() + " followed you");

                DatabaseReference following = FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("following");
                following.child(userKey).setValue(userKey);

                holder.followBtn.setText("following");
            }else{
                DatabaseReference followers = FirebaseDatabase.getInstance().getReference().child("users").child(userKey).child("followers");
                followers.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).removeValue();

                DatabaseReference following = FirebaseDatabase.getInstance().getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("following");
                following.child(userKey).removeValue();
                holder.followBtn.setText("follow");
            }
        }
    });

布局XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#303030"
tools:context="com.veloxima.sporthub.LiveDiscover">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
...

    <android.support.v7.widget.RecyclerView
        android:id="@+id/discover_people_recyclerVIew"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"></android.support.v7.widget.RecyclerView>
...
</LinearLayout>
</ScrollView>

编辑:我已确认ArrayList确实已清除。另一方面,RecyclerView仍在搞乱。

1 个答案:

答案 0 :(得分:1)

如果usersRef下发生任何变化,您将获得该位置下数据的完整新快照。因此,您的代码最终会将所有用户(已删除的用户除外)再次添加到discoverPeopleList

要阻止这种情况发生,您需要在添加新数据之前清除discoverPeopleList

public void onDataChange(DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()){
        boolean isBeingFollowed = false;
        discoverPeopleList.clear();
        for (DataSnapshot ss : dataSnapshot.getChildren()){
            if (ss.getKey().equals(mAuth.getCurrentUser().getUid())){
                isBeingFollowed = true;
            }
        }
        if (!isBeingFollowed && !key.equals(mAuth.getCurrentUser().getUid())){
            Log.v("Add", "One key added");
            discoverPeopleList.add(key);
            discoverPeopleRecyclerAdapter.notifyDataSetChanged();
        }
    }else if (!key.equals(mAuth.getCurrentUser().getUid())){
        Log.v("Add", "One key added");
        discoverPeopleList.add(key);
        discoverPeopleRecyclerAdapter.notifyDataSetChanged();
    }

这样可以使用,但可以完全重绘RecyclerView。为了更精细地更新数据,您应该使用addChildEventListener。此监听器允许您响应单个子事件,例如onChildAdded用于添加用户(包括所有初始用户)和onChildRemoved用于删除它们。

要查看此类适配器的示例,请参阅FirebaseUI库中的以下代码:https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java