Reputation: 57
I've followed this tutorial at YouTube.
I manage to search the data but now I want to open the details page of selected data on recycler view. How do I get it done? At which part should I implement the onClickListener
for recyclerview? I've tried to put onClickListener
, intent put extra ... in method OnBindViewHolder and SetDetails, but both of them have problems to recognize my context.
Here is my current code:
private void firebaseIngredientSearch(String searchIngredient) {
Toast.makeText(SearchActivity.this, "Started Search", Toast.LENGTH_LONG).show();
Query firebaseSearchQuery = databaseReference.orderByChild("ingName").startAt(searchIngredient).endAt(searchIngredient + "\uf8ff");
FirebaseRecyclerOptions<Ingredients> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Ingredients>()
.setQuery(firebaseSearchQuery, Ingredients.class)
.build();
FirebaseRecyclerAdapter<Ingredients, SearchActivity.IngredientsViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<Ingredients, SearchActivity.IngredientsViewHolder>(firebaseRecyclerOptions) {
@Override
protected void onBindViewHolder(@NonNull IngredientsViewHolder holder, int position, @NonNull final Ingredients model) {
holder.setDetails(model.getIngName(), model.getIngStatus(),model.getIngCategory(),model.getIngCategory());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goDetail = new Intent(, IngredientSearchDetailsActivity.class);
goDetail.putExtra("ingName", model.getIngName());
goDetail.putExtra("ingStatus", model.getIngStatus());
goDetail.putExtra("ingCategory", model.getIngCategory());
goDetail.putExtra("ingDescription", model.getIngDescription());
mActivity.startActivity(goDetail);
}
});
}
@NonNull
@Override
public IngredientsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_searched_ingredient, parent, false);
return new IngredientsViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
rvSearchResult.setAdapter(firebaseRecyclerAdapter);
}
////////////////////////////////////////////////////////////////////////////////
public static class IngredientsViewHolder extends RecyclerView.ViewHolder {
View view;
public IngredientsViewHolder(@androidx.annotation.NonNull View itemView) {
super(itemView);
view = itemView;
}
public void setDetails(String ingName, String ingStatus, String ingCategory, String ingDescription) {
TextView i_Name = view.findViewById(R.id.tvName);
TextView i_Status = view.findViewById(R.id.tvStatus);
TextView i_Category = view.findViewById(R.id.tvCategory);
TextView i_Description = view.findViewById(R.id.tvDescription);
ImageView iv_Status = view.findViewById(R.id.searchStatusIv);
i_Name.setText(ingName);
i_Status.setText(ingStatus);
i_Category.setText(ingCategory);
i_Description.setText(ingDescription);
}
}
Upvotes: 1
Views: 66
Reputation: 15423
User holder.view.setOnClickListener
to initiate OnClickListener
and use holder.view.getContext()
instead of mActivity
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent goDetail = new Intent(, IngredientSearchDetailsActivity.class);
goDetail.putExtra("ingName", model.getIngName());
goDetail.putExtra("ingStatus", model.getIngStatus());
goDetail.putExtra("ingCategory", model.getIngCategory());
goDetail.putExtra("ingDescription", model.getIngDescription());
holder.view.getContext().startActivity(goDetail);
}
});
Upvotes: 1