如何使用数据库添加带有新数据(名称)的新TextView?并在TableLayout中添加tableRow?
我的表:
_id name
1 Said
2 Bill
etc
Cursor cursor = database.query(TABLE_NAME,
new String[] {STUDENT_NAME},
null, null,null,null,null);
cursor.moveToFirst();
String text = cursor.getString(0);
TextView name1 = (TextView) findViewById(R.id.edit);
name1.setText(text);
cursor.close();
答案 0 :(得分:0)
如果你想动态添加TextView
:
ViewGroup group = (ViewGroup) findViewById(R.id.group_you_want_to_add_to);
try {
if (!cursor.moveToFirst()) {
return;
}
do {
TextView textView = new TextView(this);
textView.setText(cursor.getString(0));
group.addView(textView,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
} while (cursor.moveToNext());
} finally {
cursor.close();
}
如果要创建TableRow
,最好在XML文件中定义它并在每次迭代时对其进行充气:
LayoutInflater inflater = LayoutInflater.from(this);
// then you query a database and obtain a cursor...
try {
if (!cursor.moveToFirst()) {
return;
}
do {
TableRow row = inflater.inflate(R.id_table_row, table, null);
((TextView) row.findViewById(R.id.text)).setText(cursor.getString(0));
table.addView(row);
} while (cursor.moveToNext());
} finally {
cursor.close();
}