【发布时间】:2025-12-25 00:15:12
【问题描述】:
我正在显示订购的食物清单。 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();
}
【问题讨论】:
-
您的订单型号应该有产品ID
-
@VivekMishra 是的,我在订单模型中有产品 ID。我不知道如何编写代码来获取 OrderDetailAdapter 中的产品 ID
-
@VivekMishra 我不知道如何完成这一行以获得 productId:holder.btnRating.....
-
setOnClickListener 在你的按钮上并写 order.getProductId
-
@VivekMishra 我之前试过这条线: holder.btnRating.setOnClickListener(order.getProductId());但它显示错误。 (View中的setOnClickListener不能应用到java.lang.string)
标签: java android nullpointerexception dialog floating-action-button