因此,我正在查询Firebase数据库,并尝试将数据读取到recyclerview中。现在,当我简单地将数据读入文本视图并将文本设置为数据时(即,我有3个文本视图-颜色,高度,名称),一切正常,并且显示了正确的数据。
但是,我一直试图将查询结果加载到回收站视图rvResults中,但它根本无法工作。我将数据添加到arraylists中,然后将包含查询结果的arrayLists(颜色,高度,名称)传递到我的ResultsRecyclerAdapater中。但是,当我运行时,我得到了一个空白屏幕-但没有崩溃。
我不是专家,但这似乎是问题所在。似乎没有将这些添加到arrayLists中。我的“植物”课很好,所以这不是问题。
String name = plant.getName();
String bio = plant.getBio();
String height = plant.getHeight();
String colour = plant.getColour();
names.add(name);
heights.add(height);
colours.add(colour);
任何帮助将不胜感激!
ArrayList<String> names = new ArrayList<>();
ArrayList<String> heights = new ArrayList<>();
ArrayList<String> colours = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Task task1 = plantRef.whereEqualTo("height", "Medium")
.get();
Task task2 = plantRef.whereEqualTo("colour", "Red")
.get();
Task<List<QuerySnapshot>> allTasks = Tasks.whenAllSuccess(task1, task2);
allTasks.addOnSuccessListener(new OnSuccessListener<List<QuerySnapshot>>() {
@Override
public void onSuccess(List<QuerySnapshot> querySnapshots) {
String data = "";
for(QuerySnapshot queryDocumentSnapshots : querySnapshots){
for (QueryDocumentSnapshot documentSnapshot:queryDocumentSnapshots){
Plant plant = documentSnapshot.toObject(Plant.class);
plant.setDocumentID(documentSnapshot.getId());
String documentId = plant.getDocumentID();
String name = plant.getName();
String bio = plant.getBio();
String height = plant.getHeight();
String colour = plant.getColour();
names.add(name);
heights.add(height);
colours.add(colour);
}
}
}
});
//set up the RecyclerView, setting the Layout to Horizontal, and binding the adapter
RecyclerView recyclerView = findViewById(R.id.rvResults);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
resultsAdapter = new ResultsRecyclerAdapter(this, names, heights, colours);
resultsAdapter.setClickListener(this);
recyclerView.setAdapter(resultsAdapter);
}
这里是我的ResultsRecyclerAdapter类:
public class ResultsRecyclerAdapter extends RecyclerView.Adapter<ResultsRecyclerAdapter.ViewHolder> {
private List<String> mData;
private List<String> mHeights;
private List<String> mColours;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
public ResultsRecyclerAdapter(Context c, List<String> mData, List<String> mHeights, List <String> mColours) {
this.mInflater = LayoutInflater.from(c);
this.mData = mData;
this.mHeights = mHeights;
this.mColours = mColours;
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recycler_view_second_activity, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
String name = mData.get(position);
String height = mHeights.get(position);
String colour = mColours.get(position);
holder.tvName.setText(name);
holder.tvColour.setText(height);
holder.tvHeight.setText(colour);
}
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvName;
TextView tvHeight;
TextView tvColour;
ViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tvName);
tvHeight = itemView.findViewById(R.id.tvHeight);
tvColour = itemView.findViewById(R.id.tvColour);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ResultsRecyclerAdapter.ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
答案 0 :(得分:0)
您的查询运行异步,您必须致电
resultAdapter.notifyDataSetChange();
在onSuccess(List<QuerySnapshot> querySnapshots)
的末尾,您的数据已准备就绪。
答案 1 :(得分:0)
您正在呼叫ResultsRecyclerAdapter
,然后在OnSuccessListener
中准备数据
onSuccess
完成后,尝试更新适配器