更新RecyclerView onChildChanged Firebase

时间:2017-08-31 15:29:54

标签: java android firebase firebase-realtime-database

我正在尝试更新我的recyclerview但是我收到错误

  

java.lang.ArrayIndexOutOfBoundsException:length = 12;指数= -1

from Bio import SeqIO
index = 0
with open("out_file.txt","w") as f:
    for seq_record in SeqIO.parse("in_file.txt", "fasta"):
        if "Human adenovirus" in seq_record.description:
            index += 1
            print "%i]" % index, str(seq_record.description) 
            print str(seq_record.seq) + "\n"

帮助我理解这个逻辑。 如果我在mDatabase.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Upload upload = dataSnapshot.getValue(Upload.class); uploads.add(upload); adapter = new MyAdapter(MainActivity.this, uploads); recyclerView.setAdapter(adapter); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { // uploads.clear(); String key=dataSnapshot.getKey(); Upload upload = dataSnapshot.getValue(Upload.class); uploads.set(uploads.indexOf(key),upload); adapter.notifyDataSetChanged(); } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(DatabaseError databaseError) { } }); onChildChanged之外没有做任何事情,那么它会创建重复的Notify

1 个答案:

答案 0 :(得分:0)

好吧,我明白了

您将需要两个数组来完成此操作。

// Lists
private final List<ChatModel> lIndividualChats = new ArrayList<>();
private ArrayList<String> mKeys = new ArrayList<>();

然后

public void onChildAdded(@NonNull DataSnapshot dataSnapshot,
                        @Nullable String s) {

   ChatModel chatModel = dataSnapshot.getValue(ChatModel.class);
   lIndividualChats.add(chatModel);
   chatsAdapter.notifyDataSetChanged();

   String key = dataSnapshot.getKey();
   mKeys.add(key);

}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot,
                          @Nullable String s) {

   ChatModel chatModel = dataSnapshot.getValue(ChatModel.class);
   String key = dataSnapshot.getKey();

   int index = mKeys.indexOf(key);
   lIndividualChats.set(index,
                        chatModel);
   chatsAdapter.notifyDataSetChanged();

}

通过使用另一个简单字符串类型的数组,您将能够访问模型数组列表的索引。