java 传递arraylist,如何在ArrayList之间传递数据?

im new with android studio and im having trouble on how can I pass some specific data/s from one arraylist to another arraylist. Im currently working on with an ordering system. Iam trying to add the specific data (Name, Price & Qty) from my Arraylist to my Arraylist if Qty > 0.

Edited:

ProductList

public class product_List extends ListFragment{

ArrayList productList;

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

productList = new ArrayList;

productList.add(new product("Pasta 1", $10.00, 0(this is for Quantity), R.drawable.pastaIcon1));

}

}

OrderList

public class order_List extends ListFragment {

ArrayList orderList;

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

orderList = new ArrayList;

// HERE I want to add the product/s if its quantity > 0

}

}

I have a separate class for the product.class(string, double, int, int) and order.class(string, double, int). I also have an arrayAdapter for productlist that has buttons that will increment/decrement the quantity of the product.

解决方案

Generally, with the code you've given you can't simply "pass" data between those ArrayList because they are different types, which is not an Android development problem, but purely Java.

Follow this pattern, and you can "pass data between lists", but the overall solution here, is to restructure your code to follow Object Oriented Principles.

class Product {

int id; // Some unique identifer

String name; // What the product is called

double cost; // What it costs

int resourceId; // A resource ID for Android to draw for this

}

Then, a "coffee" class is too descriptive, rather it is just a "product", where a "coffee" is the instance of that product.

Product coffee = new Product("coffee");

ArrayList orders1 = new ArrayList<>();

orders.add(coffee);

ArrayList orders2 = new ArrayList<>();

orders2.add(orders1.get(0)); // "passing"

You could make an Order class that extends Product, but that doesn't seem entirely necessary; an ArrayList can be considered an instance of orders, or product inventory, for example.

More advanced Android-related topics involve implementing Parcelable for that Product class. But, with the above idea in mind, it all comes down to structuring your code more cleanly.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值