如何通过单击FAB按钮获取ID [英] how to get id by clicking FAB button
问题描述
我正在显示订购的食物清单。一个FloatingActionButton放置在订购列表中每个食品项目(productId)上,我想显示一个评分警报对话框,以使用户在单击FloatingActionButton之后对食品进行评分。当用户单击Fab按钮时,如何获取每个productId?因为我需要在另一个基于productId的食物详细信息页面上显示评分。我只是一个初学者,所以不太了解。谢谢
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
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 :-
在适配器类中
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;
}
,然后在onBindViewHolder中,您将执行以下操作:-
and in onBindViewHolder, you will do this:-
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.
这篇关于如何通过单击FAB按钮获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!