从动态内容选项卡获取控件/视图

时间:2012-08-09 00:07:43

标签: android tabcontrol

我已经从数据库创建了标签及其内容。现在我需要帮助找出如何从这些控件中获取用户输入的值 - 如何获取选项卡上的控件列表(这样可以保存数据)。以下是我如何填充标签内容的简要示例:

public View createTabContent(String tag)
{
SQLiteDatabase db2 = openOrCreateDatabase(msDbFile, MODE_PRIVATE, null);
Cursor cList = db2.rawQuery("SELECT * FROM CheckList WHERE CatID=" + tag, null);
TableLayout tl = new TableLayout(FormChecklist.this);
tl.setBackgroundColor(new Color().WHITE);
while(cList.moveToNext())
{   
    /* Create a new row to be added. */
    TableRow tr = new TableRow(FormChecklist.this);
    tr.setLayoutParams(new LayoutParams(
               LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));

     /* Create the row-content. */
    TextView lblQuestion = new TextView(FormChecklist.this);
    lblQuestion.setText(cList.getString(2));
    lblQuestion.setTag(tag);
    /* Add control to row. */
    tr.addView(lblQuestion);

    CheckBox chkCompleted = new CheckBox(FormChecklist.this);
    chkCompleted.setText("Completed");
    chkCompleted.setLayoutParams(new LayoutParams(
               LayoutParams.FILL_PARENT,
               LayoutParams.WRAP_CONTENT));
    chkCompleted.setTag(tag);
    /* Add control to row. */
    tr.addView(chkCompleted);

    /* Add row to TableLayout. */
    tl.addView(tr,new TableLayout.LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
}
db2.close();
return tl;
}

1 个答案:

答案 0 :(得分:0)

没有回复,但终于想通了,希望有人会发现此信息有用。 我采用的方法是修改我的createTabContent,为TableLayout添加一个ID:

tl.setId(1000 + miTlCnt);

然后在我的保存程序中,我循环控制如下:

for (int i = 1000 + miTlCnt; i > 1000; i--)
{ 
    TableLayout tl = (TableLayout)findViewById(i);
    if (tl != null)
    {
        for (int j = 0; j < tl.getChildCount(); j++)
        {
            TableRow row = (TableRow)tl.getChildAt(j);
            CheckBox chkCompleted = (CheckBox)row.getChildAt(1);
        }
    }
}