我正在显示订购的食物清单。一个FloatingActionButton放置在订购列表中每个食品项目(productId)上,我想显示一个评分警报对话框,以使用户在单击FloatingActionButton之后对食品进行评分。当用户单击Fab按钮时,如何获取每个productId?因为我需要在另一个基于productId的食物详细信息页面上显示评分。我只是一个初学者,所以不太了解。谢谢
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();
}
答案 0 :(得分:1)
您可以使用interface来获取活动上的浮动操作按钮的回调,如下所示:-
在您的适配器类中
public interface OnRatingButtonClickListener{
void onRatingClick(int productId);
}
,您将像这样在适配器的构造函数中注册此接口对象:-
private OnRatingButtonClickListener mOnRatingClickListener;
public OrderAdapter(OnRatingButtonClickListener listener){
this.mOnRatingClickListener = listener;
}
并在onBindViewHolder中,您将执行以下操作:-
holder.btnRating.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view){
//interface object
mOnRatingClickListener.onRatingClick(order.getProductId());
});
并在您的活动中实现此界面并显示您的评分对话框。 您必须像这样设置适配器:-
OrderAdapter adapter = new OrderAdapter(this);
并在您的活动中实现接口,您将在活动中获得如下方法:-
@Override
public onRatingClick(int productId){
// here you will get the id of your product and you can show dialog here
}
如果有任何疑问,可以询问。