Android 编程新手。我有 2 个必须可打包的对象,称为 Book 和 Author。我相当确定我准备得正确。我希望得到一些帮助:
- 创建一本书
将他们发送到新 Activity
public class Book implements Parcelable { public int id; public String title; public ArrayList<Author> authors = new ArrayList<Author>(); public int Aflags; public String isbn; public String price; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel out, int flags) { out.writeInt(id); out.writeString(title); out.writeTypedList(authors); out.writeString(isbn); out.writeString(price); } private Book(Parcel in) { id = in.readInt(); title = in.readString(); in.readTypedList(authors, Author.CREATOR); isbn = in.readString(); price = in.readString(); } public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() { public Book createFromParcel(Parcel in) { return new Book(in); } public Book[] newArray(int size) { return new Book[size]; } }; @Override public String toString() { return title + " Price: " + price; } }
现在作者:
public class Author implements Parcelable { // TODO Modify this to implement the Parcelable interface. // I think I was able to accomplish this // NOTE: middleInitial may be NULL! public String firstName; public String middleInitial; public String lastName; @Override public void writeToParcel(Parcel out, int flags) { out.writeString(firstName); if (middleInitial.length() == 0) out.writeString(middleInitial); out.writeString(lastName); } private Author(Parcel in) { firstName = in.readString(); if (in.dataSize() > 2) middleInitial = in.readString(); lastName = in.readString(); } public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() { public Author createFromParcel(Parcel in) { return new Author(in); } public Author[] newArray(int size) { return new Author[size]; } }; @Override public int describeContents() { return 0; } }
我不知道为什么我被难住了,但我不知道如何实现我的 Parcel 来制作这本书并走得更远。这是尝试制作书籍然后将其发送到另一个 Activity 的 Activity :
public class AddBookActivity extends Activity {
// Use this as the key to return the book details as a Parcelable extra in the result intent.
public static final String BOOK_RESULT_KEY = "book_result";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_book);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
// COMPLETED TODO provider SEARCH and CANCEL options
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
// TODO
switch (item.getItemId()) {
case R.id.search:
// SEARCH: return the book details to the BookStore activity
this.searchBook();
return true;
case R.id.cancel:
new AlertDialog.Builder(this)
.setTitle("Cancel Search")
.setMessage("Are you sure you want to cancel your search?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AddBookActivity.this.finish();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_dialog_alert)
.show();
// CANCEL: cancel the search request
return true;
}
return false;
}
public Book searchBook(){
/*
* Book object with the search criteria and return that.
*/
EditText editText = (EditText) findViewById(R.id.search_title);
String title = editText.getText().toString();
editText = (EditText) findViewById(R.id.search_author);
String author = editText.getText().toString();
editText = (EditText) findViewById(R.id.search_isbn);
int isbn = Integer.parseInt(editText.getText().toString());
Parcel p = Parcel.obtain();
p.writeInt(isbn);
p.writeString(title);
p.writeString(author);
p.writeString(editText.getText().toString());
p.writeString("$15.00");
return null;
}
}
最佳答案
如果您尝试通过使用 Intent 将 Parcelable 对象发送到新 Activity,则应该像调用 Intentobject.putExtra("key", Parcelableobject) 一样简单。 Android 将为您处理剩下的事情,因为您的对象实现了 Parcelable 接口(interface)。
关于java - 创建用户定义的Parcelable对象并实现功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374649/