私は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は、まだまだ混乱しています。
:FirebaseUIライブラリでこのコードを参照して、そのようなアダプタの例を参照するには
。 –
ああ、私は巣を見逃した。そして、あなたはすでに 'discoverPeopleList'リストを外側の' onDataChange'に作成しています。私はそこで何が起こっているのか分かりませんが、 'usersRef'の子イベントを聞くことをお勧めします。 –
実際には、同じフラグメントに別のRecyclerAdapterを追加する前に、うまくいきました。私は自分の質問を編集してクラス全体をそこに入れるべきですか? –