在活动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();
}
});
工作细节
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项目。
最佳答案
java.lang.RuntimeException:包裹:无法封送值
com.example.project.myapplication.bean.SearchResults@94c2757
您正在使用getSerializableExtra
检索您的ArrayList
。这意味着SearchResults
实现了Serializable
,事实并非如此。如果您想轻松修复,请让SearchResults
实现Serializable接口。对于SearchResults,序列化将立即可用。或者,您可以了解Parcelable
(了解有关here的更多信息)并使用它代替Serializable
。如果您不需要将对象保存到永久存储中,则Parcelable
比Serializable
效率更高
关于java - 如何使用 Intent 将ArrayList传递给另一个Activity?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34238121/