Reputation: 43
im newbie on android. I need to send data from ListView with customAdapter to detailActivity that show detail's item from selected item of listView. I have seen a few post about it and i still confuse about it.
Would you help me?
This is my data class :
public class ListData {
private String title;
private String desc;
public ListData(String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
}
This is my arrayList declaration :
ArrayList<ListData> data = new ArrayList<ListData>();
data.add(new ListData("Sidik Suhendar", "Telkom University"));
I don't have idea, how i can send my arraylist data to another activity. Please help me :) give me some example too :) Thanks
Upvotes: 2
Views: 3631
Reputation: 12005
You need to make your base object, ListData parcelable:
public class ListData implements Parcelable {
private String title;
private String desc;
public ListData(String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
@Override
public int describeContents() {
return 0;
}
public ListData(Parcel source) {
title = source.readString();
desc = source.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(desc);
}
public static final Creator<ListData> CREATOR = new Creator<ListData>(){
@Override
public ListData createFromParcel(Parcel source) {
return new ListData(source);
}
@Override
public ListData[] newArray(int size) {
return new ListData[size];
}
};
}
Once it is parcelable you can simply do in the first activity:
ArrayList<ListData> data = new ArrayList<ListData>();
data.add(new ListData("Sidik Suhendar", "Telkom University"));
intent.putParcelableArrayListExtra("ARRAY_LIST", data);
And you can read it in the other activity as:
ArrayList<ListData> data = (ArrayList<ListData>) getIntent().getExtras().getParcelableArrayList("ARRAY_LIST");
EDIT:
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?>adapter,View v, int position){
ListData item = adapter.getItemAtPosition(position);
Intent intent = new Intent(Activity.this,DetailActivity.class);
intent.putExtra("ARRAY_ITEM", item);
startActivity(intent);
}
});
Then in the destination activity:
ListData data = (ListData) getIntent().getExtras().getParcelable("ARRAY_ITEM");
Upvotes: 3
Reputation: 1
First step: ListData implements Parcelable:
public class ListData implements Parcelable {
private String title;
private String desc;
public ListData(String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.title);
dest.writeString(this.desc);
}
protected ListData(Parcel in) {
this.title = in.readString();
this.desc = in.readString();
}
public static final Parcelable.Creator<ListData> CREATOR = new Parcelable.Creator<ListData>() {
@Override
public ListData createFromParcel(Parcel source) {
return new ListData(source);
}
@Override
public ListData[] newArray(int size) {
return new ListData[size];
}
};
}
Second step:send data:
ArrayList<ListData> data = new ArrayList<ListData>();
data.add(new ListData("Sidik Suhendar", "Telkom University"));
Intent intent = new Intent(this, TargetActivity.class);
intent.putParcelableArrayListExtra("data",data);
Third step:receive data
ArrayList<ListData> data = intent.getParcelableArrayListExtra("data");
Upvotes: 0