我創建了一個多維數組,用於製作任務的所有分配的id及其子任務的列表。如何在Android中的二維數組中添加值
//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;
}
它將錯誤在這條線 - > ListAllIds [i] [j] = TNAME; 我不知道爲什麼。 這是在數組中添加值的不正確方法嗎?
當用戶點擊「添加新的子任務」按鈕的功能,
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] = 「任務1」;
ListAllIds [1] [1] =「task1subtask1」;
ListAllIds [1] [2] =「task1subtask2」;
ListAllIds [2] [0] =「task2」;
ListAllIds [2] [1] =「task2subtask1」;
ListAllIds [3] [0] =「task3」;
TASK 1: ________
子程序1:_____
子程序2:_____
TASK 2: ________
子程序1:_____
TASK 3: ________
我不知道如何在android中的二維數組中添加值的正確語法。
嘗試使用ArrayList –
Arraylist只有一個維度。我需要二維數組。 @RaviGadipudi – Ruby
ArrayList arrayList = new ArrayList <>();使用這個,把你的字符串數組作爲列表對象 –