简体   繁体   English

我如何一个接一个地将元素添加到ArrayList中?

[英]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: 基本上,我有应该执行以下操作的代码:每次调用getAddedInvoices时,都会在列表中添加另一个元素,但是我的问题是ArrayList是我的新手,所以我不确定它的工作方式及其不断添加元素,例如彼此叠加,而不是一个接一个地添加,这是代码:

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. 它正确地添加了选定的元素,但是idk为什么它只保存一个元素并只用新的元素替换而不是继续添加它们。 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) *编辑:问题是即使在“ if”中,即使它经过5次五次,也没有发现它显然是另一个添加,因为我添加了另一个data.add,这次我得到的大小为2(我没有这样做)以前没说过1)

Here is my suggestion, make getAddedInvoices as void return type, do not return the data as its already global. 这是我的建议,请将getAddedInvoices设置为void返回类型,不要将数据返回为已经全局的数据。

In this line you are reassigning which can be a problem. 在这一行中,您正在重新分配,这可能是个问题。 List data = invoices.getAddedInvoices(); 清单资料= invoices.getAddedInvoices();

Since "data" is already global, just use it after invoices.getAddedInvoices() is called instead of getting a return value assigned again. 由于“数据”已经是全局数据,因此仅在调用invoices.getAddedInvoices()之后使用它,而不是重新获取返回值。

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 : 由于java方法通过引用传递非原始对象,因此您可以这样做:

//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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