getParcelableArrayList returns 空列表
getParcelableArrayList returns empty list
我有一个 List
的定制 class 从我的 MainActivity
转移到 showShopActivity
。当我使用 getParcelableArrayList
时,它总是 returns null。
MainActivity
public class MainActivity extends Activity implements FetchDataListener{
private ProgressDialog dialog;
private ListView lv_nearestShops;
@Override
protected void onCreate(Bundle savedInstanceState) {
}
@Override
public void onFetchComplete(final List<ListData> data) {
// dismiss the progress dialog
// create new adapter
lv_nearestShop_adapter adapter = new lv_nearestShop_adapter(MainActivity.this, data);
// set the adapter to list
lv_nearestShops.setAdapter(adapter);
lv_nearestShops.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListData test = data.get(position);
Intent intent = new Intent(MainActivity.this, showShopActivity.class);
intent.putParcelableArrayListExtra("test", test);
}
});
}
@Override
public void onFetchFailure(String msg) {
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
showShopActivity
public class showShopActivity extends Activity {
ArrayList<ListData> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_shop);
Bundle bundle = getIntent().getExtras();
dataList = bundle.getParcelableArrayList("test");
}
}
列表数据
public class ListData extends ArrayList<Parcelable> implements Parcelable{
private String name;
private String street;
private double latitude;
private double longitude;
private int distance;
private String feature_image;
private String city;
private String postcode;
public ListData() {}
public ListData(String name, String street, double latitude, double longitude, int distance, String feature_image, String city, String postcode) {
super();
this.name = name;
this.street = street;
this.latitude = latitude;
this.longitude = longitude;
this.distance = distance;
this.feature_image = feature_image;
this.city = city;
this.postcode = postcode;
}
public ListData(Parcel in){
this.name = in.readString();
this.street = in.readString();
this.latitude = in.readDouble();
this.longitude = in.readDouble();
this.distance = in.readInt();
this.feature_image = in.readString();
this.city = in.readString();
this.postcode = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(street);
dest.writeDouble(latitude);
dest.writeDouble(longitude);
dest.writeInt(distance);
dest.writeString(feature_image);
dest.writeString(city);
dest.writeString(postcode);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public String getFeature_image() {
return feature_image;
}
public void setFeature_image(String feature_image) {
this.feature_image = feature_image;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
public ListData createFromParcel(Parcel in) {
return new ListData(in);
}
public ListData[] newArray(int size) {
return new ListData[size];
}
};
}
开发者网站说:
public Intent putParcelableArrayListExtra (String name, ArrayList<? extends Parcelable> value)
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Parameters
name The name of the extra data, with package prefix.
value The ArrayList data value.
另请检查您将此 Intent 传递到哪里,我在 OnItemClick 中看不到任何 startActivity 代码。
我有一个 List
的定制 class 从我的 MainActivity
转移到 showShopActivity
。当我使用 getParcelableArrayList
时,它总是 returns null。
MainActivity
public class MainActivity extends Activity implements FetchDataListener{
private ProgressDialog dialog;
private ListView lv_nearestShops;
@Override
protected void onCreate(Bundle savedInstanceState) {
}
@Override
public void onFetchComplete(final List<ListData> data) {
// dismiss the progress dialog
// create new adapter
lv_nearestShop_adapter adapter = new lv_nearestShop_adapter(MainActivity.this, data);
// set the adapter to list
lv_nearestShops.setAdapter(adapter);
lv_nearestShops.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListData test = data.get(position);
Intent intent = new Intent(MainActivity.this, showShopActivity.class);
intent.putParcelableArrayListExtra("test", test);
}
});
}
@Override
public void onFetchFailure(String msg) {
if(dialog != null) dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
showShopActivity
public class showShopActivity extends Activity {
ArrayList<ListData> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_shop);
Bundle bundle = getIntent().getExtras();
dataList = bundle.getParcelableArrayList("test");
}
}
列表数据
public class ListData extends ArrayList<Parcelable> implements Parcelable{
private String name;
private String street;
private double latitude;
private double longitude;
private int distance;
private String feature_image;
private String city;
private String postcode;
public ListData() {}
public ListData(String name, String street, double latitude, double longitude, int distance, String feature_image, String city, String postcode) {
super();
this.name = name;
this.street = street;
this.latitude = latitude;
this.longitude = longitude;
this.distance = distance;
this.feature_image = feature_image;
this.city = city;
this.postcode = postcode;
}
public ListData(Parcel in){
this.name = in.readString();
this.street = in.readString();
this.latitude = in.readDouble();
this.longitude = in.readDouble();
this.distance = in.readInt();
this.feature_image = in.readString();
this.city = in.readString();
this.postcode = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(street);
dest.writeDouble(latitude);
dest.writeDouble(longitude);
dest.writeInt(distance);
dest.writeString(feature_image);
dest.writeString(city);
dest.writeString(postcode);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public String getFeature_image() {
return feature_image;
}
public void setFeature_image(String feature_image) {
this.feature_image = feature_image;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostcode() {
return postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
public ListData createFromParcel(Parcel in) {
return new ListData(in);
}
public ListData[] newArray(int size) {
return new ListData[size];
}
};
}
开发者网站说:
public Intent putParcelableArrayListExtra (String name, ArrayList<? extends Parcelable> value)
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Parameters
name The name of the extra data, with package prefix.
value The ArrayList data value.
另请检查您将此 Intent 传递到哪里,我在 OnItemClick 中看不到任何 startActivity 代码。