How to pass data between two activities
I have a NetworkList
class which has a arraylist
of type string
. I am using parcelable interface in it.but i am getting only the first element of arraylist
in my new activity.
how can i get all the elements?
here is NetworkList class
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 crea开发者_开发技巧teFromParcel(Parcel in) {
return new NetworkList(in);
}
public NetworkList[] newArray(int size) {
return new NetworkList[size];
}
};
}
here is how where i pass it
Intent(AllNetworkActivity.this,WorkActivity.class);
intent.putExtra("name",network);
startActivity(intent);
}});
}
here is how i retrieve it
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));
hope u are able to help me know
You should also look into Application class in Android. You can define global variables in there which can be accessed by all activities. However Bundle is the most popular and right method for sending data between activities.
Try using appropriate methods to serialize 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);
}
first
Intent Jan = new Intent(Months.this,Holiday.class);
Jan.putExtra("ListCount", "Jan");
startActivity(Jan);
then
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 I wrote. Hope this will help you.
精彩评论