I am displaying a ordered food list. A FloatingActionButton is placed on each food item(productId) at the ordered list, and I want to show a rating alert dialog to let user rate the food after clicking the FloatingActionButton. How do I get each of the productId when user click on the Fab button? Because I need to display the ratings at another food detail page based on the productId. I am just a beginner learning so don't really understand.Thanks
OrderDetailAdapter.java
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, quantity, price, discount;
public FloatingActionButton btnRating;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.product_name);
quantity = (TextView)itemView.findViewById(R.id.product_quantity);
price = (TextView)itemView.findViewById(R.id.product_price);
discount = (TextView)itemView.findViewById(R.id.product_discount);
btnRating = (FloatingActionButton)itemView.findViewById(R.id.btn_rating);
}
}
public class OrderDetailAdapter extends RecyclerView.Adapter<MyViewHolder>{
List<Order> myOrders;
private OnRatingButtonClickListener mOnRatingClickListener;
public OrderDetailAdapter(List<Order> myOrders, OnRatingButtonClickListener listener) {
this.myOrders = myOrders;
this.mOnRatingClickListener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.order_detail_layout,parent,false);
return new MyViewHolder(itemView);
}
public interface OnRatingButtonClickListener
{
void onRatingClick(String productId);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Order order = myOrders.get(position);
holder.name.setText(String.format("Name: %s", order.getProductName()));
holder.quantity.setText(String.format("Quantity: %s", order.getQuantity()));
holder.price.setText(String.format("Price: %s", order.getPrice()));
holder.discount.setText(String.format("Discount: %s", order.getDiscount()));
holder.btnRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mOnRatingClickListener.onRatingClick(order.getProductId());
}
});
}
@Override
public int getItemCount() {
return myOrders.size();
}
}
OrderDetail.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_detail);
//Firebase
database = FirebaseDatabase.getInstance();
foods = database.getReference("Food");
ratingTbl = database.getReference("Rating");
//Initialize view
order_id = (TextView)findViewById(R.id.order_id);
order_phone = (TextView)findViewById(R.id.order_phone);
order_total = (TextView)findViewById(R.id.order_total);
lstFoods = (RecyclerView)findViewById(R.id.lstFoods);
lstFoods.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
lstFoods.setLayoutManager(layoutManager);
btnRating = (FloatingActionButton)findViewById(R.id.btn_rating);
if(getIntent() != null) {
order_id_value = getIntent().getStringExtra("OrderId");
foodId = getIntent().getStringExtra("FoodId");
//Set value
order_id.setText(order_id_value);
order_phone.setText(Common.currentRequest.getPhone());
order_total.setText(Common.currentRequest.getTotal());
OrderDetailAdapter adapter = new OrderDetailAdapter(Common.currentRequest.getFoods(),this);
//OrderDetailAdapter adapter = new OrderDetailAdapter(this);
adapter.notifyDataSetChanged();
lstFoods.setAdapter(adapter); }
@Override
public void onRatingClick(String productId) {
btnRating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRatingDialog();
}
});
}
private void showRatingDialog() {
new AppRatingDialog.Builder()
.setPositiveButtonText("Submit")
.setNegativeButtonText("Cancel")
.setNoteDescriptions(Arrays.asList("Very Bad","Bad","Quite OK","Very Good","Excellent"))
.setDefaultRating(1)
.setTitle("Rate this food")
.setDescription("Kindly give your ratings and comment")
.setTitleTextColor(R.color.transparentBlack)
.setDescriptionTextColor(R.color.transparentBlack)
.setHint("Please write your comment here...")
.setHintTextColor(R.color.fbutton_color_midnight_blue)
.setCommentTextColor(android.R.color.black)
.setCommentBackgroundColor(android.R.color.white)
.setWindowAnimation(R.style.RatingDialogFadeAnim)
.create(OrderDetail.this)
.show();
}