2012-11-16 47 views
0

我有以下我想转换为parcelable类的类。 试过this tutorialthis thread但卡住了。也许你可以把我推向正确的方向?convert class to parcelable

CurrentClass

import java.util.ArrayList; 

public class SimpleBookManager implements BookManager { 

    private ArrayList<Book> allBooks = new ArrayList<Book>(); 

    public ArrayList<Book> getAllBooks(){ 
    return allBooks; 
    } 
    public int count(){ 
     return getAllBooks().size(); 
    } 
    public Book getBook(int index){ 
     return allBooks.get(index); 
    } 
    public Book createBook(){ 
     Book book = new Book(); 
     allBooks.add(book); 
     return book; 
    } 
    public void removeBook(Book book){ 
     allBooks.remove(book); 
     //Remove instance of book 
    } 
    public void moveBook (int from, int to){ 
     Book book1 = allBooks.get(from); 
     Book book2 = allBooks.get(to); 
     allBooks.add(to, book1); 
     allBooks.add(from, book2); 

    } 
    public int getMinPrice(){ 
    ArrayList<Integer> allPrices = getAllPrices(); 
    int smallestElem=allPrices.get(0); 
    for(int i=0; i < allPrices.size(); i++){ 
     if (smallestElem > allPrices.get(i)){ 
      smallestElem = allPrices.get(i); 
     } 
     } 
     return smallestElem;  
    } 
    public int getMaxPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int biggestElem=allPrices.get(0); 
     for(int i=0; i < allPrices.size(); i++){ 
      if (biggestElem < allPrices.get(i)){ 
       biggestElem = allPrices.get(i); 
      } 
      } 
      return biggestElem; 
    } 
    public float getMeanPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
      } 
      return total/allPrices.size(); 

    } 
    public int getTotalCost(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
      } 
      return total; 
    } 
    public void saveChanges(){ 
     //What to do here 
    } 
    private ArrayList<Integer> getAllPrices(){ 
     int totalElements = allBooks.size(); 
     ArrayList<Integer> allBookPrices = new ArrayList<Integer>(); 
     //loop through it 
     for(int i=0; i < totalElements; i++){ 
      allBookPrices.add(allBooks.get(i).getPrice()); 
     } 
     return allBookPrices; 
    } 

    public SimpleBookManager(){ 
     Book harryPotter1 = createBook(); 
     Book harryPotter2 = createBook(); 

     harryPotter1.setAuthor("JK Rowling"); 
     harryPotter1.setCourse("Harry Potter Kunskap"); 
     harryPotter1.setPrice(199); 
     harryPotter1.setTitle("Harry Potter and the philosifer Stone"); 
     harryPotter1.setIsbn("9780590353403"); 

     harryPotter2.setAuthor("JK Rowling"); 
     harryPotter2.setCourse("Harry Potter Kunskap"); 
     harryPotter2.setPrice(299); 
     harryPotter2.setTitle("Harry Potter and snake"); 
     harryPotter2.setIsbn("0439064872"); 
    } 
} 

我走了多远

import android.os.Parcel; 
import android.os.Parcelable; 
import java.util.ArrayList; 

public class SimpleBookManager implements BookManager, Parcelable{ 

    private ArrayList<Book> allBooks = new ArrayList<Book>(); 

