我有三个活动,第一个叫做摆单,我在其中创建了一个数组列表,然后向其中添加字符串如下:
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++;
}我把它打印在logcat上,它正确地显示:
Log.e("product list",prodlist+""); 我已经把它从第一个活动发送到第三个活动,,虽然,我应该通过第二个活动,然后再进入第三个活动:
Intent intent = new Intent(PenddingOrders.this, ProductInfoDetails.class);
intent.putStringArrayListExtra("prodlist", prodlist);
startActivity(intent);在第三个活动中,我通过以下代码获得数组列表:
try{
prodlist = getIntent().getStringArrayListExtra("prodlist");
}
catch(Exception e){
e.printStackTrace();
}
Log.e("prodlist",prodlist+"");但在logcat中我得到了空值..。那么我做错了什么请帮忙??
发布于 2014-04-21 10:13:39
您可以使用getter setter设置属性,以便很好地管理您的值。让它变得简单易懂。因此,您可以将它的Extras ArrayList用于下一个活动。
发布于 2014-04-21 10:14:03
这是我在堆栈溢出上的ans,请检查一下,希望它能帮上忙:
发布于 2014-04-21 10:23:52
最好使用Application类来持久化数据。供参考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;
}
}步骤-1首先设置活动的值。
MyApplication globalVariable = (MyApplication) getApplicationContext();
globalVariable.setValue(prodlist)// Set your ArraList that you want to store步骤2检索其他活动中的值,如
MyApplication globalVariable = (MyApplication) getApplicationContext();
// Get ArrayList from global/application context
ArrayList<String> list = globalVariable.getValue();和不要忘记在Manifest.xml注册
<application
android:name="YOUR_PACKAGE_NAME.MyApplication "
-----------------------https://stackoverflow.com/questions/23195174
复制相似问题