2011-06-26 47 views
0

我有一個NetworkList類,它有一個arraylist類型string。我正在使用parcelable接口,但在我的新活動中,我只獲得了arraylist的第一個元素。 我如何獲得所有元素? 這裏是NetworkList類如何在兩個活動之間傳遞數據

public class NetworkList implements Parcelable{ 
private ArrayList<String> name=new ArrayList<String>(); 
private ArrayList<String> id=new ArrayList<String>(); 



@SuppressWarnings("unchecked") 
public NetworkList(Parcel in) { 
    // TODO Auto-generated constructor stub 
    name=in.readArrayList(null); 
    id=in.readArrayList(null); 
} 



public NetworkList() { 
    // TODO Auto-generated constructor stub 
} 



public void setName(String tempValue) { 
    // TODO Auto-generated method stub 
    this.name.add(tempValue); 
} 



public void setid(String tempValue) { 
    // TODO Auto-generated method stub 
    this.id.add(tempValue); 
} 



@Override 
public int describeContents() { 
    // TODO Auto-generated method stub 
    return 0; 
} 



@Override 
public void writeToParcel(Parcel dest, int flags) { 
    // TODO Auto-generated method stub 
    dest.writeList(name); 
    dest.writeList(id); 
} 



public ArrayList<String> getname() { 
    return name; 
} 

public static final Creator<NetworkList> CREATOR = new Parcelable.Creator<NetworkList>() { 
    public NetworkList createFromParcel(Parcel in) { 
     return new NetworkList(in); 
    } 

    public NetworkList[] newArray(int size) { 
     return new NetworkList[size]; 
    } 

}; 
} 

這裏是如何在這裏我將它傳遞

Intent(AllNetworkActivity.this,WorkActivity.class); 
      intent.putExtra("name",network); 
      startActivity(intent); 
    }}); 



} 

這裏,我怎麼找回它

Bundle extras=getIntent().getExtras(); 
    NetworkList network=(NetworkList)extras.getParcelable("name"); 
    String name[]=new String[network.getname().size()]; 
    for(int i=0;i<network.getname().size();i++){ 
     name[i]=network.getname().get(i); 
    } 
    setListAdapter(new ArrayAdapter<String>(this,R.layout.work,name)); 

希望你能幫助我知道

+1

你能告訴我們一些代碼嗎? –

+0

我已經發布代碼..我剛剛得到arraylist的第一個元素 – Arihant

回答

0

您還應該看看Android中的Application類。您可以在其中定義可供所有活動訪問的全局變量。然而Bundle是在活動之間發送數據的最流行和正確的方法。

0

嘗試使用適當的方法來序列化List<String>

public NetworkList(Parcel in) { 
    in.readStringList(name); 
    in.readStringList(id); 
} 


public void writeToParcel(Parcel dest, int flags) { 
    // log the number of elements - check that this is actually what you expect it to be 
    // use only during development 
    Log.i("NetworkList"," number of elements = "+name.size()) 

    dest.writeStringList(name); 
    dest.writeStringList(id); 
} 
+0

不,它仍然沒有給出所需的結果 – Arihant

+1

您在將它們寫入包裹之前是否檢查了元素的數量? Log.i()'行? –

+0

其實我不知道如何檢查。謝謝你的回答 – Arihant

0

第一

Intent Jan = new Intent(Months.this,Holiday.class); 
        Jan.putExtra("ListCount", "Jan"); 
        startActivity(Jan); 

然後

Bundle extras = getIntent().getExtras(); 
     String data = extras.getString("ListCount"); 

     if(data.equals("Jan")){ 
      TextView txtMonth = (TextView)findViewById(R.id.txtMonth); 
      txtMonth.setText("January"); 

      TextView txtDetail = (TextView)findViewById(R.id.txtDetail); 
      txtDetail.setText(R.string.JanHol); 
     } 

check this post我寫的。希望這會幫助你。

相關問題