我想將sqlite列拆分爲數組,然後爲數組中的每個項目添加TextView
。下面是我的功能在陣列添加TextView
每個項目:爲陣列中的每個項目添加TextView
private void BuildTable() {
sqlcon.open();
Cursor c = sqlcon.readEntry();
int rows = c.getCount();
int cols = c.getColumnCount();
String[] array;
c.moveToFirst();
// outer for loop
for (int i = 0; i < rows; i++) {
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 0; j < cols; j++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// tv.setBackgroundResource(R.drawable.cell_shape);
tv.setGravity(Gravity.CENTER);
tv.setTextSize(18);
tv.setPadding(0, 5, 0, 5);
array = c.getString(1).split(",");
for (int k = 0; k < array.length; k++) {
tv.setText(array[k]);
row.addView(tv);
}
}
c.moveToNext();
table_layout.addView(row);
}
sqlcon.close();
}
我收到以下錯誤:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.logquiz.thequiz.logquiz/com.logquiz.thequiz.logquiz.RulesActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
爲什麼不只是使用'ListView'? –