2017-02-01 12 views
0

私はRecyclerViewを使用してカードのリストを表示しています。カード上の特定のボタンを押すと、それに関連付けられているアイテムはRecyclerViewと同様にデータベースから削除されたものとみなされ、更新されたRecyclerViewに残りのアイテムが表示されます。RecyclerViewアイテムが削除されない

ここで、ボタンを押して項目を削除すると、Firebase ValueListenerがトリガーされ、新しいRecyclerViewが生成されますが、残りの項目はそれぞれ複製されるという問題があります。これは、私に各項目の2つを持つ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

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など、個々の子イベントに応答できます。これはrecyclerviewにループはそのために、すべての任意の項目をアップ終わらない、次のオブジェクトに移りするたびにクリアしますhttps://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseRecyclerAdapter.java

+0

:FirebaseUIライブラリでこのコードを参照して、そのようなアダプタの例を参照するには

。 –

+0

ああ、私は巣を見逃した。そして、あなたはすでに 'discoverPeopleList'リストを外側の' onDataChange'に作成しています。私はそこで何が起こっているのか分かりませんが、 'usersRef'の子イベントを聞くことをお勧めします。 –

+0

実際には、同じフラグメントに別のRecyclerAdapterを追加する前に、うまくいきました。私は自分の質問を編集してクラス全体をそこに入れるべきですか? –

関連する問題