如何使用intent将ArrayList传递给另一个Activity?

时间:2015-12-12 09:04:23

标签: java android android-intent arraylist

活动A 中,我使用此代码传递数组

 ArrayList<SearchResults> results = new ArrayList<SearchResults>();
 Intent i=new Intent(getApplication(),B.class);
 i.putExtra("results", results);
 startActivity(i);

活动B 中,接收results

ArrayList result;
result = (ArrayList<SearchResults>)getIntent().getSerializableExtra("results");   
 Toast.makeText(getApplicationContext(),result+"",Toast.LENGTH_LONG).show();

信息搜索结果

public class SearchResults {

    private String weather = "";
    private String date = "";
    private String status = "";
    private String timeIn="";
    private String timeOut="";
    private String project="";
    private String description="";
    private String progress="";

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getWeather() {
        return weather;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setTimeIn(String timeIn) {
        this.timeIn = timeIn;
    }

    public String getTimeIn() {
        return timeIn;
    }

    public void setTimeOut(String timeOut){
       this.timeOut=timeOut;
    }

    public String getTimeOut()
    {
        return timeOut;
    }

    public void setProject(String project){
        this.project=project;
    }

    public String getProject()
    {
        return project;
    }

    public void setProgress(String progress){
        this.progress=progress;
    }

    public String getProgress()
    {
        return progress;
    }

    public void setDescription(String description){
        this.description=description;
    }

    public String getDescription()
    {
        return description;
    }

}

错误LogCat

 Process: com.example.project.myapplication, PID: 2698
    java.lang.RuntimeException: Parcel: unable to marshal value com.example.project.myapplication.bean.SearchResults@94c2757
            at android.os.Parcel.writeValue(Parcel.java:1397)
            at android.os.Parcel.writeList(Parcel.java:738)
            at android.os.Parcel.writeValue(Parcel.java:1344)

这一行

 startActivity(i);

如何使用serializable传递bean对象,并使bean类实现serializable?非常感谢。

在同一活动中,我希望将结果保存到数据库中。

 btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { 
                WD.insertWorkDetails(result,a);  //a is pass from Activtity a too
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                Toast.makeText(getApplication(),"DONE",Toast.LENGTH_LONG).show();
            }
        });

WorkDetails

 public long insertWorkDetails(ArrayList<SearchResults> listItems, long id)
    {
        database=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        for(SearchResults s:listItems) {

            String Project = s.getProject();
            String temp = s.getDescription();
            String[] ReceiveDescription = temp.split(":");
            String Progress = s.getProgress();
            String[] ReceiveProgress = Progress.split(":");
            String TimeIn = s.getTimeIn();
            String[] ReceiveTimeIn = TimeIn.split(":");
            String TimeOut = s.getTimeOut();
            String[] ReceiveTimeOut = TimeOut.split(":");
           values.put(MyDatabaseHelper.Project,Project);
            values.put(MyDatabaseHelper.WorkDescription,ReceiveDescription[1]);
            values.put(MyDatabaseHelper.Percentage,ReceiveProgress[1]);
            values.put(MyDatabaseHelper.TimeIn,ReceiveTimeIn[1]);
            values.put(MyDatabaseHelper.TimeOut,ReceiveTimeOut[1]);
            values.put("Twd_id",id);
          database.insert(MyDatabaseHelper.TABLE_WORKDETAILS, null, values);
        }
        database.close();
        return 0 ;

    }

当我检查我的数据库时,没有添加listView项。

1 个答案:

答案 0 :(得分:1)

  

java.lang.RuntimeException:Parcel:无法编组值   com.example.project.myapplication.bean.SearchResults@94c2757

您正在使用getSerializableExtra来检索ArrayList。这意味着SearchResults实现了Serializable,而实际情况并非如此。如果您想要一个简单的修复,只需让SearchResults实现Serializable接口。在SearchResults的情况下,序列化将开箱即用。或者您可以了解Parcelable(详细了解here)并使用它代替Serializable。如果您不需要将对象保存在永久存储空间中,Parcelable的效率会高于Serializable