[英]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.