java - getParcelableArrayList 返回一个 Parcelable,但我需要它来返回我的对象​​。 (java 安卓)

标签 java android parcelable parcel

我收到如下错误:

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 及其所有方法。我也无法输入cast 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];
    }

};

我还有第二个问题 - 似乎所有大型多 UI 应用程序几乎所有类都会实现 Parcelable?因为这是获取应用程序数据的唯一方法?这是最佳实践吗?

最佳答案

将语句分成两部分,使用一个变量来保存正确类型的 ArrayList,如下所示:

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

为什么这有效,而强制转换却无效?我不知道。如果有人知道,请替换这一段!

关于第二个问题,关于在所有地方实现 Parcelable:所有 Activity 的临时设计性质和 Intents 的大量使用可能导致在许多地方需要 Parcelable。某些应用程序设计模式可以帮助缓解该问题。例如,遵循 MVC 技术,您的应用程序数据可以存在于模型中,可以通过从 Application 派生的自定义类进行访问。这允许大多数 Activity 避免保存和恢复 bundle 数据,因为它们只是模型的 View ,在设备旋转等过程中持续存在。当然,这是一个更大的主题,有许多不同的方法,但希望这能激发一些想法。

关于java - getParcelableArrayList 返回一个 Parcelable,但我需要它来返回我的对象​​。 (java 安卓),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30608681/

相关文章:

java - Spring Data Repository 保存未返回具有更新审核字段的实例

java - TextView 与父宽度不匹配

android - 如何在android中的自定义构建布局中添加子布局

android - onWindowFocusChanged() 不调用 false,只调用 true

Java android Q 创建PDF java.lang.IllegalArgumentException : Unknown URL

android - 是否可以在 Android 上创建一个 Parcelable 的 HashMap?

java - Hadoop Junit 测试面临编译错误

java - 无法从 Java 中删除命名管道

java - 编写 Parcelable 时出现 StackOverflowError

Android:将对象从一个 Activity 发送到另一 Activity