如何将ArrayList的NameValuePair对象传递给另一个Activity?

时间:2015-02-03 06:13:01

标签: android

我试图将NameValuePair的arrylist一个活动传递给另一个活动,我搜索了这么多,我发现了类似的问题但是我无法得到答案,我试过但我得到了例外 ,请有人告诉我如何通过

    ArrayList<NameValuePair> namevlaList= new ArrayList<NameValuePair>();
    namevlaList.add(new BasicNameValuePair("formal_name",mLegalName));
    namevlaList.add(new BasicNameValuePair("device_phone_no",mPhno));
    namevlaList.add(new BasicNameValuePair("email",mEmail));
    namevlaList.add(new BasicNameValuePair("contact_name",mContactName));
    namevlaList.add(new            
    BasicNameValuePair("contact_number",mContactNumber));
    namevlaList.add(new BasicNameValuePair("password",mPwd));  
    Intent intent = new 
    Intent(this,Second.class);
    intent.putExtra("extra", namevlaList);
    startActivity(intent);

2 个答案:

答案 0 :(得分:0)

您可以使用Bundle

传递
Intent intent = new Intent(this,Second.class);
Bundle extra = new Bundle();
extra.putSerializable("extra",  namevlaList.toArray());// cast to array and put
intent .putExtras(extra);
startActivity(intent);

和第二项活动

Intent i = getIntent();
Bundle extras = i.getExtras();
List<NameValuePair> Namevaluepairvariable = new ArrayList<NameValuePair>();
Namevaluepairvariable =  (List<NameValuePair>) extras.getSerializable("extra");

使用implements Serializable进行第二次活动

答案 1 :(得分:0)

你可以这样做,希望有所帮助

Intent intent = new Intent(this,Second.class);
intent.putExtra("namevlaList", namevlaList);
startActivity(intent);

在下一个活动中使用此

// initialize array list in onCreate
namevlaList= (ArrayList) getIntent().getExtra().get("namevlaList");

另见link

这是因为BasicNameValuePair不可序列化,请参阅this希望有帮助