2012-11-16 2 views
0

필자는 다음과 같은 클래스를 소트 가능한 클래스로 변환하고자합니다. this tutorialthis thread 다음을 시도했지만 막혔습니다. 아마도 나를 올바른 방향으로 밀어 넣을 수 있을까요?클래스를 parcelable로 변환하십시오.

내가 그 많은 생성자가 어떤 방법처럼 보이는 방법을 말한다 아마도 확장 된 대답을하도록 요청하지만 것을 알고

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

얼마나 멀리 왔는지 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"); 
    } 
} 

그래서 나는 그 일을 어떻게 볼 수 있습니다. 왜냐하면 예제가 얼마나 쉽게 보일지도 모르지만 나는 그걸 이해할 수 없기 때문입니다. 감사합니다.)

답변

1

Parcelable의 문장은 객체가 상태를 소포로 직렬화하고 다시 직렬화 할 수 있다는 것입니다. 스트림에서 객체를 쓰고 읽는 것과 비교하십시오. 사실이 아이디어는 Serializable 인터페이스와 매우 유사합니다. QA 언급을 많이 볼 수있는 이유는 대체로 Serializable입니다. 대부분의 경우 개발자가 거의 노력하지 않아도되는 가장 쉬운 솔루션입니다. 그러나 Parcelable을 사용하면 Android에서 더 효율적입니다.

어쨌든, 저는 여러분의 코드 스 니펫을 사용하여 간단한 예제 구현을 작성했습니다. 참고 사항 :

  1. 나는 simplicty 용 BookManager 인터페이스를 제거했습니다.
  2. 나는 일종의 역전 류인 Book 클래스를 조작했다. 그것은 아마도 불완전하지만,이 예제를 위해 충분해야합니다.
  3. 나는 SimpleBookManager의 생성자에서 해리 포터 2 권을 삭제했습니다. 모두가 Parcelable
    // 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)을 구현

    :
  4. 예를 아래로

,이 같은 일을하고 managerparcelledManager에 대해 동일한 내용을 볼 수 있어야합니다

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]; 
     } 
    }; 
} 
관련 문제