gpt4 book ai didi

java - Recyclerview 适配器不显示片段中的数据

转载 作者:行者123 更新时间:2023-12-01 17:51:19 25 4
gpt4 key购买 nike

我正在尝试通过回收器 View 在片段中显示数据库中的数据。我使用 for 循环来遍历数据库中的所有用户。所有既不是好友也没有发送请求的用户应该按照fragment类的if条件中的实现来显示。下面编写fragment类的代码。

public class AddMemberFragment extends Fragment {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private View view ;
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
String userId;
String n_team ;
ArrayList<Friend> friends;


public AddMemberFragment() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.addnewfriend_fragment, container, false);
return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

userId = firebaseUser.getUid();
ManageTeamTabbedActivity activity = (ManageTeamTabbedActivity) getActivity();
n_team = activity.getMyData();
mRecyclerView = (RecyclerView) getView().findViewById(R.id.addNewFriend);
//mRecyclerView.setHasFixedSize(true);//
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);

friends = new ArrayList<>();
mAdapter = new AddMemberAdapter(getActivity(), friends, n_team) ;
mRecyclerView.setAdapter(mAdapter);

mRootRef.child("users");
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Friend> friendArrayList = new ArrayList<>();
DataSnapshot friendsData = dataSnapshot.child(firebaseUser.getUid()).child("friends");
DataSnapshot requests = null;
DataSnapshot currentFriends = null;
if (friendsData.hasChild("requests")) {
requests = dataSnapshot.child(firebaseUser.getUid()).child("friends").child("requests");
}
if (friendsData.hasChild("currentFriends")) {
currentFriends = dataSnapshot.child(firebaseUser.getUid()).child("friends").child("currentFriends");
}

for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
Log.d("demo", "USERID " + FirebaseAuth.getInstance().getCurrentUser().getUid());
Friend friend = new Friend();
if ((userSnapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) || (requests != null && requests.hasChild(userSnapshot.getKey()))
|| (currentFriends != null && currentFriends.hasChild(userSnapshot.getKey()))) {

} else {
friend.setId(userSnapshot.getKey());
friend.setFriendName((String) userSnapshot.child("profiles").child("fname").getValue());
friendArrayList.add(friend);
}
}
friends = friendArrayList;
if (friends != null && !friends.isEmpty()) {
mAdapter.setFriendList(friends);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}
}

相应的适配器代码写在这里:

public class AddMemberAdapter extends RecyclerView.Adapter<AddMemberAdapter.RecyclerViewHolder>{

private ArrayList<Friend> mData;
private Context mContext;
private FirebaseAuth auth;
private String u_team ;

public AddMemberAdapter(Context mContext, ArrayList<Friend> friendList , String u_team) {
this.mData = friendList;
this.mContext = mContext;
this.u_team= u_team;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.addnewfriend_layout, parent, false);
AddMemberAdapter.RecyclerViewHolder viewHolder = new AddMemberAdapter.RecyclerViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
final Friend frnd =(Friend) mData.get(position);

holder.friendName.setText(frnd.getFriendName());
holder.addFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
frnd.setStatus("sent");
final DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final String UID = firebaseUser.getUid();
mRootRef.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mRootRef.child("users").child(UID).child("friends").child("requests").child(frnd.getId()).setValue(frnd);
Friend temp = new Friend();
temp.setId(firebaseUser.getUid());
temp.setFriendName(firebaseUser.getDisplayName());
temp.setStatus("received");
mRootRef.child("users").child(frnd.getId()).child("friends").child("requests").child(firebaseUser.getUid()).setValue(temp);
mData.remove(frnd);
notifyDataSetChanged();
}

@Override

public void onCancelled(DatabaseError databaseError) {

}
});
}
});
}

@Override
public int getItemCount() {
return mData.size();
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView friendName;
ImageButton addFriend;

public RecyclerViewHolder(View itemView) {
super(itemView);
friendName = (TextView)itemView.findViewById(R.id.textViewFriendName);
addFriend = (ImageButton) itemView.findViewById(R.id.imagebuttonFriendAdd);
}
}

public void setFriendList(ArrayList<Friend> friendList){
this.mData = friendList;
notifyDataSetChanged();
}
}

我得到的输出附在此处。

enter image description here

不是好友且当前用户未发送好友请求的用户应该通过此 RecyclerView 显示在此添加新成员片段中。我尝试自己调试并删除 if 条件,但它也不起作用。请指导如何显示它。

还附上了数据库屏幕截图。

enter image description here

最佳答案

我找不到您的 Firebase 查询和数据提取技术的问题,但是,使用这种数据结构,随着列表的增长,您将很难获取好友列表。据我了解,在该片段中显示列表只是需要花费大量时间。

此外,您还可以考虑在初始化 RecyclerView 时使用 RecyclerView 设置适配器。

您可以考虑执行以下操作。

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

userId = firebaseUser.getUid();
ManageTeamTabbedActivity activity = (ManageTeamTabbedActivity) getActivity();
n_team = activity.getMyData();
mRecyclerView = (RecyclerView) getView().findViewById(R.id.addNewFriend);
// mRecyclerView.setHasFixedSize(true); // Remove this line
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);

// Set the adapter here with an empty list first.
friends = new ArrayList<>();
mAdapter = new AddMemberAdapter(getActivity(), friends, n_team);
mRecyclerView.setAdapter(mAdapter);

mRootRef.child("users");
mRootRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Friend> friendArrayList = new ArrayList<>();
DataSnapshot friendsData = dataSnapshot.child(firebaseUser.getUid()).child("friends");
DataSnapshot requests = null;
DataSnapshot currentFriends = null;
if (friendsData.hasChild("requests")) {
requests = dataSnapshot.child(firebaseUser.getUid()).child("friends").child("requests");
}
if (friendsData.hasChild("currentFriends")) {
currentFriends = dataSnapshot.child(firebaseUser.getUid()).child("friends").child("currentFriends");
}

for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
Log.d("demo", "USERID " + FirebaseAuth.getInstance().getCurrentUser().getUid());
Friend friend = new Friend();
if ((userSnapshot.getKey().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) || (requests != null && requests.hasChild(userSnapshot.getKey()))
|| (currentFriends != null && currentFriends.hasChild(userSnapshot.getKey()))) {

} else {
friend.setId(userSnapshot.getKey());
friend.setFriendName((String) userSnapshot.child("profiles").child("fname").getValue());
friendArrayList.add(friend);
}
}
friends = friendArrayList;
if (friends != null && !friends.isEmpty()) {
mAdapter.setFriendList(friends); // Set the friend list to the adapter when the list is loaded.
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

在您的适配器中,您将需要一个函数来设置好友列表并调用 notifyDataSetChanged()

// In your adapter class, add the following method. 
public void setFriendList(ArrayList<Friend> friendList) {
this.mData = friendList;
notifyDataSetChanged();
}

我希望这会有所帮助。

关于java - Recyclerview 适配器不显示片段中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com