我已使用Parcelable
将对象传递给新活动,并将其写入writeToParcel
。我相信该对象已被转移,因为我可以使用.toString()但它没有任何相关的方法。我使用了这个链接:http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/我的代码运行,但我希望能够使用a.clubName()
或其他一些能够访问这些详细信息,或者我试图错误地访问详细信息或我还没有完全正确的设置。
感谢您的帮助。
这是我的俱乐部课程,它将详细信息与包裹相关联
public class Clubs implements Parcelable{
//lots of variables defining things such as clubName, address etc.
public static final Parcelable.Creator CREATOR = new Parcelable.Creator(){
public Clubs createFromParcel(Parcel in) {
return new Clubs(in); }
public Clubs[] newArray(int size) {
return new Clubs[size]; }
};
private void readFromParcel(Parcel in) {
clubName = in.readString();
address = in.readString();
postcode = in.readString();
contactName = in.readString();
contactPhone = in.readString();
date = in.readString();
eventType = in.readString();
scrutTime = in.readString();
startTime = in.readString();
eventName = in.readString();
week = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(clubName);
dest.writeString(address);
dest.writeString(postcode);
dest.writeString(contactName);
dest.writeString(contactPhone);
dest.writeString(date);
dest.writeString(eventType);
dest.writeString(scrutTime);
dest.writeString(startTime);
dest.writeString(eventName);
dest.writeString(week);
}
public Clubs(Parcel in) { readFromParcel(in); }
这是我的onItemClick方法,它启动应该通过
发送对象的新活动public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Clubs mymeeting = db.get(map.get(position));
Intent i = new Intent(ListSample.this, DynamicEvents.class);
i.putExtra("mymeeting", mymeeting);
startActivity(i);
}
});
这是我希望能够将详细信息写入
的课程public class DynamicEvents extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
Object a = b.getParcelable("mymeeting");
textView.setText(a.toString());
// Set the text view as the activity layout
setContentView(textView);
}
}
答案 0 :(得分:1)
您需要将parcelable转换为正确的类类型。
Parcelable parcelable = b.getParcelable("mymeeting");
if(parcelable instanceof Clubs) {
Clubs clubs = (Clubs) parcelable;
}