如何将字符串添加到嵌套的Arraylist?

时间:2012-09-25 23:36:26

标签: android string arraylist

我有这段代码:

ArrayList<ArrayList<String>> row = new ArrayList<ArrayList<String>>();
ArrayList<String> column = new ArrayList<String>();

我的问题是我无法添加String。 如何从TextView(在Android中)添加String?

2 个答案:

答案 0 :(得分:3)

假设您已在xml中声明了TextView(将myTextView替换为TextView的ID):

TextView tv = (TextView) findViewById(R.id.myTextView);
int index = 0;//change to index in row that you want to modify
String text = tv.getText().toString();
ArrayList<String> col = row.get(index);
if (col == null) {
    col = new ArrayList<String>();
    col.add(text);
    row.add(col);
}
else {
    col.add(text);
}

答案 1 :(得分:1)

ArrayList<ArrayList<String>> row = new ArrayList<ArrayList<String>>();
ArrayList<String> column = new ArrayList<String>();
row.add(column);
column.add(myString);

String theString = row.get(0).get(0);