我有一个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));
希望你能帮助我知道
答案 0 :(得分: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);
}
答案 1 :(得分: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我写道。希望这会帮助你。
答案 2 :(得分:0)
您还应该在Android中查看Application类。您可以在其中定义可由所有活动访问的全局变量。但是Bundle是在活动之间发送数据的最流行和最正确的方法。