我在应用程序中更新我的好友列表(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) {
}
});
}