我尝试使用intent和putExtras()使用此代码将自定义类从活动传递到另一个活动:
public class MyFirstActivity extends ListActivity {
String selectedItem = "selectedItem";
String[] allItemNames;
CustomClass[] allItems = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_all_items);
allItems = createItemsMethod(); //IsTooLongToPost
CustomClass item = new CustomClass();
String[] allItemNames = getAllTheNamesMethod(); //IsTooLongToPost
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.show_item_in_row, R.id.textview_item_row, allItemNames);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
super.onListItemClick(l, v, position, id);
String value = (String) getListView().getItemAtPosition(position);
Intent intent = new Intent(this, MySecondActivity.class);
Item currentItem = allItems[position];
Bundle b = new Bundle();
b.putParcelable("Key", currentItem);
intent.putExtras(b);
startActivity(intent);
}}
这个意图中的值是可以的,但是当我尝试将它们带入我的其他活动时,意图为空并且Extras不在那里,我的其他活动有这个代码:
public class MySecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_activity);
}
protected void onStart() {
super.onStart();
Intent i = getIntent();
Bundle b = getIntent().getExtras();
if(b!=null){
CustomClass item = b.getParcelable("Key");
}
}}
这是CustomClass
public class CustomClass implements Parcelable{
String name;
int id;
String description;
LinkedList<CustomClass> related;
String[] itemList;
// Other costructors and getters
public CustomClass(Parcel parcel) {
name = parcel.readString();
id = parcel.readInt();
description = parcel.readString();
related = null;
}
// Parcel Part
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeInt(id);
parcel.writeString(description);
parcel.writeTypedList(related);
}
public static Creator<CustomClass> CREATOR = new Creator<CustomClass>() {
public CustomClass createFromParcel(Parcel parcel) {
return new CustomClass(parcel);
}
public CustomClass[] newArray(int size) {
return new CustomClass[size];
}
};}
任何想法为什么会发生这种情况?感谢
答案 0 :(得分:0)
您需要在类Anime中实现Parcelable并添加未实现的方法 请点击此链接了解如何实施它 https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/account/Account.java
答案 1 :(得分:0)
您可以将对象从一个活动传递到另一个活动,根据我的知识,您可以使用两种方法。 一个是使你的Bean(类) Parcelable ,其他简单的方法是使它 Serializable 。
使用Serializable 假设您要传递此类的对象
public class CustomClass implements Serializable{
//put your class member variable here and it's getters and setters
}
然后从你的第一个活动开始,
Intent intent = new Intent(this, MySecondActivity.class);
//get the object of CustomClass which you want to pass
// let's say it's name is **customClassObject**
//Now pass it to the second activity
intent.putExtra(Intent.EXTRA_TEXT,clickedMenuItemBean);
startActivity(intent);
现在进入第二个Activity.获取Custom类的pass对象 意图。
CustomClass cc = (CustomClass) intent.getSerializableExtra(Intent.EXTRA_TEXT);