如何在ArrayList之间传递数据? [英] How to pass data between ArrayLists?

查看:162
本文介绍了如何在ArrayList之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android studio的新手,即时通讯在如何将某些特定数据从一个arraylist传递到另一个arraylist时遇到了麻烦.我目前正在使用订购系统.我试图将数量> 0的特定数据(名称,价格和数量)从我的阵列列表添加到我的阵列列表中.

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.

产品列表

public class product_List extends ListFragment{
ArrayList<product> productList;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        productList = new ArrayList<product>;

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

        }
}

OrderList

OrderList

public class order_List extends ListFragment {

ArrayList<order> orderList;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        orderList = new ArrayList<order>;

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

我为product.class(s​​tring,double,int,int)和order.class(s​​tring,double,int)有一个单独的类.我也有一个用于productlist的arrayAdapter,它的按钮可以增加/减少产品的数量.

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.

推荐答案

通常,使用您提供的代码,您不能简单地在这些ArrayList之间传递"数据,因为它们是不同的类型,这不是Android开发的问题,但纯粹是Java.

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<Product> orders1 = new ArrayList<>();
orders.add(coffee);

ArrayList<Product> orders2 = new ArrayList<>();
orders2.add(orders1.get(0)); // "passing"

您可以创建一个Order的类,该类为extends Product,但这似乎并非完全必要.例如,ArrayList<Product>可以被视为订单或产品库存的实例.

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

与Android相关的更高级的主题涉及为该Product类实现Parcelable.但是,考虑到以上想法,所有这些都归结为更干净地构建代码.

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.

这篇关于如何在ArrayList之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