    public ArrayList<Book> getAllBooks(){ 
    return allBooks; 
    } 
    public int count(){ 
     return getAllBooks().size(); 
    } 
    public Book getBook(int index){ 
     return allBooks.get(index); 
    } 
    public Book createBook(){ 
     Book book = new Book(); 
     allBooks.add(book); 
     return book; 
    } 
    public void removeBook(Book book){ 
     allBooks.remove(book); 
     //Remove instance of book 
    } 
    public void moveBook (int from, int to){ 
     Book book1 = allBooks.get(from); 
     Book book2 = allBooks.get(to); 
     allBooks.add(to, book1); 
     allBooks.add(from, book2); 
    } 
    public int getMinPrice(){ 
    ArrayList<Integer> allPrices = getAllPrices(); 
    int smallestElem=allPrices.get(0); 
    for(int i=0; i < allPrices.size(); i++){ 
     if (smallestElem > allPrices.get(i)){ 
      smallestElem = allPrices.get(i); 
     } 
     } 
     return smallestElem;  
    } 
    public int getMaxPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int biggestElem=allPrices.get(0); 
     for(int i=0; i < allPrices.size(); i++){ 
      if (biggestElem < allPrices.get(i)){ 
       biggestElem = allPrices.get(i); 
      } 
      } 
      return biggestElem; 
    } 
    public float getMeanPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
      } 
      return total/allPrices.size(); 

    } 
    public int getTotalCost(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
      } 
      return total; 
    } 
    public void saveChanges(){ 
     //What to do here 
    } 
    private ArrayList<Integer> getAllPrices(){ 
     int totalElements = allBooks.size(); 
     ArrayList<Integer> allBookPrices = new ArrayList<Integer>(); 
     //loop through it 
     for(int i=0; i < totalElements; i++){ 
      allBookPrices.add(allBooks.get(i).getPrice()); 
     } 
     return allBookPrices; 
    } 

    public SimpleBookManager(){ 
     Book harryPotter1 = createBook(); 
     Book harryPotter2 = createBook(); 

     harryPotter1.setAuthor("JK Rowling"); 
     harryPotter1.setCourse("Harry Potter Kunskap"); 
     harryPotter1.setPrice(199); 
     harryPotter1.setTitle("Harry Potter and the philosifer Stone"); 
     harryPotter1.setIsbn("9780590353403"); 

     harryPotter2.setAuthor("JK Rowling"); 
     harryPotter2.setCourse("Harry Potter Kunskap"); 
     harryPotter2.setPrice(299); 
     harryPotter2.setTitle("Harry Potter and snake"); 
     harryPotter2.setIsbn("0439064872"); 
    } 

    //parcel part, not finished_________________________________________________________________________________ 
    public SimpleBookManager(Parcel in){ 
    //...Incomplete 
    } 
    @Override 
    public int describeContents() { 
    // TODO Auto-generated method stub 
    return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
    // TODO Auto-generated method stub 

    dest.writeStringArray(new String[]{this.UserName,this.Password,String.valueOf(this.Action)}); 
    } 

    public static final Parcelable.Creator<SimpleBookManager> CREATOR= new Parcelable.Creator<SimpleBookManager>() { 

    @Override 
    public SimpleBookManager createFromParcel(Parcel source) { 
    // TODO Auto-generated method stub 
    return new SimpleBookManager(source); //using parcelable constructor 
    } 

    @Override 
    public SimpleBookManager[] newArray(int size) { 
    // TODO Auto-generated method stub 
    return new SimpleBookManager[size]; 
    } 
    }; 
} 

我知道,它过分的要求给予延长的答案,但也许说的构造应该看起来怎么样,有些方法所以我可以看到它是如何完成的,因为尽管示例看起来很容易,但我无法控制它。谢谢=)

回答

1

Parcelable的principe是该对象能够序列化其状态到宗地并反序列化它。将它与写入和读取流中的对象进行比较。实际上,这个想法与Serializable界面非常相似,这就是为什么你会看到很多Q &提及Serializable作为替代。在大多数情况下,这是最简单的解决方案,因为它几乎不需要开发人员的努力。但是,在Android上使用Parcelable通常更有效。

不管怎样,我用自己的代码片段写了一个快速的示例实现。有些事情要注意:

  1. 我删除了BookManager接口,以方便使用。
  2. 我倒过来设计了Book班。这很可能是不完整的,但为了这个例子,它应该足够了。
  3. 我从SimpleBookManager的构造函中删除了两本哈利波特的书。

有了下面的例子,你应该能够做这样的事,看到相同的内容managerparcelledManager

// create manager and two example books 
SimpleBookManager manager = new SimpleBookManager(); 
Book harryPotter1 = manager.createBook(); 
Book harryPotter2 = manager.createBook(); 

harryPotter1.setAuthor("JK Rowling"); 
harryPotter1.setCourse("Harry Potter Kunskap"); 
harryPotter1.setPrice(199); 
harryPotter1.setTitle("Harry Potter and the philosifer Stone"); 
harryPotter1.setIsbn("9780590353403"); 

harryPotter2.setAuthor("JK Rowling"); 
harryPotter2.setCourse("Harry Potter Kunskap"); 
harryPotter2.setPrice(299); 
harryPotter2.setTitle("Harry Potter and snake"); 
harryPotter2.setIsbn("0439064872"); 

// let's use an intent to parcel the manager to 
Intent intent = new Intent(); 
intent.putExtra("extra_book_manager", manager); 

// read the parcelled manager back from the intent 
SimpleBookManager parcelledManager = intent.getParcelableExtra("extra_book_manager"); 

SimpleBookManager(和Book嵌套静态类),既实现Parcelable

public class SimpleBookManager implements Parcelable{ 

    /**************************** 
    * Book 
    ****************************/ 

    public static class Book implements Parcelable { 
     private String mAuthor, mTitle, mIsbn, mCourse; 
     private int mPrice; 

