代码之家  ›  专栏  ›  技术社区  ›  Vanhaeren Thomas

向Intent发送ArrayList

  •  1
  • Vanhaeren Thomas  · 技术社区  · 7 年前

    我在一个活动中 ArrayList<ArrayList<String>> ,所以基本上是一个列表列表。

    我想完成的是,从活动1向活动2发送这个列表列表,因此我考虑了意图,但似乎无法找到必要的getExtra()。

    更准确地说, questions 包含 Strings 与问题相关,以及中的相应位置 answers 包含该问题的所有选定答案。

    public class ReviewActivity extends AppCompatActivity {
    
    ArrayList<String> questions = new ArrayList<>();
    ArrayList<ArrayList<String>> answers = new ArrayList<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);
    
        Intent intent = getIntent();
        questions = intent.getStringArrayListExtra("questions");
        answers = intent.getStringArrayListExtra("answers");
    
    }
    

    我就是这样把原稿 阵列列表(&L);阵列列表(&L);字符串(>)&燃气轮机; 目的是:

     Intent mIntent = new Intent(this, ReviewActivity.class);
                                mIntent.putExtra("questions", questions);
                                mIntent.putExtra("answers", answers);
                                startActivity(mIntent);
    

    代码在行中给出了一个错误 answers = intent.getStringArrayListExtra("answers"); 我明白为什么,但我如何才能找到解决办法?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  3
  •   ADM    7 年前

    ArrayList Serializable 因此,您可以将其作为可序列化的文件发送到bundle。

     mIntent.putExtra("answers", answers);
    

    要获取列表,请执行以下操作:

    ArrayList<ArrayList<String>> list= (ArrayList<ArrayList<String>>) getIntent().getExtras().getSerializable("answers");