我有以下课程,我想将其转换为 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];
}
};
}
我知道要给出一个扩展的答案需要很多,但也许会说构造函数的外观和一些方法,以便我可以看到它是如何完成的,因为尽管示例看起来很简单,但我无法掌握它. 谢谢 =)