如何在Android中的二维数组中添加值

时间:2015-09-28 12:50:28

标签: android arrays multidimensional-array

我创建了一个多维数组,用于列出所有已分配任务及其子任务的ID。

//VARIABLES
    int rowIndex = 2;
    int colIndex = 1;
    int rowIndex2 = 2;
    int colIndex2 = 0;
    int i = 0;
    int j = 0;
private String[][] ListAllIds;

然后当用户点击" 添加新任务"按钮,

public void addTask(View view) {

        i++;
        Map<String, Integer> idsMap = new HashMap<String, Integer>();
        String tname = "task" + Integer.toString(i);
        EditText editText = new EditText(this);
        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        param.width = GridLayout.LayoutParams.MATCH_PARENT;
        param.rowSpec = GridLayout.spec(rowIndex);
        param.columnSpec = GridLayout.spec(colIndex);
        editText.setTextColor(Color.BLACK);
        editText.setBackgroundResource(android.R.drawable.edit_text);
        editText.setText(tname);
        editText.setLayoutParams(param);

        TextView textView = new TextView(this);
        GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
        param2.rowSpec = GridLayout.spec(rowIndex2);
        param2.columnSpec = GridLayout.spec(colIndex2);
        textView.setPadding(30, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTypeface(Typeface.DEFAULT_BOLD);

        textView.setLayoutParams(param2);

        if (rowIndex > 1) {
            textView.setText("TASK "+Integer.toString(i)+": ");
            editText.setId(i);
            idsMap.put(tname, i);
        }

        ListAllIds[i][j] = tname;
        gridLayout.addView(editText);
        gridLayout.addView(textView);
        rowIndex++;
        rowIndex2++;
        this.j = 0;
    }

这一行会出错 - &gt; ListAllIds [i] [j] = tname; 我不知道为什么。 在数组中添加值是不正确的方法吗?

用户点击&#34; 添加新子任务&#34;按钮,

public void addSubtask(View view) {
        j++;
        Map<String, Integer> idsMap = new HashMap<String, Integer>();
        String taskno = "task" + Integer.toString(i) + "subtask" + Integer.toString(j);
        EditText editText = new EditText(this);
        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        param.width = GridLayout.LayoutParams.MATCH_PARENT;
        param.rowSpec = GridLayout.spec(rowIndex);
        param.columnSpec = GridLayout.spec(colIndex);
        editText.setTextColor(Color.BLACK);
        editText.setBackgroundResource(android.R.drawable.edit_text);
        editText.setText(taskno);
        editText.setLayoutParams(param);

        TextView textView = new TextView(this);
        GridLayout.LayoutParams param2 = new GridLayout.LayoutParams();
        param2.rowSpec = GridLayout.spec(rowIndex2);
        param2.columnSpec = GridLayout.spec(colIndex2);
        textView.setPadding(30,0,0,0);
        textView.setTextColor(Color.BLACK);

        textView.setLayoutParams(param2);

        if (rowIndex > 1) {
            textView.setText("Subtask "+Integer.toString(j)+": ");
            editText.setId(j);
            idsMap.put(taskno, j);
        }

        //allEdittext.add(i-1,editText);

        ListAllIds[i][j] = taskno;

        gridLayout.addView(editText);
        gridLayout.addView(textView);
        rowIndex++;
        rowIndex2++;
    }

我只想用他们的子任务创建任务ID的二维数组。

例如:

ListAllIds [1] [0] =&#34; task1&#34 ;;

ListAllIds [1] [1] =&#34; task1subtask1&#34;;

ListAllIds [1] [2] =&#34; task1subtask2&#34;;

ListAllIds [2] [0] =&#34; task2&#34 ;;

ListAllIds [2] [1] =&#34; task2subtask1&#34;;

ListAllIds [3] [0] =&#34; task3&#34 ;;

任务1: ________

子任务1:_____

子任务2:_____

任务2: ________

子任务1:_____

任务3: ________

我不知道如何在android中的二维数组中添加值的正确语法。

4 个答案:

答案 0 :(得分:1)

我对android和java也很陌生,但是从我所理解的那些:

经典数组(如String [])使用固定数量的对象进行初始化。这意味着您可以修改数组中的对象,但不能向其中添加新对象。

如果您不知道要在阵列中存储多少元素,请使用ArrayList而不是经典阵列。这样您就可以添加新元素。

这里和我创建列表的示例,其中包含一些字符串列表:

ArrayList<List<String>> myArray = new ArrayList<>();
myArray.add(Arrays.asList("task1", "subTask1", "subTask2"));
myArray.add(Arrays.asList("task2", "subTask1"));

// get the task1 value
String task1 = myArray.get(0).get(0);

// get the subTask1 of task2
String subTask1ofTask2 = myArray.get(1).get(1);

向此ArrayList添加元素的另一种方法:

myArray.add(new ArrayList<String>());
int lastElementIndex = myArray.size()-1;
List<String> lastElement = myArray.get(lastElementIndex);
lastElement.add("task3");
lastElement.add("subTask1");
lastElement.add("subTask2");

// get the task3 value
String task3 = myArray.get(2).get(0);

答案 1 :(得分:0)

试试此代码

ArrayList<String[]> strings = new ArrayList<>();
    String[] test1 = new String[]{"a","b","c"};
    String[] test2 = new String[]{"d","e","f"};
    String[] test3 = new String[]{"g","h","i"};
    String[] test4 = new String[]{"j","k","l"};

    strings.add(test1);
    strings.add(test2);
    strings.add(test3);
    strings.add(test4);

    // to edit and save back
    String[] temp = strings.get(0);
    temp[1] = "z";
    strings.set(0, temp);

如果您有任何问题,请告诉我

答案 2 :(得分:0)

当您声明数组时,必须初始化String[][]

private String[][] ListAllIds=new String[row_count][column_count];

请参阅此link

答案 3 :(得分:0)

你可以使用ArrayOfArrays

ArrayList<ArrayList<String>> myList = new ArrayList<ArrayList<String>>();

然后填写为:

myList.add(Arrays.asList("task1","Task1subtask1","Task1subtask2"));
myList.add(Arrays.asList("task2","Task2subtask1","Task2subtask2"));