我得到了一個錯誤如下: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
兩個QuoteQuestion
和QuoteBank
實施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?因爲它是獲取應用程序數據的唯一方法?這是最佳做法嗎?