java - 如何覆盖现有数据?
问题描述
我有两个recyclerview (rvone, rvtwo) 这个rvone是在rvone项被点击时显示数据列表的那个,数据的值减少这个项的一个例子
标题 Rp.1000 X 4 pcs 共 4000
好吧,如果它被点击它必须是标题 Rp.1000 X 3 pcs 总共 3000 不是只减少了一个吗,那一个移动到 rvtwo 意味着它变成了这样
标题 Rp.1000 X 1 pcs 共 1000
好吧,该功能已经在运行,但是每次单击它时,即使新添加对现有的也不是新的
这是一个示例标题 Rp.1000 X 4 pcs 共 4000 // 这是点击减少,缺少显示在 rvtwo
// 表示像这个标题 Rp.1000 X 1 pcs 共 1000
和 rvone 成为标题 Rp.1000 X 3 pcs 共 3000
当我再次点击时,它变成了这样的 rvone:title Rp.1000 X 2 pcs 2000 total
rvtwo:标题 Rp.1000 X 1 pcs 共 1000
标题 Rp.1000 X 1 pcs 共 1000
相反,它变成了两个项目,而不是在注释之前覆盖它 :我只是添加了构造函数和 bindViewHolder 的代码
这是我的 rvone_adapter 函数 onclick 将数据发送到 rvtwo
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {
List<OrderModel> orderModels;
public List<OrderCatchModel> orderCatchModels = new ArrayList<>();
List<OrderDetailModel> orderDetailModels;
Context context;
OrderCatchAdapter orderCatchAdapter;
OrderActivity orderActivity;
public OrderAdapter(Context context, List<OrderModel> orderModels,List<OrderDetailModel> orderDetailModels){
this.context = context;
this.orderModels = orderModels;
this.orderDetailModels = orderDetailModels;
}
@Override
public void onBindViewHolder(@NonNull final OrderViewHolder holder, final int position) {
final OrderModel orderModel = orderModels.get(0);
final Long id = orderModel.getOrderDetailModels().get(position).getId();
final String itemName = orderModel.getOrderDetailModels().get(position).getItem();
final long price = orderModel.getOrderDetailModels().get(position).getPrice();
final long quantity = orderModel.getOrderDetailModels().get(position).getQuantity();
//final long amount = orderModel.getOrderDetailModels().get(position).getAmount();
holder.mTxt_item.setText(orderModel.getOrderDetailModels().get(position).getItem());
holder.mTxt_price.setText("Rp. " + orderModel.getOrderDetailModels().get(position).getPrice());
holder.mTxt_quantity.setText(orderModel.getOrderDetailModels().get(position).getQuantity() + " Pcs");
holder.mTxt_amount.setText("Rp. " + orderModel.getOrderDetailModels().get(position).getAmount());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quantity == 0){
orderModels.get(0).getOrderDetailModels().remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,orderModels.get(0).getOrderDetailModels().size());
}else {
long q1 = quantity - 1;
long a1 = q1 * price;
orderModel.getOrderDetailModels().get(position).setQuantity(q1);
orderModel.getOrderDetailModels().get(position).setAmount(a1);
notifyDataSetChanged();
OrderCatchModel orderCatchModel = new OrderCatchModel();
orderCatchModel.setId(id);
orderCatchModel.setItem(itemName);
orderCatchModel.setPrice(price);
orderCatchModel.setQuantity(quantity - q1);
orderCatchModel.setAmount((quantity - q1) * price);
orderCatchModels.add(orderCatchModel);
if (context instanceof OrderActivity) {
((OrderActivity)context).addItemToRvTwo(orderCatchModels, orderModel.getOrderDetailModels(), orderModels);
}
}
}
});
}
这是我的 rvtwo_adapter 函数 onclick 以将数据从 rvtwo 发送回 rvone
public OrderCatchAdapter(Context context, List<OrderCatchModel> orderCatchModel, List<OrderDetailModel> orderDetailModels, List<OrderModel> orderModels){
this.context = context;
this.orderDetailModels = orderDetailModels;
this.orderCatchModels = orderCatchModel;
this.orderModels = orderModels;
}
@Override
public void onBindViewHolder(@NonNull OrderCatchViewHolder holder, final int position) {
final OrderCatchModel orderCatchModel = orderCatchModels.get(position);
final Long id = orderCatchModel.getId();
final String itemName = orderCatchModel.getItem();
final long price = orderCatchModel.getPrice();
final long quantity = orderCatchModel.getQuantity();
holder.mTxt_item.setText(orderCatchModel.getItem());
holder.mTxt_price.setText("Rp. " + orderCatchModel.getPrice());
holder.mTxt_quantity.setText(orderCatchModel.getQuantity() + " Pcs");
holder.mTxt_amount.setText("Rp. " + orderCatchModel.getAmount());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quantity <= 0){
orderCatchModels.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,orderCatchModels.size());
notifyDataSetChanged();
}else {
long q1 = quantity - 1;
long a1 = q1 * price;
orderCatchModel.setQuantity(q1);
orderCatchModel.setAmount(a1);
notifyDataSetChanged();
// Long idItem = orderModels.get(0).getOrderDetailModels().get(position).getId();
OrderDetailModel orderDetailModel = new OrderDetailModel();
orderDetailModel.setId(id);
orderDetailModel.setItem(itemName);
orderDetailModel.setPrice(price);
orderDetailModel.setQuantity(quantity - q1);
orderDetailModel.setAmount((quantity - q1) * price);
orderDetailModels.add(orderDetailModel);
if (context instanceof OrderActivity) {
((OrderActivity)context).addDataToRvOne(orderModels, orderDetailModels);
}
}
}
});
}
这是我的活动
public void addDataToRvOne(List<OrderModel> orderModels, List<OrderDetailModel> orderDetailModels) {
orderAdapter = new OrderAdapter(this, orderModels,orderDetailModels);
LinearLayoutManager linearLayoutManagerRvOne = new LinearLayoutManager(this);
rv_one.setLayoutManager(linearLayoutManagerRvOne);
rv_one.setAdapter(orderAdapter);
orderAdapter.notifyItemInserted(itemPosition);
itemPosition += 1;
}
public void addItemToRvTwo(List<OrderCatchModel> orderCatchModels, List<OrderDetailModel> orderDetailModels, List<OrderModel> orderModels) {
orderCatchAdapter = new OrderCatchAdapter(this, orderCatchModels,orderDetailModels, orderModels);
LinearLayoutManager linearLayoutManagerRvTwo = new LinearLayoutManager(this);
rv_two.setLayoutManager(linearLayoutManagerRvTwo);
rv_two.setAdapter(orderCatchAdapter);
orderCatchAdapter.notifyItemInserted(itemPosition);
itemPosition += 1;
}
这是抛出项目 rv_one 的模型
public class OrderDetailModel implements Serializable {
private Long id;
private String item;
private long price;
private long quantity;
private long amount;
public OrderDetailModel(
Long id,
String item,
long price,
long quantity
) {
this.id = id;
this.item = item;
this.price = price;
this.quantity = quantity;
this.amount = price * quantity;
}
public OrderDetailModel() {
}
这个模型 rv_two 捕获项目 rv_one
public class OrderCatchModel implements Serializable {
private Long id;
private String item;
private long price;
private long quantity;
private long amount;
public OrderCatchModel(Long id, String item, long price, long quantity, long amount) {
this.id = id;
this.item = item;
this.price = price;
this.quantity = quantity;
this.amount = amount;
}
public OrderCatchModel() {
}
这是虚拟数据
@SuppressLint("SimpleDateFormat")
public class MockData {
public static List<OrderModel> ORDER_MODELS;
static {
ORDER_MODELS = new ArrayList<>();
try {
order(
1L,
"#00000001-001",
new SimpleDateFormat("dd-MM-yyyy HH:mm").parse("21-08-2019 09:47"),
null,
"MJ002",
"Sugiyono",
Arrays.asList(
new OrderDetailModel(
1L,
"Nasi Goreng",
10000,
2
),
new OrderDetailModel(
2L,
"Bubur Ayam",
9000,
4
),
new OrderDetailModel(
3L,
"Bubur Kacang",
5000,
5
),
new OrderDetailModel(
4L,
"Jus Alpukat",
8000,
6
),
new OrderDetailModel(
5L,
"Jus Durian",
15000,
2
),
new OrderDetailModel(
6L,
"Jus Apel",
13000,
3
)
)
);
} catch (Exception ex) {
Log.wtf(MockData.class.getSimpleName(), ex);
}
}
@SuppressWarnings("SameParameterValue")
private static void order(
Long id,
String number,
Date date,
String customer,
String table,
String staff,
List<OrderDetailModel> orderDetailModels
) {
OrderModel orderModel = new OrderModel();
orderModel.setId(id);
orderModel.setNumber(number);
orderModel.setDate(date);
orderModel.setCustomer(customer);
orderModel.setTable(table);
orderModel.setStaff(staff);
orderModel.getOrderDetailModels().addAll(orderDetailModels);
ORDER_MODELS.add(orderModel);
}
}
问我你是否想要另一个代码
解决方案
推荐阅读
- python - 在倾斜数据集的训练中使用 Precision 和 Recall
- uima - 在 Uima Pipeline 中使用时,TreeTagger 找不到 Charsetname
- amazon-web-services - AWS 中的实时实时视频处理
- javascript - R闪亮包中的父/子行
- c++ - 简化大量模板特化
- mysql - Netbean SQL检查数据库中是否存在名称
- python - 将 `tf.Summary` 对象序列化为 `string` 张量
- csv - 将远程文件加载到 Power BI
- javascript - 为什么http服务器在6次请求后没有响应
- sql - 我想要一些 SQL 语法来检查值是否与其下面的不同