getParcelableArrayList返回空列表

时间:2015-04-19 11:30:54

标签: java android list arraylist parcelable

我有List个自定义类要从MainActivity转移到showShopActivity。当我使用getParcelableArrayList时,它总是返回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");
    }

}

的ListData

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];
        }
    };

}

1 个答案:

答案 0 :(得分:0)

开发者网站说:

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.

另外请检查您传递此意图的位置,我在OnItemClick中看不到任何startActivity代码。