我有一个RecylcerView,它显示存储在SQLite中的项目列表。
每行上都有一个向上和按钮,单击时可以增加/减少金额。假设我点击了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);
}
});
}
}
当前问题是单击向上/向下按钮时,它不能修改金额文本。该如何纠正?
答案 0 :(得分:1)
您应该在btn.setOnClickListener()
中进行操作,而不是在MyViewHolder
类中进行操作onBindViewHolder()
。
访问如下所示的元素:
@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());
holder.btnInc.setOnClickListener(
//do ur stuff here
)
holder.btnInc.setOnClickListener(
//do ur another stuff here
)
}
同时删除onClickListener()
类中按钮的MyViewHolder
。因为MyViewHolder
只是您查找所有UI元素的地方。该操作在onBindViewHolder()
答案 1 :(得分:1)
首先,必须在amount
和onIncrement
方法中更改onDecrement
的值。像这样的东西:
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();
}
});
答案 2 :(得分:0)
只需更改项目数量,然后调用mAdapter.notifyItemChanged(position), 示例:
mAdapter = new ProductAdapter(this, productList, new ProductAdapter.MyClickListener() {
@Override
public void onIncrement(View v, int position) {
item = itemList.get(position);
item.setAmount(item.getAmount()++);
mAdapter.notifyItemChanged(position);
}
@Override
public void onDecrement(View v, int position) {
item = itemList.get(position);
item.setAmount(item.getAmount()--);
mAdapter.notifyItemChanged(position);
}
});