2016-01-20 151 views
0

我有我使用從一個CSV文件(稱爲CSVReader)解析數據類,裏面有用於解析一些方法,以及一對夫婦的字符串和一個自定義對象類型的列表(List allRecords)。 我正在做的是當應用程序加載時,將所有數據解析到該列表中,然後嘗試將該信息傳遞給下一個活動,但在下一個活動中,我將所有記錄保留爲空。從一個Android的活動傳遞對象到另一個

LoginActivity

CSVReader data = new CSVReader(); 
    data.populateRecords(this); 
    Intent intent = new Intent(LoginActivity.this, Find.class); 
    Bundle bundle = new Bundle(); 
    bundle.putParcelable("list", data); 
    intent.putExtra("bundle", bundle); 
    startActivity(intent); 

我已經通過了調試和包在裏面絕對有數據。

查找

Intent intent = getIntent(); 
Bundle bundle = intent.getBundleExtra("bundle"); 
data = (CSVReader) bundle.getParcelable("list"); 

使用調試器仍然和mmap(中包)現在是零,所以是數據。

我做錯了什麼? DummyData和CSVReader都實現了Parcelable。

編輯:添加自定義類CSVReader:

List<DummyData> allRecords; 
    private String base; 
    private String location; 
    private String partner; 

    public static Creator<CSVReader> CREATOR = new Creator<CSVReader>(){ 
     @Override 
     public CSVReader createFromParcel(Parcel source){ 
      return new CSVReader(source); 
     } 
     @Override 
     public CSVReader[] newArray(int size) { 
      return new CSVReader[size]; 
     } 
    }; 

    private CSVReader(Parcel in){ 
      allRecords = new ArrayList<DummyData>(); 
      in.readTypedList(allRecords, DummyData.CREATOR); 
      base = in.readString(); 
      location = in.readString(); 
      partner = in.readString(); 
    } 
    ... 
    ... 

    @Override 
    public int describeContents() { 
      return 0; 
    } 
    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
      dest.writeString(base); 
      dest.writeString(location); 
      dest.writeString(partner); 
      dest.writeList(allRecords); 
    } 
+3

而不是'bundle.getParcelable(「this」);'它應該是'bundle.getParcelable(「list」);' –

+0

@DanielZolnai你是對的,但是這並沒有解決它 – John

+0

請張貼你的自定義對象類,您正在嘗試傳遞給下一個活動的類。 –

回答

1

[編輯]

我是看在你的代碼一遍,發現你寫的列表中包裹LAST並從包裹FIRST閱讀列表。您必須按照書面順序讀取包裹中的物品。請嘗試將dest.writeList(allRecords)放在方法的頂部,以便它成爲寫入的第一項,或者可以將in.readList(allRecords, DummyData.class.getClassLoader())放在該方法列表的底部。

給它一個鏡頭。

//////////////////////////////////////////////

從文檔,

public final void readTypedList (List<T> list, Creator<T> c)

讀入包含在當前 dataPosition被寫入與writeTypedList(列表),該特定對象類型 給定的列表項()。該列表必須先前通過 writeTypedList(List)使用相同的對象類型編寫。

您正在使用writeList()將數據寫入宗地。嘗試使用writeTypedList()。我相信你也可以試着改變readTypedList()readList()。例如:

in.readList(allRecords, CSVReader.class.getClassLoader()); 

希望這會有所幫助。

+0

使用http://www.parcelabler.com/生成parcelable類。 – user2260040

+0

哦好點!我甚至沒有想到這些搞砸了,但它是有道理的。我會給它一個鏡頭,看看發生了什麼事謝謝 – John

+1

沒問題:),高興地幫助。 –

0

您需要使用putExtras方法把包對象。然後你可以使用getIntent()。getExtras()來獲得包。

Intent intent = new Intent(LoginActivity.this, Find.class); 
Bundle bundle = new Bundle(); 
bundle.putParcelable("list", data); 
intent.putExtras(bundle); 
startActivity(intent); 

Intent intent = getIntent(); 
Bundle bundle = intent.getExtras(); 
data = (CSVReader) bundle.getParcelable("list"); 
1

如果您打算將數據傳遞給另一個活動,則需要使用實現Parcelable的類的對象。這裏是一個例子,

package com.weather.model; 

import java.util.ArrayList; 
import java.util.List; 

import android.os.Parcel; 
import android.os.Parcelable; 

public class Forcast implements Parcelable { 

private String id; 
private String code; 
private String message; 
private City city; 
private String count; 
private List<Weather> weatherList = new ArrayList<Weather>(); 

public Forcast(){ 
} 

public String getId() { 
    return id; 
} 
public void setId(String id) { 
    this.id = id; 
} 
public String getCode() { 
    return code; 
} 
public void setCode(String code) { 
    this.code = code; 
} 
public String getMessage() { 
    return message; 
} 
public void setMessage(String message) { 
    this.message = message; 
} 
public City getCity() { 
    return city; 
} 
public void setCity(City city) { 
    this.city = city; 
} 
public String getCount() { 
    return count; 
} 
public void setCount(String count) { 
    this.count = count; 
} 
public List<Weather> getWeatherList() { 
    return weatherList; 
} 
public void setWeatherList(List<Weather> weather) { 
    this.weatherList = weather; 
} 


@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(id); 
    dest.writeString(code); 
    dest.writeString(message); 
    dest.writeParcelable(city, flags); 
    dest.writeString(count); 
    dest.writeList(weatherList); 

} 

public static final Parcelable.Creator<Forcast> CREATOR = new Parcelable.Creator<Forcast>() { 

    @Override 
    public Forcast createFromParcel(Parcel source) { 
     return new Forcast(source); 
    } 

    @Override 
    public Forcast[] newArray(int size) { 
     return new Forcast[size]; 
    } 
}; 

protected Forcast(Parcel in){ 
    id = in.readString(); 
    code = in.readString(); 
    message = in.readString(); 
    city = (City)in.readParcelable(City.class.getClassLoader()); 
    count = in.readString(); 
    in.readList(weatherList, Weather.class.getClassLoader()); 
} 

} 

我包括城市模型,所以你可以看到它是如何去對象與對象列表。假設你有一個推算未來實例的所有值 'FORCAST'(推算未來FORCAST =新推算未來()或類似的東西)

Intent intent = new Intent(this, SomeActivity.class); 
intent.putExtra(com.weather.model,forcast) 
startActivity(intent) 

我希望幫助

+0

我的自定義類看起來非常相似,所以我不確定它爲什麼不起作用。我做了一些改變,比如將'in.readTypedList(...)'改爲'in.readList(...)',但仍然不起作用。在你的例子中,'Weather'類是怎麼樣的? – John

+0

你可以在這裏看到一切。 https://github.com/siambo/android/blob/master/WeazrAPI/src/com/weazrapi/model/Weather.java –