开发者

How to add new TextView with new data in cycle?

How to add new TextView with new data(name), from Data Base? And add to tableRow in TableLayout?

My table:

        _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();
开发者_运维技巧


If you want do add TextViews dynamically:

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();
}

If you want to create a TableRow, you'd better define it in an XML file and inflate it on every iteration:

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