我正在尝试制作一个从JSON提取数据并使用它在Map上显示位置的应用。当将List直接加载到包含Map Fragment的活动中时,Map机制工作正常。现在,当尝试在另一个活动中加载列表,然后尝试通过意图发送列表时,我无法显示数据,我认为这与意图有关。
这是Intent发送部分
if (data != null && !data.isEmpty()) {
List<AirQuality> receivedData= (List<AirQuality>) data;
Intent intent = new Intent(AirQualityActivity.this, MapsActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("MYLIST",(Serializable) data);
intent.putExtras(bundle);
startActivity(intent);
}
这是Intent接收部分
Intent i = getIntent();
Bundle b = i.getExtras();
List<AirQuality> receivedDataList = (List<AirQuality>) b.getSerializable("MYLIST");
这是实现Serializable的自定义对象类
import java.io.Serializable;
public class AirQuality implements Serializable{
private String mId;
private String mStation;
private String mState;
private String mCity;
private String mPollutantID;
private String mPollutantAvg;
private String mPollutantMin;
private String mPollutantMax;
/*Default Constructor*/
public AirQuality(String id, String station, String state, String city, String pollutantID, String pollutantAvg ,String pollutantMin, String pollutantMax) {
mId = id;
mStation = station;
mState = state;
mCity = city;
mPollutantID = pollutantID;
mPollutantAvg = pollutantAvg;
mPollutantMin = pollutantMin;
mPollutantMax = pollutantMax;
}
//Returns Item ID
public String getId(){
return mId;
}
//Returns Station Name
public String getStation() {
return mStation;
}
//Returns State Name
public String getState() {
return mState;
}
//Returns City Name
public String getCity(){
return mCity;
}
//Returns Pollutant ID
public String getPollutantID() {
return mPollutantID;
}
//Return Pollutant Average value
public String getPollutantAvg() {
return mPollutantAvg;
}
//Returns Pollutant Minimum value
public String getPollutantMin(){
return mPollutantMin;
}
//Returns Pollutant Maximum value
public String getPollutantMax(){
return mPollutantMax;
}
}
任何帮助将不胜感激。预先感谢。
答案 0 :(得分:0)
首先,我建议实现为 Parcelable ,而不是Serializable。在速度,反射等方面具有以下特点...
回答您的问题:
您说过“现在,当尝试在其他活动中加载列表,然后尝试通过意图发送列表时,我将无法显示数据” 我编译了您的代码并成功运行。它将列表从firstActivity发送到secondActivity,然后对第二个列表进行了检查。
答案 1 :(得分:0)
尝试一下:
空气质量
public class AirQuality implements Parcelable {
private String mId;
private String mStation;
private String mState;
private String mCity;
private String mPollutantID;
private String mPollutantAvg;
private String mPollutantMin;
private String mPollutantMax;
public AirQuality() {
}
public AirQuality(String mId, String mStation, String mState, String mCity, String mPollutantID, String mPollutantAvg, String mPollutantMin, String mPollutantMax) {
this.mId = mId;
this.mStation = mStation;
this.mState = mState;
this.mCity = mCity;
this.mPollutantID = mPollutantID;
this.mPollutantAvg = mPollutantAvg;
this.mPollutantMin = mPollutantMin;
this.mPollutantMax = mPollutantMax;
}
protected AirQuality(Parcel in) {
mId = in.readString();
mStation = in.readString();
mState = in.readString();
mCity = in.readString();
mPollutantID = in.readString();
mPollutantAvg = in.readString();
mPollutantMin = in.readString();
mPollutantMax = in.readString();
}
public static final Creator<AirQuality> CREATOR = new Creator<AirQuality>() {
@Override
public AirQuality createFromParcel(Parcel in) {
return new AirQuality(in);
}
@Override
public AirQuality[] newArray(int size) {
return new AirQuality[size];
}
};
public String getmId() {
return mId;
}
public void setmId(String mId) {
this.mId = mId;
}
public String getmStation() {
return mStation;
}
public void setmStation(String mStation) {
this.mStation = mStation;
}
public String getmState() {
return mState;
}
public void setmState(String mState) {
this.mState = mState;
}
public String getmCity() {
return mCity;
}
public void setmCity(String mCity) {
this.mCity = mCity;
}
public String getmPollutantID() {
return mPollutantID;
}
public void setmPollutantID(String mPollutantID) {
this.mPollutantID = mPollutantID;
}
public String getmPollutantAvg() {
return mPollutantAvg;
}
public void setmPollutantAvg(String mPollutantAvg) {
this.mPollutantAvg = mPollutantAvg;
}
public String getmPollutantMin() {
return mPollutantMin;
}
public void setmPollutantMin(String mPollutantMin) {
this.mPollutantMin = mPollutantMin;
}
public String getmPollutantMax() {
return mPollutantMax;
}
public void setmPollutantMax(String mPollutantMax) {
this.mPollutantMax = mPollutantMax;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mId);
parcel.writeString(mStation);
parcel.writeString(mState);
parcel.writeString(mCity);
parcel.writeString(mPollutantID);
parcel.writeString(mPollutantAvg);
parcel.writeString(mPollutantMin);
parcel.writeString(mPollutantMax);
}
}
意图发送部分
bundle.putParcelableArrayList("MYLIST", data);
意向接收部分
List<AirQuality> receivedDataList = bundle.getParcelableArrayList("MYLIST");