我想要的数据在recyclerview,但它没有显示在那里,但当我试图传递数据在toast它显示数据在那里,我不知道为什么。我试过firebaseui,但它也没有工作
{依赖关系
implementation 'com.firebaseui:firebase-ui-auth:8.0.1'
implementation 'com.firebaseui:firebase-ui-database:8.0.1'
implementation 'androidx.fragment:fragment-ktx:1.5.5'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-storage:20.0.1'
implementation 'com.google.firebase:firebase-database:20.0.4'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
implementation 'com.google.firebase:firebase-firestore-ktx:24.4.4'
testImplementation 'junit:junit:4.13.2'
implementation 'com.karumi:dexter:6.2.3'
implementation 'com.firebaseui:firebase-ui-firestore:8.0.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}
这是我的主要类,我想在其中的数据。
public class createdplaylist extends Fragment {
ArrayList<String> str=new ArrayList<>();
RecyclerView recyclerView;
palylistadapter palylistadapter;
Context context;
FirestoreRecyclerAdapter adapter;
FirebaseFirestore firestore;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_createdplaylist, container, false);
context=container.getContext();
firestore = FirebaseFirestore.getInstance();
Query query=firestore.collection("playlist");
recyclerView =view.findViewById(R.id.recyclerr);
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable
FirebaseFirestoreException error) {
for(DocumentSnapshot ds: value.getDocuments()){
str.add(ds.getId());
Toast.makeText(context, ds.getId(), Toast.LENGTH_SHORT).show();
}
}
});
palylistadapter=new palylistadapter(context,str);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(palylistadapter);
return view;
}
my adapter class
public class palylistadapter extends RecyclerView.Adapter<palylistadapter.viewhold> {
Context context;
ArrayList<String>list;
public palylistadapter( Context c,ArrayList<String>s){
this.list=s;
this.context=c;
}
@NonNull
@Override
public viewhold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(context).inflate(R.layout.playlistlayout,parent,false);
viewhold viewhold=new viewhold(v);
return viewhold;
}
@Override
public void onBindViewHolder(@NonNull viewhold holder, int position) {
holder.t.setText(list.get(position));
}
@Override
public int getItemCount() {
if(list.isEmpty())
return 0;
else
return list.size();
}
public class viewhold extends RecyclerView.ViewHolder {
TextView t;
public viewhold(@NonNull View itemView) {
super(itemView);
t=itemView.findViewById(R.id.textname);
}
}
}
//忽略这部分mdgxbjkdbjvzzdjxcvbfxbcnbmxcbzgvjbvxkbvmxnvbzxv
您没有在RecylerView中获得任何内容,因为您正在从Firestore读取数据,但您没有通知适配器您已将对象添加到列表中。因此,为了解决这个问题,您必须重新排列代码并在for循环结束后立即调用notifyDataSetChanged():
palylistadapter=new palylistadapter(context,str);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(palylistadapter);
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable
FirebaseFirestoreException error) {
for(DocumentSnapshot ds: value.getDocuments()){
str.add(ds.getId());
Toast.makeText(context, ds.getId(), Toast.LENGTH_SHORT).show();
}
palylistadapter.notifyDataSetChanged();
// 👆
}
});