简体   繁体   English

如何通过单击FAB按钮获取ID

[英]how to get id by clicking FAB button

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. 一个FloatingActionButton放置在订购列表中每个食品项目(productId)上,我想显示一个评分警报对话框,以使用户在单击FloatingActionButton之后对食品进行评分。 How do I get each of the productId when user click on the Fab button? 当用户单击Fab按钮时,如何获取每个productId? Because I need to display the ratings at another food detail page based on the productId. 因为我需要在另一个基于productId的食物详细信息页面上显示评分。 I am just a beginner learning so don't really understand.Thanks 我只是初学者,所以不太了解。

OrderDetailAdapter.java 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 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();

}

You can use interface to get the callback of the floating action button on your activity like this :- 您可以使用interface来获取活动上浮动操作按钮的回调,如下所示:

In your adapter class 在您的适配器类中

public interface OnRatingButtonClickListener{
  void onRatingClick(int productId);
}

and you will register this interface object in constructor of your adapter like this:- 并且您将在适配器的构造函数中注册此接口对象,如下所示:-

private OnRatingButtonClickListener mOnRatingClickListener;

public OrderAdapter(OnRatingButtonClickListener listener){
 this.mOnRatingClickListener = listener;
}

and in onBindViewHolder, you will do this:- 在onBindViewHolder中,您将执行以下操作:

 holder.btnRating.setOnClickListener(new View.OnClickListener()  
 @Override
 public void onClick(View view){
    //interface object
    mOnRatingClickListener.onRatingClick(order.getProductId());
 });

and implement this interface in your activity and show your rating dialog. 并在您的活动中实现此界面,并显示您的评分对话框。 you have to set the adapter like this :- 您必须像这样设置适配器:

  OrderAdapter adapter = new OrderAdapter(this);

and implements the interface in your activity and you will get the method in your activity like this:- 并在您的活动中实现该接口,您将在活动中获得如下方法:-

@Override
public onRatingClick(int productId){
  // here you will get the id of your product and you can show dialog here
}   

If any furthur query,you can ask. 如果有任何疑问,可以询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM