我是Android Studio的新手,因此我尝试使用其Adapter和ViewHolder制作一个RecyclerView,Adapter类需要一个列表来制作列表中的每个项目,该列表应从FireBase的数据库中填充。 FireBase参考检索所需的数据并将其添加到列表中,但是列表始终保持为空,为什么?
这是代码:
MainActivity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public class MainActivity extends AppCompatActivity {
private RecyclerView rView;
private RecyclerView.LayoutManager layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rView = findViewById(R.id.rView);
layout = new LinearLayoutManager(this);
rView.setLayoutManager(layout);
rView.setHasFixedSize(true);
ProjectAdapter reAdapter = new ProjectAdapter();
rView.setAdapter(reAdapter);
}
}
适配器:
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.ArrayList;
import java.util.List;
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {
private List<Project> proList;
private FirebaseFirestore firestore;
private FirebaseStorage storage;
private StorageReference storageRef;
private Project p;
public ProjectAdapter(){
firestore = FirebaseFirestore.getInstance();
storage = FirebaseStorage.getInstance();
storageRef = FirebaseStorage.getInstance().getReference();
proList = new ArrayList<>();
firestore.collection("Projects").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if(e == null){
for(DocumentSnapshot doc : documentSnapshots){
Log.d("MainActivity", "onComplete: " + doc.getString("proName"));
p = new Project();
p.setProName(doc.getString("proName"));
p.setProDes(doc.getString("proDescription"));
p.setProPic(doc.getString("picture"));
System.out.println(p.getProName());
proList.add(p);
}
}
}
});
}
public static class ProjectViewHolder extends RecyclerView.ViewHolder {
public TextView proName;
public ImageView proPic;
public TextView proBreif;
public ProjectViewHolder(@NonNull View itemView) {
super(itemView);
proName = itemView.findViewById(R.id.proName);
proBreif = itemView.findViewById(R.id.DesctextView);
proPic = itemView.findViewById(R.id.proPic);
}
}
@NonNull
@Override
public ProjectViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_item, viewGroup, false);
return new ProjectViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProjectViewHolder projectViewHolder, int i) {
projectViewHolder.proName.setText(proList.get(i).getProName());
projectViewHolder.proBreif.setText(proList.get(i).getProDes());
[projectViewHolder.proPic.setImageURI(Uri.parse(proList.get(i).getProPic()));][1]
}
@Override
public int getItemCount() {
System.out.println(proList.size()); // the output always 0, it got called before retrieving form the database.
return proList.size();
}
@Override
public int getItemViewType(final int position) {
return R.layout.activity_item;
}
}
输出为:
名称是从数据库中检索的数据。
答案 0 :(得分:0)
我建议您使用Firestore的内置适配器之一-Firestore Recycler Adapter。 https://github.com/firebase/FirebaseUI-Android/tree/master/firestore
它是专门为此目的而构建的,并且易于实现。