dimanche 19 avril 2015

getParcelableArrayList returns empty list

I have an List of a custom class to be transferred from my MainActivity to showShopActivity. When I use getParcelableArrayList, it always 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");
}

}


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

}

Aucun commentaire:

Enregistrer un commentaire