相关疑难解决方法(0)

如何在android中使用Intent将对象的ArrayList从一个传递给另一个活动?

我在我的onClick()方法中的代码中有以下内容

 List<Question> mQuestionsList = QuestionBank.getQuestions();
Run Code Online (Sandbox Code Playgroud)

现在我有了这一行后的意图,如下:

  Intent resultIntent = new Intent(this, ResultActivity.class);
  resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);
  startActivity(resultIntent);
Run Code Online (Sandbox Code Playgroud)

我不知道如何将这个问题列表从一个活动传递到另一个活动My Question类

public class Question {
    private int[] operands;
    private int[] choices;
    private int userAnswerIndex;

    public Question(int[] operands, int[] choices) {
        this.operands = operands;
        this.choices = choices;
        this.userAnswerIndex = -1;
    }

    public int[] getChoices() {
        return choices;
    }

    public void setChoices(int[] choices) {
        this.choices = choices;
    }

    public int[] getOperands() {
        return operands;
    }

    public void setOperands(int[] operands) {
        this.operands …
Run Code Online (Sandbox Code Playgroud)

android parcelable android-intent

27
推荐指数
7
解决办法
9万
查看次数

Kotlin通过Intents传递自定义对象的数组,并以ArrayList的形式接收

我有一些活动,我使用Intent在它们之间传递数据。我想从第一个活动中传递我的自定义对象的数组,并在第二个活动中使其成为arraylist。我拥有的代码是:

data class Attachment(val Name: String, val Content: String)

class ActivityA {
    private var attachments: Array<Attachment> = arrayOf()
    fun callB() {
        intent = Intent(this,ActivityB::class.java).apply {
            putExtra("Attachments", attachments)
        }
    }
}

class ActivityB {
    private var attachments: ArrayList<Attachment>?

    override fun onCreate(savedInstanceState: Bundle?) {
        // How do I get the passed array and store in the arraylist here ?

        val a: Array<Attachment> = intent.getParcelableArrayExtra("Attachments") as Array<Attachment>
        attachments = a // fails with a type mismatch error
        attachments = ArrayList(a) // fails again …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-extension kotlin-android-extensions

3
推荐指数
1
解决办法
2981
查看次数