0

我有以下课程,我想将其转换为 parcelable 课程。尝试按照本教程此线程进行操作,但被卡住了。也许你可以把我推向正确的方向?

当前类

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];
    }
    };  
}

我知道要给出一个扩展的答案需要很多,但也许会说构造函数的外观和一些方法,以便我可以看到它是如何完成的,因为尽管示例看起来很简单,但我无法掌握它. 谢谢 =)

4

1 回答 1

1

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

无论如何,我使用您自己的代码片段编写了一个快速示例实现。需要注意的一些事项:

  1. 为了简单起见,我删除了 BookManager 界面。
  2. 我对课程进行了逆向工程Book。它很可能是不完整的,但对于这个例子来说应该足够了。
  3. SimpleBookManager我从's 的构造函数中删除了两本哈利波特书籍。

使用下面的示例,您应该能够执行类似的操作并看到 and 的相同manager内容parcelledManager

// 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];
        }
    };  
}
于 2012-11-17T04:53:06.877 回答