我正在尝试创建一个这样的数组:
JSONArray outer = new JSONArray();
JSONArray orows = new JSONArray();
JSONArray inner = new JSONArray();
JSONArray trows = new JSONArray();
for (int i = 0; i < rows; i++) {
String temprow = 10 + "" + qid + "" + i;
int rid = Integer.parseInt(temprow);
final TableRow tr = new TableRow(context);
tr.setId(rid);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f));
for (int cols = 0; cols < columnHeaders.size(); cols++) {
int cid = Integer.parseInt(tempcol);
// Create a TextView to house the name of the province
final EditText labelTV = new EditText(context);
labelTV.setId(cid);
if (answers.isEmpty()) {
System.out.println("TABLES: Answer Empty");
labelTV.setText(columnHeaders.get(cols));
} else {
JSONObject test = new JSONObject();
for (int z = 0; z < answers.size(); z++) {
String ans = answers.get(z);
String[] parts = ans.split("<:>");
int col = Integer.parseInt(parts[0]);
int row = Integer.parseInt(parts[1]);
String text = parts[2];
if (labelTV.getId() == col && tr.getId() == row) {
labelTV.setText(text);
try {
test.put("id", col);
test.put("value", text);
trows.put(test);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
inner.put(trows);
orows.put(inner);
}
outer.put(trows);
System.out.println("JSON outer IS: " + outer.toString());
}
我有一种感觉,这是我的JSON逻辑的一个简单问题,但我在某处弄错了。
这是我的代码:
JSON outer IS: [[{"id":206780,"value":"p1"},{"id":206781,"value":"n1"},
{"id":206782,"value":"d1"},{"id":206780,"value":"p2"},
{"id":206781,"value":"n2"},{"id":206782,"value":"d2"},
{"id":206780,"value":"p3"},{"id":206781,"value":"n3"},
{"id":206782,"value":"d3"},{"id":206780,"value":"p4"},
{"id":206781,"value":"n4"},{"id":206782,"value":"d4"},
{"id":206780,"value":"p5"},{"id":206781,"value":"n5"},
{"id":206782,"value":"d5"}]]
我得到的输出是:
JSONArray inner = new JSONArray();
for (int i = 0; i < rows; i++) {
JSONArray trows = new JSONArray();
String temprow = 10 + "" + qid + "" + i;
int rid = Integer.parseInt(temprow);
// Create a TableRow and give it an ID
final TableRow tr = new TableRow(context);
tr.setId(rid);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f));
//TODO: create the JSON object here
for (int cols = 0; cols < columnHeaders.size(); cols++) {
String tempcol = 20 + "" + qid + "" + cols;
int cid = Integer.parseInt(tempcol);
// Create a TextView to house the name of the province
final EditText labelTV = new EditText(context);
labelTV.setId(cid);
if (answers.isEmpty()) {
System.out.println("TABLES: Answer Empty");
labelTV.setText(columnHeaders.get(cols));
} else {
JSONObject test = new JSONObject();
for (int z = 0; z < answers.size(); z++) {
String ans = answers.get(z);
String[] parts = ans.split("<:>");
int col = Integer.parseInt(parts[0]);
int row = Integer.parseInt(parts[1]);
String text = parts[2];
//set the answer from the prefilled database
if (labelTV.getId() == col && tr.getId() == row) {
labelTV.setText(text);
try {
test.put("id", col);
test.put("value", text);
trows.put(test);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
inner.put(trows);
}
我不确定我哪里出错了。
解。感谢ci _
editor.session.insert({row: 1, column: 0}, "text\n")
答案 0 :(得分:2)
您需要在外部for循环中初始化ElementNotVisibleException
,然后trows
将是您的最终输出。
由于您当前正在循环外部初始化inner
,因此它永远不会被重置&#34;重置&#34;在循环迭代之间,你的最终trows
只是一个单个元素为outer.put(trows)
的数组。