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.

Edited: ProductList

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

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
        }
}

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.

mark.jon
  • 107
  • 12
  • 8
    Woha, a lot of code you are posting here. Maybe you want to turn to the help center to read about providing minimal, viable examples. You see, you won't find many people willing to dive through all of that. Hint x2: please study java coding style conventions. You don't use _ like that; class names always start Uppercase. Not adhering to conventions makes your code *harder* to read for anybody with reasonable Java experience. – GhostCat Aug 03 '16 at 18:05
  • Your code is difficult to read because you are not following basic naming conventions, that is that classes should always be Capitalized. – Rabbit Guy Aug 03 '16 at 18:06
  • Given the title of your question; you might want to study this question: http://stackoverflow.com/questions/7042182/how-to-make-a-deep-copy-of-java-arraylist ... probably that gives you the knowledge you need to fix your code. – GhostCat Aug 03 '16 at 18:06
  • 1
    Whoah, please reduce the code and add code conventions! More people will want to help then – Adam Ratzman Aug 03 '16 at 18:19
  • im sorry about the previous lengthy codes, i already edited the codes, i hope you people can still provide me with some ideas. thx – mark.jon Aug 05 '16 at 09:52

1 Answers1

0

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"

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.


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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Im thinking of passing multiple items from different food categories(coffee,pasta, etc.) to the order array, then save them to another list and empty the order array to take new orders.. my question is, is it possible to do without a database or should I use sqlite/mysql? – mark.jon Aug 06 '16 at 06:04
  • It's definitely possible with and without a database, but there's too much code required to write out all those details in an answer here – OneCricketeer Aug 06 '16 at 11:52
  • i see, ill try posting a new question regarding that topic. if you dont mind, ill tag you there for your inputs. – mark.jon Aug 06 '16 at 12:48