我有一个Books.java
班implements
Book.java
列表,MainActivity.java
我用图书填充对象库。我想将对象库从MainActivity.java
发送到ListActivity.java
,但不能与i.putExtra.
一起使用我怎么做?
Books.java
import java.util.ArrayList;
import java.util.List;
public class Books implements DataAggregate <Book>{
private List<Book> books;
public Books(){};
public List<Book> getAll() {
return this.getBooks();
}
public int size() {
return this.getBooks().size();
}
public void insert(Book element) {
this.getBooks().add(element);
}
public void delete(Book element) {
this.getBooks().remove(element);
}
private List<Book> getBooks() {
if (books==null){
books=new ArrayList<Book>();
}
return books;
}
}
MainActivity.java
Books library;
for (int i = 0; i<db_books.size(); i++){
String name = db_books.get(i).getBookName();
String author = db_books.get(i).getBookAuthor();
String epc = db_books.get(i).getBookEpc();
Integer number = db_books.get(i).getNumBooks();
Book myBook = new Book(name, author, epc, number);
library.insert(myBook);
}
Intent i = new Intent(getApplicationContext(),ListActivity.class);
i.putExtra("library",library);
startActivity(i);
ListActivity.java
Books library = extras.getString("library");
Book.java
public class Book implements Serializable {
private String bookName;
private String bookAuthor;
private String bookEpc;
private Integer numBooks;
public Book(){
}
public Book(String bname, String bauthor, String bepc, Integer nbooks){
this.bookName = bname;
this.bookAuthor = bauthor;
this.bookEpc = bepc;
this.numBooks = nbooks;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookEpc() {
return bookEpc;
}
public void setBookEpc(String bookEpc) {
this.bookEpc = bookEpc;
}
public Integer getNumBooks() {
return numBooks;
}
public void setNumBooks(Integer numBooks) {
this.numBooks = numBooks;
}
public void prettyPrint(){
Log.d("Book:", this.bookName+" "+this.bookAuthor+" "+this.bookEpc+" "+this.numBooks.toString());
}
}
答案 0 :(得分:0)
试试这可能对你有所帮助
像这样发送
Intent i = new Intent(getApplicationContext(),DetailedBookActivity.class);
i.putExtra("library",library);
并得到这样的
Book libs= new Book();
libs = (Book)getIntent().getSerializableExtra("library");
答案 1 :(得分:0)
而不是
Books library = extras.getString("library");
你应该做
Books library = (Books) extras. getSerializableExtra("library");
答案 2 :(得分:0)
我这样解决了:
要在两个活动之间传递,我使用它,
在MainActivity.java中:
Intent intent=new Intent(getApplicationContext(),ListActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("library", library);
intent.putExtras(bundle);
startActivity(intent);
在ListActivity.java中设置库:
Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
final Books library =(Books)bundle.getSerializable("library");
ListActivity.java中的最终库对象包含所有书籍。
答案 3 :(得分:0)
Books类必须实现Serializable
import java.util.ArrayList;
import java.util.List;
public class Books implements DataAggregate <Book>, Serializable {
private List<Book> books;
public Books(){};
public List<Book> getAll() {
return this.getBooks();
}
public int size() {
return this.getBooks().size();
}
public void insert(Book element) {
this.getBooks().add(element);
}
public void delete(Book element) {
this.getBooks().remove(element);
}
private List<Book> getBooks() {
if (books==null){
books=new ArrayList<Book>();
}
return books;
}
}
MainActivity
Books library;
for (int i = 0; i<db_books.size(); i++){
String name = db_books.get(i).getBookName();
String author = db_books.get(i).getBookAuthor();
String epc = db_books.get(i).getBookEpc();
Integer number = db_books.get(i).getNumBooks();
Book myBook = new Book(name, author, epc, number);
library.insert(myBook);
}
// don't use applicationContext. use activity context "this".
Intent i = new Intent(this, ListActivity.class);
i.putExtra("library", library);
startActivity(i);
和ListActivity getIntent
Books library = (Books) getIntent().getSerializableExtra("library");
答案 4 :(得分:0)
答案 5 :(得分:0)
first your Books class implements to Serializable.
Intent i = new Intent(getApplicationContext(),ListActivity.class);
i.putExtra("library",library);
startActivity(i);
get method
Intent i = getIntent();
Book libs= new Book();
libs = (Book) i.getSerializableExtra("library");