简体   繁体   English

将对象从一个android活动传递到另一个

[英]Passing an object from one android activity to another

I have a class that I'm using for parsing data from a csv file (called CSVReader), inside of that there is some methods for the parsing as well as a couple of strings and a list that is of a custom object type (List allRecords). 我有一个用于解析csv文件中数据的类(称为CSVReader),其中有一些解析方法以及一些字符串和一个自定义对象类型的列表(列表allRecords)。 What I'm doing is when the app loads, parsing all the data into that list and then trying to pass that information along to the next activity but inside the next activity I keep getting allRecords as being null. 我正在做的是加载应用程序时,将所有数据解析到该列表中,然后尝试将该信息传递给下一个活动,但是在下一个活动中,我一直将allRecords设置为null。

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);

I've gone through the debugger and the bundle definitely has the data in there. 我已经通过了调试器,并且捆绑包中肯定有数据。

Find

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

Using the debugger still, and mMap (in the bundle) is now null and so is data. 仍在使用调试器,并且mMap(在捆绑包中)现在为null,数据也为null。

Am I doing something wrong? 难道我做错了什么? Both classes DummyData and CSVReader implement Parcelable. DummyData和CSVReader这两个类都实现了Parcelable。

EDIT: Adding custom class CSVReader: 编辑:添加自定义类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);
     }

If you plan to pass data to another activity, you need to use objects of classes that implement Parcelable. 如果计划将数据传递到另一个活动,则需要使用实现Parcelable的类的对象。 Here is an example, 这是一个例子

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());
}

}

I included City model so you can see how it goes for an object in comparison to list of objects. 我包括了City模型,因此您可以比较对象列表与对象列表的关系。 Assume you have a Forcast instance with all the values as 'forcast' ( Forcast forcast = new Forcast() or something similar) 假设您有一个Forcast实例,其所有值都为“ forcast”(Forcast forcast = new Forcast()或类似名称)

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

I hope that helps 希望对您有所帮助

[EDIT] [编辑]

I was looking over your code again, and noticed you're writing the List to the parcel LAST and reading the list from the parcel FIRST . 我再次查看了您的代码,发现您正在将List写入地块LAST并从地块FIRST中读取该列表。 You have to read items from the parcel in the order they are written. 您必须按照包裹的顺序读取包裹中的物品。 Try putting dest.writeList(allRecords) at the top of the method so it is the first item written, or you can put in.readList(allRecords, DummyData.class.getClassLoader()) at the bottom of the list in that method. 尝试将dest.writeList(allRecords)放在该方法的顶部,这样它是第一个写入的项目,或者您可以将in.readList(allRecords, DummyData.class.getClassLoader())放在该方法的列表底部。

Give it a shot. 试一试。

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

From the docs, 从文档中

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

Read into the given List items containing a particular object type that were written with writeTypedList(List) at the current dataPosition(). 读入给定的List项,这些项包含在当前dataPosition()处使用writeTypedList(List)写入的特定对象类型。 The list must have previously been written via writeTypedList(List) with the same object type. 该列表必须事先通过具有相同对象类型的writeTypedList(List)写入。

You're using writeList() to write the data to the parcel. 您正在使用writeList()将数据写入包裹。 Try using writeTypedList() . 尝试使用writeTypedList() You could also try changing readTypedList() with readList() I believe. 您也可以尝试使用readList()更改readTypedList() ,我相信。 Something like: 就像是:

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

Hope this helps. 希望这可以帮助。

You need to put bundle object by using putExtras method. 您需要使用putExtras方法放置捆绑对象。 Then you can get the bundle by using getIntent().getExtras(). 然后,您可以使用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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM