如何将List <Question> quesList
之类的列表传递给Android中的其他活动。
问题类似于
public class Question
{
private int Question_id;
private String Question;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String Answer;
private int Category_id;
private String YourAnswer;
//getter and setter methods
}
答案 0 :(得分:2)
您的问题应该实现Parcelable界面,然后您可以通过意图及其[putParcelableArrayListExtra方法] [2]
来放置您的数据Intent intentToLaunchMyNewActivity = new Intent(getApplicationContext(), NewActivity.class)
.putParcelableArrayListExtra("myList", myList);
startActivity(intentToLaunchMyNewActivity );
[2]:http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra(java.lang.String,java.util.ArrayList)
答案 1 :(得分:0)
您需要为问题类实现parcelable接口:
注意:我使用在线工具创建了此模板。你也可以尝试一下。到这里 http://parcelabler.com/
public class Question implements Parcelable {
private int Question_id;
private String Question;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String Answer;
private int Category_id;
private String YourAnswer;
//getter and setter methods
protected Question(Parcel in) {
Question_id = in.readInt();
Question = in.readString();
OPTA = in.readString();
OPTB = in.readString();
OPTC = in.readString();
OPTD = in.readString();
Answer = in.readString();
Category_id = in.readInt();
YourAnswer = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(Question_id);
dest.writeString(Question);
dest.writeString(OPTA);
dest.writeString(OPTB);
dest.writeString(OPTC);
dest.writeString(OPTD);
dest.writeString(Answer);
dest.writeInt(Category_id);
dest.writeString(YourAnswer);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
@Override
public Question createFromParcel(Parcel in) {
return new Question(in);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
}
答案 2 :(得分:0)
使用该示例
ArrayList<String> list= new ArrayList<String>();
serverIntent=new Intent(getApplicationContext(),add_costomer_meter_info.class);
serverIntent.putExtra("list", list);
startActivity(serverIntent);
和add_costomer_meter_info.java
ArrayList<String> list= new ArrayList<String>();
Intent intent=getIntent();
list= (HashMap<String, String>) intent.getSerializableExtra("list");
答案 3 :(得分:0)
您可以通过 Parcelable 和可序列化
来实现使用Serializable实现它有几个步骤。
Step1:创建您的类并实现Serializable接口。如果没有实现Serializable接口,则无法执行这些步骤。
@SuppressWarnings("serial")
public class MyClass implements Serializable {
}
第2步:当你要去 startActivity(意图)时,在第一个活动中应用此代码;
ArrayList<MyClass> list = new ArrayList<MainActivity.MyClass>();
list.add(new MyClass());
Intent intent = new Intent();
intent.putExtra("MyClassList", list);
Step3:在第二个活动中应用此代码,您将获得arrayList。
ArrayList<MyClass> list1 = (ArrayList<MyClass>)getIntent().getSerializableExtra("MyClassList");
System.out.println(list1);
希望它会对你有所帮助.. :)