gpt4 book ai didi

java - 如何在回收 View 中使用按钮?

转载 作者:行者123 更新时间:2023-12-01 17:14:46 24 4
gpt4 key购买 nike

我有这个餐厅应用程序,我想要像 FoodPanda 那样进行结帐(如果您考虑到布局)。 recyclerview 项目应包含一个包含一种产品数量的 TextView ,周围有两个用于减少和增加该数量的按钮,然后是两个包含产品名称和价格的 TextView 。到目前为止我已经在我的适配器类中尝试过这个:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<Product> product;
private OnItemClickListener mListener;

public interface OnItemClickListener {
void decrementQ(int position);
void incrementQ(int position);
}

public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}

public static class MyViewHolder extends ViewHolder {

public ImageView incrementQty, decrementQty;
public TextView productName, price, qty;

public MyViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
incrementQty = itemView.findViewById(R.id.incrementProduct);
decrementQty = itemView.findViewById(R.id.decrementProduct);
qty = itemView.findViewById(R.id.qtyProduct);
productName = itemView.findViewById(R.id.productName);
price = itemView.findViewById(R.id.productPrice);

decrementQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.decrementQ(position);
}
}
});

incrementQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.incrementQ(position);
}
}
});
}

}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Product> thisProduct) {
product = thisProduct;
}

// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.products, parent, false);
MyViewHolder mv = new MyViewHolder(v, mListener);
return mv;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Product currentProduct = product.get(position);
holder.decrementQty.setImageResource(currentProduct.getDecrementSource());
holder.qty.setText(currentProduct.getQty());
holder.incrementQty.setImageResource(currentProduct.getIncrementSource());
holder.productName.setText(currentProduct.getProductName());
holder.price.setText(currentProduct.getProductPrice());

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return product.size();
}
}

这是我的结帐 Activity 类:

public class checkout extends AppCompatActivity {

private ArrayList<Product> product;
private RecyclerView recyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);

product = new ArrayList<>();
product.add(new Product(R.drawable.minus, "1", R.drawable.plus, "Buttered Noodles", "15 RON"));
product.add(new Product(R.drawable.minus, "2", R.drawable.plus, "Teryiaki","20 RON"));
product.add(new Product(R.drawable.minus, "3", R.drawable.plus, "ginger", "3 RON"));

buildCheckout();
}
public void removeItem(int position) {
product.remove(position);
mAdapter.notifyItemRemoved(position);
}

public void buildCheckout(){
recyclerView = findViewById(R.id.checkoutList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
mAdapter = new MyAdapter(product);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);

mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void decrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty--;
if(qty < 1)
removeItem(position);
else
q.setText(qty);
}

@Override
public void incrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty++;
q.setText(qty);
}
});
}
}

问题是,当我尝试与递增和递减按钮交互时,应用程序不断停止,我不知道下一步应该做什么或我做错了什么。

最佳答案

您将 Int 值而不是 String 值设置为 TextView,您将得到 ResourceNotFoundException:

@Override
public void incrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty++;
q.setText(qty);//ERROR change to q.setText("" + qty);
}

您在 decrementQ 中遇到同样的问题

关于java - 如何在回收 View 中使用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61404854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com