简体   繁体   English

使用put extra通过3个活动发送数组列表

[英]send array list through 3 activities using put extra

what i have is three activities, the first one called penddingorders which i make an array list inside it and i add strings to it like this : 我所拥有的是三个活动,第一个活动称为penddingorders,它在其中创建一个数组列表,并向其中添加字符串,如下所示:

 ArrayList prodlist=new ArrayList();
    for(int z=0;z<=mylist.size();z++){
    if(id.equals(mylist.get(arg2).getOrderId()))
    {
    product=mylist.get(arg2).getProductName();
    quantity=mylist.get(arg2).getQuantity();
    unit=mylist.get(arg2).getUnit();
    price=mylist.get(arg2).getPrice();
    totalprice=mylist.get(arg2).getTotalPrice();
                       totalpriceAfterDiscount=mylist.get(arg2).getPriceAfterDiscount();
                                 note=mylist.get(arg2).getNote();
                                prodlist.add(product);
                                prodlist.add(quantity);
                                prodlist.add(unit);
                                prodlist.add(totalprice);
                                prodlist.add(price);
                                prodlist.add(totalpriceAfterDiscount);
                                prodlist.add(note);
                                arg2++;
                            }

and i have print it in the logcat and it shows correctly: 我已经在logcat中打印了它,并正确显示:

 Log.e("product list",prodlist+""); 

and i have send it from the first activity to the third activity like this , though i should pass through the second one before i go to the third one : 我已经将它从第一个活动发送到了第三个活动, 尽管我应该在进入第三个活动之前先通过第二个活动:

Intent intent = new Intent(PenddingOrders.this, ProductInfoDetails.class);
intent.putStringArrayListExtra("prodlist", prodlist);
startActivity(intent);

and in the third activity i get the array list through this code : 在第三个活动中,我通过以下代码获取数组列表:

try{
prodlist = getIntent().getStringArrayListExtra("prodlist");                     
}
catch(Exception e){
e.printStackTrace();
}
    Log.e("prodlist",prodlist+"");

but in the logcat i get null value .. so what i am doing wrong please help ?? 但是在logcat中,我得到了空值..所以我在做错什么,请帮忙?

you can set your property using getter setter so you can manage your value very well. 您可以使用getter setter来设置属性,从而可以很好地管理自己的价值。 and make it parceable. 并使其变得可笑。 so you can use it Extras ArrayList of it into next Activity. 因此您可以将它的Extras ArrayList使用到下一个Activity中。

Please follow this https://stackoverflow.com/a/15731120/942224 请遵循此https://stackoverflow.com/a/15731120/942224

This is my ans on stackoverflow please checked and hope it's help alot: 这是我对stackoverflow的回答,请检查并希望对您有所帮助:

Java / Android ArrayList in different Activity Java / Android ArrayList中的不同活动

Better to use Android Application class to persist data. 最好使用Android Application类来持久化数据。 For reference link 供参考链接

public class MyApplication extends Application {
    private ArrayList<String> list;

    public void setValue(ArrayList<String> list) {
        this.list = list;
    }

    public ArrayList<String> getValue() {
        return list;
    }

}

Step-1 First set the value from your Activity 步骤1首先从“活动”中设置值

MyApplication globalVariable = (MyApplication) getApplicationContext();
globalVariable.setValue(prodlist)// Set your ArraList that you want to store

Step-2 Retrieve the value in other activity like 步骤2检索其他活动中的值,例如

    MyApplication globalVariable = (MyApplication) getApplicationContext();

    // Get ArrayList from global/application context
    ArrayList<String> list = globalVariable.getValue();

and don't forget to register in Manifest.xml 并且不要忘记在Manifest.xml中注册

<application
        android:name="YOUR_PACKAGE_NAME.MyApplication "
        -----------------------

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

相关问题 Android通过多个活动发送额外的数据 - Android send extra Data through multiple Activities 无法通过意图发送url额外的方法发送url - Failed to send url through intent put extra method 将JSON数组数据放入Hashmap中,并通过Intent Extra传递 - Put JSON array data in Hashmap and pass it through Intent Extra 在父活动中使用startActivityForResult()时,如何获得通过多个活动传递的额外费用? - How to get extra that is passed through several activities when using startActivityForResult() in parent activity? 使用parcelable在活动之间传递数组列表对象 - Passing Array List Object between activities using parcelable 如何发送 ArrayList<customclass> () 通过活动</customclass> - How to send ArrayList<CustomClass>() through activities Android-检查数组是否具有此值,获取位置并发送多余的意图到下一个活动片段 - Android - Check whether the array has this value , get the position and send intent put extra to next activity fragment 如何将字节数组数据发送到其他活动? - How to send byte array data to other activities? 利用Parcelable在两个活动之间发送数组 - Utilising Parcelable to send Array across two activities 如何通过名单 <custom object> 通过活动 - How to pass List<custom object> through activities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM