1

I have a class Books.java that implements a list of Book.java, and in MainActivity.java i fill the object library with books. I want send the object library from MainActivity.java to ListActivity.java but not works with i.putExtra. How I can do it?

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());
    }
}
Joan Triay
  • 1,518
  • 6
  • 20
  • 35
  • Refer this [link](http://stackoverflow.com/a/14333555/2078074). Hope it helps for you. – Dhruv Jun 03 '16 at 13:32
  • @Lawrance I have to implement Books.java as Serializable? – Joan Triay Jun 03 '16 at 14:17
  • @Lawrance not works, `java.lang.RuntimeException: Unable to start activity ComponentInfo{upf.mybooks/upf.mybooks.ListActivity}: java.lang.ClassCastException: upf.mybooks.model.Books cannot be cast to java.util.List` – Joan Triay Jun 03 '16 at 14:17

6 Answers6

0

Try this may be help to you

send like this

 Intent i = new Intent(getApplicationContext(),DetailedBookActivity.class);
 i.putExtra("library",library);

and get like this

Book libs= new Book();
libs = (Book)getIntent().getSerializableExtra("library");
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
  • this not works, `i.putExtra("library", (Serializable) library);` – Joan Triay Jun 03 '16 at 13:57
  • It gives a ERROR `FATAL EXCEPTION: main Process: upf.mybooks, PID: 27029 java.lang.ClassCastException: upf.mybooks.model.Books cannot be cast to java.io.Serializable` – Joan Triay Jun 03 '16 at 13:57
0

Instead of

Books library = extras.getString("library");

You should do

Books library = (Books) extras. getSerializableExtra("library");
Dalma Racz
  • 149
  • 1
  • 7
  • That not works, `FATAL EXCEPTION: main Process: upf.mybooks, PID: 30008 java.lang.ClassCastException: upf.mybooks.model.Books cannot be cast to java.io.Serializable` – Joan Triay Jun 03 '16 at 14:09
0

I solved in this way:

To pass between two activities I use this,

In MainActivity.java:

Intent intent=new Intent(getApplicationContext(),ListActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("library", library);
intent.putExtras(bundle);
startActivity(intent);

In ListActivity.java to caputre the library:

Intent intent=this.getIntent();
Bundle bundle=intent.getExtras();
final Books library =(Books)bundle.getSerializable("library");

Final library object in ListActivity.java has all books.

Joan Triay
  • 1,518
  • 6
  • 20
  • 35
0

Books class have to implements 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);

and ListActivity getIntent

Books library = (Books) getIntent().getSerializableExtra("library");
sakony
  • 88
  • 1
  • 9
0

In my opinion first you should use Parcelable. For more details refer below links.

Link1

Link2

DJhon
  • 1,548
  • 3
  • 22
  • 39
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");
ViratBhavsar
  • 104
  • 4