检索数据未出现在 ListView 的片段中
Posted
技术标签:
【中文标题】检索数据未出现在 ListView 的片段中【英文标题】:retrieving Data does not appear in fragment in ListView 【发布时间】:2020-06-20 12:52:12 【问题描述】:我想从 Firebase 实时数据库中检索保存的联系人。联系人保存在 Firebase 实时数据库中。但不检索。当用户接受聊天请求时,他的姓名已保存在数据库的联系人节点中。我想在联系人片段中检索此保存的联系人,但数据未出现在片段中。请帮帮我。这是代码:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".ContactsFragment"
android:background="@color/colorAccent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/contacts_list"
android:layout_
android:layout_ />
</RelativeLayout>
java
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* A simple @link Fragment subclass.
*/
public class ContactsFragment extends Fragment
private View ContactsView;
private RecyclerView myContactsList;
private DatabaseReference ContactsRef,UsersRef;
private FirebaseAuth mAuth;
private String currentUserID;
public ContactsFragment()
// Required empty public constructor
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
ContactsView= inflater.inflate(R.layout.fragment_contacts, container, false);
myContactsList=(RecyclerView) ContactsView.findViewById(R.id.contacts_list);
myContactsList.setLayoutManager(new LinearLayoutManager(getContext()));
mAuth=FirebaseAuth.getInstance();
currentUserID=mAuth.getCurrentUser().getUid();
ContactsRef= FirebaseDatabase.getInstance().getReference().child("Contacts").child(currentUserID);
UsersRef= FirebaseDatabase.getInstance().getReference().child("Users");
return ContactsView;
@Override
public void onStart()
super.onStart();
FirebaseRecyclerOptions options=new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(ContactsRef,Contacts.class)
.build();
FirebaseRecyclerAdapter<Contacts,ContactsViewHolder> adapter=new FirebaseRecyclerAdapter<Contacts, ContactsViewHolder>(options)
@Override
protected void onBindViewHolder(@NonNull final ContactsViewHolder holder, int position, @NonNull Contacts model)
String userIDs=getRef(position).getKey();
UsersRef.child(userIDs).addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if (dataSnapshot.hasChild("Image"))
String userProfileImage=dataSnapshot.child("Image").getValue().toString();
String profileName=dataSnapshot.child("name").getValue().toString();
String profileStatus=dataSnapshot.child("status").getValue().toString();
holder.userName.setText(profileName);
holder.userStatus.setText(profileStatus);
Picasso.get().load(userProfileImage).placeholder(R.drawable.profile_image).into(holder.profileImage);
else
String profileName=dataSnapshot.child("name").getValue().toString();
String profileStatus=dataSnapshot.child("status").getValue().toString();
holder.userName.setText(profileName);
holder.userStatus.setText(profileStatus);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
@NonNull
@Override
public ContactsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout,viewGroup,false);
ContactsViewHolder viewHolder=new ContactsViewHolder(view);
return viewHolder;
;
public static class ContactsViewHolder extends RecyclerView.ViewHolder
TextView userName,userStatus;
CircleImageView profileImage;
public ContactsViewHolder(@NonNull View itemView)
super(itemView);
userName=(TextView)itemView.findViewById(R.id.user_profile_name);
userStatus=(TextView)itemView.findViewById(R.id.user_status);
profileImage=(CircleImageView) itemView.findViewById(R.id.users_profile_image);
【问题讨论】:
【参考方案1】:您需要使用startListening
开始填充数据:
@Override
protected void onStart()
super.onStart();
adapter.startListening();
来自文档:
FirebaseRecyclerPagingAdapter 侦听滚动事件并仅在需要时从数据库加载其他页面。
要开始填充数据,请调用 startListening() 方法。您可能想在 onStart() 方法中调用它。在调用 startListening() 之前,请确保您已完成读取数据所需的任何身份验证,否则您的查询将失败。
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
【讨论】:
以上是关于检索数据未出现在 ListView 的片段中的主要内容,如果未能解决你的问题,请参考以下文章