我有一个ListView填充Firebase存储中的用户注释,然后在另一个选项卡中有一个ListView,我可以从第一个ListView发送注释。我遇到的问题是,当我将注释添加到第二个ListView时,它不会更新,除非用户注销并重新登录或关闭应用程序并重新打开它:/我一直在尝试找出原因关于注销或关闭该应用程序可以使此工作正常进行,但我似乎无法弄清楚,我将在下面留下一些代码以进行深入研究,看看我可能做错了什么,大声感谢您的帮助! :)
注意:在第一个和第二个列表视图中添加注释实际上是相同的,因此我不确定是什么导致了第二个列表的这种行为:/并发送文件工作正常,它实际上只是将文件发送到Firebase及以下下载后,代码只会填充ListView,与第一个列表相同:/
填充第二个列表
mCollabsDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> allFiles = dataSnapshot.getChildren();
for (DataSnapshot eachFilename : allFiles) {
String each = eachFilename.getKey();
NoteFilenames.add(each);
}
for (String eachFile : NoteFilenames) {
String filename = eachFile + Utilities.FileExtention;
Note note = Utilities.getNotesByName(mContext, filename);
notes.add(note);
NoteAdapter.notifyDataSetChanged();
}
collab_notes_list.setAdapter(NoteAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Utilities.getNotesByName(上下文上下文,字符串文件名)
public static Note getNotesByName(Context context, String filename) {
File file = new File(context.getFilesDir(), "notes");
File newFile = new File(file, filename);
Note note;
if (newFile.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try {
fis = new FileInputStream(newFile);
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
return null;
}