简体   繁体   中英

How do i add elements to my ArrayList one after the other?

Basically i have this code that's supposed to do this: Each time i call getAddedInvoices it's gonna add another element to the list, but my problem is that i'm new to the ArrayList so i'm not sure about how the thing works and it keeps adding elements like on top of each other instead of one after the other, This is the Code:

List<InvoiceData> data = new ArrayList<InvoiceData>();

public List<InvoiceData> getAddedInvoices() {
    int f=0;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    String jsonString = ReadingURL("http://pos.avancari.co.cr/app/buscar.productos.php");
    try{
        JSONArray jsonItems = new JSONArray(jsonString);
        for(int i =0; i<jsonItems.length();i++){
            if(jsonItems.getJSONObject(i).getString("itemCode").equals(addedcode)){
                InvoiceData row = new InvoiceData();
                row.id = addedcant;
                row.invoiceNumber = i;
                row.productName = jsonItems.getJSONObject(i).getString("itemDesc");
                row.productCode = jsonItems.getJSONObject(i).getString("itemCode");
                row.precio = jsonItems.getJSONObject(i).getString("itemPrice");
                data.add(row);
                f++;
            }
            else{
                System.out.println("No entra a condicional con: "+addedcode);
            }
        }
        return data;
    }catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

That was the function, and then i just call it like this:

Invoices invoices = new Invoices();
List<InvoiceData> data = invoices.getAddedInvoices();

It's adding the selected elements correctly but idk why it just holds one and just replaces it with the new one instead of keep adding them. Thanks in Advance

*Edit: The problem is that in the "if", even if it goes 5 fives times doesn't detect that it's another add apparently, because i added another data.add and this time i got size: 2 (which i didn't have before, it just said 1)

Here is my suggestion, make getAddedInvoices as void return type, do not return the data as its already global.

In this line you are reassigning which can be a problem. List data = invoices.getAddedInvoices();

Since "data" is already global, just use it after invoices.getAddedInvoices() is called instead of getting a return value assigned again.

Your issue, if I understand what you want, is that you overwrite your data everytime you call your method. Since java methods pass non-primitive objects by reference, you can do like this :

//In your target class or as a field
List<InvoiceData> data = new ArrayList<InvoiceData>();

// That's another method
public void addInvoices(ArrayList<InvoiceData> pData) {
    int f=0;
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    String jsonString = 
        ReadingURL("http://pos.avancari.co.cr/app/buscar.productos.php");
    try{
        JSONArray jsonItems = new JSONArray(jsonString);
        for(int i =0; i<jsonItems.length();i++){
            if(jsonItems.getJSONObject(i).getString("itemCode").equals(addedcode)){
                InvoiceData row = new InvoiceData();
                row.id = addedcant;
                row.invoiceNumber = i;
                row.productName = jsonItems.getJSONObject(i).getString("itemDesc");
                row.productCode = jsonItems.getJSONObject(i).getString("itemCode");
                row.precio = jsonItems.getJSONObject(i).getString("itemPrice");
                pData.add(row);
                f++;
            }
            else{
                System.out.println("No entra a condicional con: "+addedcode);
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

Then call it like this :

invoices.addInvoices(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM