gpt4 book ai didi

java - getParcelableArrayList 返回空列表

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:56 25 4
gpt4 key购买 nike

我有一个自定义类的List,要从我的MainActivity 传输到showShopActivity。当我使用 getParcelableArrayList 时,它总是返回 null。

主要 Activity

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 代码。

关于java - getParcelableArrayList 返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com