我只想在recyclerview中显示特定文档中的字段。除了在FirestoreRecyclerOptions内部传递查询以返回集合中的所有文档外,我在Internet上找不到任何解决方案。
如何在recyclerview中仅显示一个文档字段,而不是全部? 例如:显示第一个文档的电子邮件,名称,电话号码。
我用来显示集合用户中所有文档的代码:
private CollectionReference colref = fStore.collection("users");
PassengerAdapter mDriverAdapter;
private void setUpRecyclerView() {
Query query = colref;
FirestoreRecyclerOptions<PassengerDetails> options = new FirestoreRecyclerOptions.Builder<PassengerDetails>()
.setQuery(query, PassengerDetails.class)
.build();
mDriverAdapter = new PassengerAdapter(options);
RecyclerView recyclerView = findViewById(R.id.recylerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mDriverAdapter);
}
PassengerAdapter类:
class PassengerAdapter extends FirestoreRecyclerAdapter<PassengerDetails, PassengerAdapter.PassengerHolder> {
public PassengerAdapter(@NonNull FirestoreRecyclerOptions<PassengerDetails> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull PassengerAdapter.PassengerHolder holder, int position, @NonNull PassengerDetails model) {
holder.name.setText(model.getName());
holder.phone.setText(model.getPhoneno());
}
@NonNull
@Override
public PassengerAdapter.PassengerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.passenger_list,
parent, false);
return new PassengerAdapter.PassengerHolder(v);
}
class PassengerHolder extends RecyclerView.ViewHolder {
TextView name;
TextView phone;
public PassengerHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
phone = itemView.findViewById(R.id.phoneno);
}
}
}
PassengerDetails
public class PassengerDetails {
String fName,phoneno;
public PassengerDetails(){
}
public PassengerDetails(String name, String phoneno) {
this.fName = name;
this.phoneno = phoneno;
}
public String getName() {
return fName;
}
public void setName(String name) {
this.fName = name;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
}