В настоящее время я пытаюсь внедрить Firestore Recycler Adapter в свой проект Android Studio, но когда я пытаюсь войти в действие с помощью recycler, происходит сбой приложения. Когда я использую отладчик, он падает, когда достигает метода .build () объекта FireStoreRecyclerOptions. Это то, что я делаю, и если нет, есть ли обходной путь?
Вот код для реализации RecyclerView:
public class SecurityCurrentRequests extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference requestRef = db.collection("Request");
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security_current_requests);
setUpRecycler();
}
public void setUpRecycler()
{
Query query = requestRef.orderBy("priority", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
adapter = new Adapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
protected void onStart(){
super.onStart();
adapter.startListening();
}
@Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
, и это класс адаптера:
public class Adapter extends FirestoreRecyclerAdapter<Request, Adapter.RequestViewHolder> {
public Adapter(@NonNull FirestoreRecyclerOptions<Request> options){
super(options);
}
@NonNull
@Override
public RequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//create new instance of the ViewHolder
// using request_cardview custom layout
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.request_cardview, parent, false);
return new RequestViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RequestViewHolder holder, int position, @NonNull Request requestObject) {
String firstText = requestObject.getName();
String secondText = requestObject.getMessage();
int thirdText = requestObject.getPriority();
String fourthText = requestObject.getCurrentDate();
holder.text1.setText(firstText);
holder.text2.setText(secondText);
holder.text3.setText(thirdText);
holder.text4.setText(fourthText);
}
public class RequestViewHolder extends RecyclerView.ViewHolder{
private TextView text1, text2, text3, text4;
RequestViewHolder(View itemView){
super(itemView);
text1 = itemView.findViewById(R.id.name);
text2 = itemView.findViewById(R.id.description);
text3 = itemView.findViewById(R.id.text_view_priority);
text4 = itemView.findViewById(R.id.date);
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}