     public Book() { /* empty */ } 

     public Book(Parcel in) { 
      // read all fields back from the parcel 
      mAuthor = in.readString(); 
      mTitle = in.readString(); 
      mIsbn = in.readString(); 
      mCourse = in.readString(); 
      mPrice = in.readInt(); 
     } 

     public String getAuthor() { 
      return mAuthor; 
     } 
     public String getTitle() { 
      return mTitle; 
     } 
     public String getIsbn() { 
      return mIsbn; 
     } 
     public String getCourse() { 
      return mCourse; 
     } 
     public int getPrice() { 
      return mPrice; 
     } 
     public void setAuthor(String author) { 
      mAuthor = author; 
     } 
     public void setTitle(String title) { 
      mTitle = title; 
     } 
     public void setIsbn(String isbn) { 
      mIsbn = isbn; 
     } 
     public void setCourse(String course) { 
      mCourse = course; 
     } 
     public void setPrice(int price) { 
      mPrice = price; 
     } 

     @Override public int describeContents() { 
      return 0; 
     } 

     @Override public void writeToParcel(Parcel out, int flags) { 
      // write all fields to the parcel 
      out.writeString(mAuthor); 
      out.writeString(mTitle); 
      out.writeString(mIsbn); 
      out.writeString(mCourse); 
      out.writeInt(mPrice); 
     } 

     public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() { 
      @Override public Book createFromParcel(Parcel source) { 
       return new Book(source); 
      } 

      @Override public Book[] newArray(int size) { 
       return new Book[size]; 
      } 
     }; 
    } 

    /**************************** 
    * BookManager 
    ****************************/ 

    private ArrayList<Book> allBooks = new ArrayList<Book>(); 

    public SimpleBookManager() { /* empty */ } 

    public SimpleBookManager(Parcel in) { 
     // read all the books from the parcel as a typed list 
     in.readTypedList(allBooks, Book.CREATOR); 
    } 

    public ArrayList<Book> getAllBooks(){ 
     return allBooks; 
    } 

    public int count(){ 
     return getAllBooks().size(); 
    } 

    public Book getBook(int index){ 
     return allBooks.get(index); 
    } 

    public Book createBook(){ 
     Book book = new Book(); 
     allBooks.add(book); 
     return book; 
    } 

    public void removeBook(Book book){ 
     allBooks.remove(book); 
    } 

    public void moveBook (int from, int to){ 
     Book book1 = allBooks.get(from); 
     Book book2 = allBooks.get(to); 
     allBooks.add(to, book1); 
     allBooks.add(from, book2); 
    } 

    public int getMinPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int smallestElem=allPrices.get(0); 
     for(int i=0; i < allPrices.size(); i++){ 
      if (smallestElem > allPrices.get(i)){ 
       smallestElem = allPrices.get(i); 
      } 
     } 
     return smallestElem;  
    } 

    public int getMaxPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int biggestElem=allPrices.get(0); 
     for(int i=0; i < allPrices.size(); i++){ 
      if (biggestElem < allPrices.get(i)){ 
       biggestElem = allPrices.get(i); 
      } 
     } 
     return biggestElem; 
    } 

    public float getMeanPrice(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
     } 
     return total/allPrices.size(); 

    } 

    public int getTotalCost(){ 
     ArrayList<Integer> allPrices = getAllPrices(); 
     int total=0; 
     for(int i=0; i < allPrices.size(); i++){ 
      total+=allPrices.get(i); 
     } 
     return total; 
    } 

    public void saveChanges(){ 
     //What to do here 
    } 

    private ArrayList<Integer> getAllPrices(){ 
     int totalElements = allBooks.size(); 
     ArrayList<Integer> allBookPrices = new ArrayList<Integer>(); 
     //loop through it 
     for(int i=0; i < totalElements; i++){ 
      allBookPrices.add(allBooks.get(i).getPrice()); 
     } 
     return allBookPrices; 
    } 

    @Override public int describeContents() { 
     return 0; 
    } 

    @Override public void writeToParcel(Parcel out, int flags) { 
     // write all books as a typed list into the parcel 
     out.writeTypedList(allBooks); 
    } 

    public static final Parcelable.Creator<SimpleBookManager> CREATOR= new Parcelable.Creator<SimpleBookManager>() { 

     @Override public SimpleBookManager createFromParcel(Parcel source) { 
      return new SimpleBookManager(source); 
     } 

     @Override public SimpleBookManager[] newArray(int size) { 
      return new SimpleBookManager[size]; 
     } 
    }; 
}