简体   繁体   English

如何将数组传递给子活动(android)?

[英]How to pass arrays to a subactivity (android)?

My program is basically a list of items, and when you click the items it sends you to an expandable list. 我的程序基本上是项目列表,当您单击项目时,它会将您发送到可扩展列表。 The problem is, my list of items is hundreds of items long, so it becomes tedious to have to make a different expandable list class for each one. 问题是,我的项目列表长了数百个项目,因此必须为每个项目创建一个不同的可扩展列表类,这很繁琐。 Instead, I want to send different arrays to one subactivity depending on which list option was pressed. 相反,我想根据按下的列表选项将不同的数组发送到一个子活动。

Here's the part of my code in my main activity that is supposed to send the info: 这是我的主要活动中应发送信息的代码部分:

if ((String) ((TextView) view).getText() == "Name of List Item") {
               Intent myIntent = new Intent(view.getContext(),
                     SubActivity.class);
               myIntent.putExtra("parents", new String[] { "bunch of parents" });
               myIntent.putExtra("children", new String[][] {
                           { "bunch" },
                           { "of" },
                           { "children"} });
               startActivityForResult(myIntent, 0); }

Here's the part of my code that is supposed to receive it: 这是我应该接收的代码部分:

String[] parents;              //declared outside onCreate() so they can be used by methods like createParentList()
String[][] children;

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Bundle b = this.getIntent().getExtras();

      moves = b.getStringArray("parents");
      parents = (String[][]) b.getSerializable("children");   

I go on to try to use createParentList()/createChildList(), etc for my expandable list. 我继续尝试将createParentList()/ createChildList()等用于可扩展列表。 It compiles perfectly fine. 它编译得很好。 However, whenever I run it and try to click on the list item, it says that the process has stopped unexpectedly. 但是,每当我运行它并尝试单击列表项时,它都表明该过程已意外停止。 Does anyone know what is wrong? 有人知道哪里出问题了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM