gpt4 book ai didi

android - 如何处理更新 UI 的 RecyclerView 内的点击?

转载 作者:行者123 更新时间:2023-11-29 18:35:48 25 4
gpt4 key购买 nike

我有一个 RecylcerView,它显示存储在 SQLite 中的项目列表。

enter image description here

在每一行上,都有向上和向下按钮,单击时会增加/减少数量。假设我点击了 ABCD 行的向下按钮,然后金额文本将更改为“金额:1”

到目前为止,这是我的代码:

ItemAdapter.java

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> {

private List<Itemt> itemList;
private Context ctxt;
private MyClickListener clickListener;

public ItemAdapter(Context ctxt, List<Item> list, MyClickListener listener){
this.ctxt = ctxt;
this.itemtList = list;
this.clickListener = listener;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int type) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);

return new ItemAdapter.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Item item = itemtList.get(position);
holder.tvName.setText(item.getName());
holder.tvPrice.setText(item.getPrice());
holder.tvAmount.setText("Amount: "+ item.getAmount());
}

@Override
public int getItemCount() {
return itemList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {

public TextView tvNama, tvHargaJual, tvHargaBeli, tvJumlah;
public Button btnInc, btnDec;
MyClickListener listener;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
tvPrice = itemView.findViewById(R.id.tv_price);
tvAmount = itemView.findViewById(R.id.tv_amount);
btnInc = itemView.findViewById(R.id.btn_inc);
btnDec = itemView.findViewById(R.id.btn_dec);

btnInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickListener.onIncrement(view, getAdapterPosition());
}
});

btnDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickListener.onDecrement(view, getAdapterPosition());
}
});
}
}

public interface MyClickListener {
void onIncrement(View v, int position);
void onDecrement(View v, int position);
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

RecyclerView recView
DBHelper db;
List<Item> itemList;
Item item;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recView = (RecyclerView) findViewById(R.id.recycler_view);
itemtList = new ArrayList<>();

db = new DBHelper(this);
itemList.addAll(db.getAllItems()); // load all stored items in SQLite db

mAdapter = new ProductAdapter(this, productList, new ProductAdapter.MyClickListener() {
@Override
public void onIncrement(View v, int position) {
item = itemList.get(position);
}

@Override
public void onDecrement(View v, int position) {
item = itemList.get(position);
}
});

}
}

目前的问题是点击向上/向下按钮时,它不能修改金额文本。如何纠正?

最佳答案

首先,您必须在onIncrementonDecrement 方法中更改amount 的值。像这样:

item = itemList.get(position);
itme.setAmount(item.getAmount()++);
----------------------------------------------
item = itemList.get(position);
itme.setAmount(item.getAmount()--);

然后您必须通知 recyclerView 适配器数据已被此代码更改:

 mAdapter.notifyDataSetChanged();

这里是所有的代码:

mAdapter = new ProductAdapter(this, productList, new ProductAdapter.MyClickListener() {
@Override
public void onIncrement(View v, int position) {
item = itemList.get(position);
itme.setAmount(item.getAmount()++);
mAdapter.notifyDataSetChanged();
}

@Override
public void onDecrement(View v, int position) {
item = itemList.get(position);
itme.setAmount(item.getAmount()--);
mAdapter.notifyDataSetChanged();
}
});

关于android - 如何处理更新 UI 的 RecyclerView 内的点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54442628/

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