Android编程新手。我有2个对象必须是Parcelable,称为Book和Author。我相当肯定我正确地准备了它们。我希望得到一些帮助:
将他们发送到新活动
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;
}
}
我不知道为什么我感到难过,但我无法找到如何实施我的包裹来制作这本书并继续前进。这是试图让书籍将其发送到另一个活动的活动:
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;
}
}
答案 0 :(得分:2)
如果您尝试通过使用Intent将parcelable对象发送到新的Activity,它应该像调用intentobject.putExtra(“key”,parcelableobject)一样简单。 Android将为您处理其余部分,因为您的对象实现了Parcelable接口。
答案 1 :(得分:1)
用于发送数据使用:
Intent intent = new Intent(this,NewActivity.class);
intent.putExtra("listOfBook", bookList); // sending list
intent.putExtra("objOfBook", bookObj); // sending obj
startActivity(intent);
并获得了:
ArrayList<Book> myList = getIntent().getParcelableExtra("listOfBook"); // getting list
Book bookObj = getIntent().getParcelableExtra("objOfBook"); // getting obj
使用我的代码编辑以下函数:
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());
Book b = new Book();
b.SetIsbn(isbn);
b.SetTitle(title);
b.SetAuthor(author);
b.SetPrice("$15.00")
// i don't know what for is last line
return b;
}