我在应用程序中实时更新我的好友列表(mListView)视图时遇到了一些问题,它似乎只在启动时更新。我知道它是一个更新功能的问题,但我不知道该使用什么。
(我对firebase和android很新,我还在学习它们。)
以下是代码:
private ListView mListView;
private ArrayList<String> mFriends;
private List<User> mUserList;
private UserListAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_friends, container, false);
mSearchParam = (EditText) view.findViewById(R.id.searchFriends);
mListView = (ListView) view.findViewById(R.id.lvProfileFriends);
mPhotos = new ArrayList<>();
mUserList = new ArrayList<>();
mFriends = new ArrayList<>();
DisplayFriends();
return view;
}
private void DisplayFriends(){
getFriends();
}
private void updateUsersList(){
Log.d(TAG, "updateUsersList: updating users list");
mAdapter = new UserListAdapter(getActivity(), R.layout.layout_user_listitem, mUserList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: selected user: " + mUserList.get(position).toString());
//navigate to profile activity
Intent intent = new Intent(getActivity(), ViewProfileFragment.class);
intent.putExtra(getString(R.string.calling_activity), getString(R.string.fragment_profile));
intent.putExtra(getString(R.string.intent_user), mUserList.get(position));
startActivity(intent);
}
});
}
private void updateFriendsList(){
Log.d(TAG, "getPhotos: getting Friend(s)");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
for(int i = 0; i < mFriends.size(); i++){
final int count = i;
Query query = reference
.child(getString(R.string.dbname_users))
.orderByChild(getString(R.string.field_user_id))
.equalTo(mFriends.get(i));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
mUserList.add(singleSnapshot.getValue(User.class));
}
if(count >= mFriends.size() - 1){
updateUsersList();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
private void getFriends(){
Log.d(TAG, "getFollowing: searching for FRIENDS **************************************");
mFriends.clear();
mUserList.clear();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_friends))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange: found friend----------------**: " + singleSnapshot.getValue(User.class).toString());
mFriends.add(singleSnapshot.child(getString(R.string.field_user_id)).getValue().toString());
Log.d(TAG, "onDataChange: Freinds user id: " + mFriends.toString() );
}
updateFriendsList();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
答案 0 :(得分:1)
看看这个:
} catch (IOException exception) {
final Throwable[] suppressedExceptions = exception.getSuppressed();
// exception = the exception from try
// suppressedExceptions = the last exception (if any) is likely caused by closing
}
答案 1 :(得分:0)
每当您在friendList上收到firebase更新时,您可以在适配器上调用For i = 0 To Manager.Grid.Rows.Count - 1
Text = Manager.Grid.Rows(i).Cells(0).Value & ";" & Manager.Grid.Rows(i).Cells(1).Value
Write.WriteLine(Text)
Next
,这样适配器就会知道必须执行更新。
我建议您使用 RecyclerView 来实现此目的,因为它比 listview 更灵活。