0

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

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;
}
4

1 回答 1

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);
        }
    }
}
于 2012-08-15T22:23:59.777 回答