我有一个名为Boy的类,它有一个类型为ArrayList Question>
的实例变量。另一方面,问题有5个类型为string的实例变量。当我打开Arraylist Question>
时,我尝试读取一个问题的字符串值,而不是实际值,我得到com.example.test.Question@418a39e0
或类似的文本取决于我选择的问题。
为什么会这样呢?
这是班男生
public class Boy implements Parcelable{
ArrayList<Question> questionList;
public Boy(){
questionList = new ArrayList<Question>();
Question ques1 = new Question();
Question ques2 = new Question();
questionList.add(ques1);
questionList.add(ques2);
}
@SuppressWarnings("unchecked")
public Boy(Parcel in) {
questionList = in.readArrayList(Question.class.getClassLoader());
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(questionList);
}
public static final Parcelable.Creator<Boy> CREATOR
= new Parcelable.Creator<Boy>() {
public Boy createFromParcel(Parcel in) {
return new Boy(in);
}
public Boy[] newArray(int size) {
return new Boy[size];
}
};
}
这是课程问题
public class Question implements Parcelable{
public String ques = "how do you create a parcelable?";
public Question(){
}
public Question(Parcel in) {
ques = in.readString();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(ques);
}
public static final Parcelable.Creator<Question> CREATOR
= new Parcelable.Creator<Question>() {
public Question createFromParcel(Parcel in) {
return new Question(in);
}
public Question[] newArray(int size) {
return new Question[size];
}
};
}
此代码显示控制第二个活动的类。我正在尝试打开arrayList并阅读其中一个问题的内容,而不是我得到的实际文本com.example.test.Question@418a39e0
public class SecondActivity extends Activity{
TextView tv1;
TextView tv2;
String ques1;
String ques2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Boy b = (Boy)getIntent().getExtras().getParcelable("marcus");
ques1 = b.questionList.get(0).toString();
ques2 = b.questionList.get(1).toString();
tv1 = (TextView)findViewById(R.id.textView1);
tv2= (TextView)findViewById(R.id.textView2);
tv1.setText(ques1);
tv2.setText(ques2);
}
}
答案 0 :(得分:0)
您必须覆盖Question类中的toString()以打印出您需要的内容。