2015-06-03 45 views
1

我得到了一個錯誤如下:getParcelableArrayList返回一個Parcelable,但我需要它返回我的對象​​。 (JAVA的機器人)

Error:(133, 15) error: method setMonkeyBuisness in class QuoteBank cannot be applied to given types; 
required: ArrayList<QuoteQuestion> 
found: ArrayList<Parcelable> 
reason: actual argument ArrayList<Parcelable> cannot be converted to  
ArrayList<QuoteQuestion> by method invocation conversion 

兩個QuoteQuestionQuoteBank實施Parcelable及其所有方法。我也無法輸入演員陣容。

我正在使用Parcelable數組列表是否正確?

這裏是QuoteBank我的一些代碼部分:

public class QuoteBank implements Parcelable{ 
    public static final String ARRAY_LIST_KEY = "arrayListKey"; 
    private ArrayList<QuoteQuestion> monkeyBuisness; 


    public QuoteBank(){ 
    } 
    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     Bundle bundle = new Bundle(); 

     bundle.putParcelableArrayList(ARRAY_LIST_KEY, monkeyBuisness); 

     dest.writeBundle(bundle); 
    } 

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

     @Override 
     public QuoteBank createFromParcel(Parcel source) { 

      Bundle bundle = source.readBundle(); 
      QuoteBank qb = new QuoteBank(); 
      qb.setMonkeyBuisness(bundle.getParcelableArrayList(ARRAY_LIST_KEY)); 

      return qb; 
     } 

    public void setMonkeyBuisness(ArrayList<QuoteQuestion> monkeyBuisness) { 
     this.monkeyBuisness = monkeyBuisness; 
    } 

這裏是QuoteQuestion代碼:

public class QuoteQuestion implements Parcelable{ 
public static final String QUOTE_TYPE = "quoteType"; 
public static final String QUOTE_NUMBER = "quoteNumber"; 
public static final String QUOTE_ARRAY = "quoteArray"; 
public static final String SPEAKER_ARRAY = "speakerArray"; 
public static final String ANSWER_INDEX_ARRAY = "answerIndexArray"; 
public static final String ANSWER_CHOICE_ARRAY = "answerChoiceArray"; 
public static final String CONTEXT_KEY = "contextKey"; 
public static final String CHOSEN_ANSWER = "chosenAnswer"; 
public static final String WORD_SPLIT = "wordSplit"; 
private int quoteNumber; 
private String quoteType; 
private ArrayList<String> quote; 
private ArrayList<String> speaker; 
private ArrayList<Integer> answerIndex; 
private ArrayList<String> answerChoice; 
private String context; 
private String chosenAnswer; 

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

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    Bundle bundle = new Bundle(); 

    // insert the key value pairs to the bundle 

    bundle.putInt(QUOTE_NUMBER, quoteNumber); 
    bundle.putString(QUOTE_TYPE, quoteType); 
    bundle.putStringArrayList(QUOTE_ARRAY, quote); 
    bundle.putStringArrayList(SPEAKER_ARRAY, speaker); 
    bundle.putIntegerArrayList(ANSWER_INDEX_ARRAY, answerIndex); 
    bundle.putStringArrayList(ANSWER_CHOICE_ARRAY, answerChoice); 
    bundle.putString(CONTEXT_KEY, context); 
    bundle.putString(CHOSEN_ANSWER, chosenAnswer); 
    bundle.putStringArrayList(WORD_SPLIT, wordSplitTypeA); 

    // write the key value pairs to the parcel 
    dest.writeBundle(bundle); 

} 

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

    @Override 
    public QuoteQuestion createFromParcel(Parcel source) { 
     // read the bundle containing key value pairs from the parcel 
     Bundle bundle = source.readBundle(); 
     QuoteQuestion quoteQuestion = new QuoteQuestion(); 

     quoteQuestion.setQuoteNumber(bundle.getInt(QUOTE_NUMBER)); 
     quoteQuestion.setQuoteType(bundle.getString(QUOTE_TYPE)); 
     quoteQuestion.setQuote(bundle.getStringArrayList(QUOTE_ARRAY)); 
     quoteQuestion.setSpeaker(bundle.getStringArrayList(SPEAKER_ARRAY)); 
     quoteQuestion.setAnswerIndex(bundle.getIntegerArrayList(ANSWER_INDEX_ARRAY)); 
     quoteQuestion.setAnswerChoice(bundle.getStringArrayList(ANSWER_CHOICE_ARRAY)); 
     quoteQuestion.setContext(bundle.getString(CONTEXT_KEY)); 
     quoteQuestion.setChosenAnswer(bundle.getString(CHOSEN_ANSWER)); 
     quoteQuestion.setWordSplitTypeA(bundle.getStringArrayList(WORD_SPLIT)); 

     return quoteQuestion; 
    } 

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

}; 

也對我有第二個問題,而在這裏 - 這似乎所有的大型多用戶界面應用程序將幾乎所有的類都實現了parcelable?因爲它是獲取應用程序數據的唯一方法?這是最佳做法嗎?

回答

0

分裂您的發言分爲二,用變量來保存正確類型的ArrayList,像這樣:

ArrayList<QuoteQuestion> qq = bundle.getParcelableArrayList(ARRAY_LIST_KEY); 
qb.setMonkeyBuisness(qq); 

爲什麼這個工程,而鑄造不?我不知道。如果有人知道,請替換此段!

至於第二個問題,關於在所有地方實施Parcelable:所有活動的臨時設計性質和大量使用意向可導致在許多地方需要Parcelable。某些應用程序設計模式可以幫助緩解問題。例如,遵循MVC技術,您的應用程序數據可以存在於模型中,可通過從Application導出的自定義類訪問。這允許大多數活動避免保存恢復捆綁數據,因爲它們僅僅是模型的視圖,它貫穿設備輪換等。當然,這是一個更大的話題,有許多不同的方法,但希望這會引發一些想法。

0

應該足以寫:

qb.setMonkeyBuisness(bundle.<QuoteQuestion>getParcelableArrayList(ARRAY_LIST_KEY)); 
相關問題