如何在android中向表中添加数据

如何在android中向表中添加数据,android,mysql,sql,json,tablelayout,Android,Mysql,Sql,Json,Tablelayout,我正在尝试在android应用程序中填充一个表,该应用程序使用来自MYSQL数据库的JSON格式的数据构建xml,下面是我尝试填充表单元格的代码段: TableLayout table = (TableLayout)findViewById(R.id.tablelayout1); JSONObject jobj = json.getJSONObject(0); Log.d("json.getJSONObject(0)", ""+json.get

我正在尝试在android应用程序中填充一个表,该应用程序使用来自MYSQL数据库的JSON格式的数据构建xml,下面是我尝试填充表单元格的代码段:

        TableLayout table = (TableLayout)findViewById(R.id.tablelayout1);

        JSONObject jobj = json.getJSONObject(0);

        Log.d("json.getJSONObject(0)", ""+json.getJSONObject(0));
        textView.setId(R.id.tv21);
        textView.setText(jobj.getString("id"));

        Log.d("jobj.getString(c)", ""+jobj.getString("c"));
        rows.addView(textView);
        table.addView(rows);
json.getJSONObject0返回[{id:2222222,c:some text},foo:some other text}]

jobj.getStringc返回一些文本

我无法在条目21处设置文本,或者在整个表中的任何其他条目中设置文本。
有人能帮忙吗?

您必须指定各个值的名称。例如,如果您的表看起来像此注释,则实际上无效:

<TableLayout>
    <TableRow>
        <TextView android:id="@+id/tv1_1/>
        <TextView android:id="@+id/tv1_2/>
    </TableRow>
    <TableRow>
        <TextView android:id="@+id/tv2_1/>
        <TextView android:id="@+id/tv2_2/>
    </TableRow>
</TableLayout>

根据您的数据,您可能实际上想要使用,但我会将该解释另存一次。它适用于长数据集,例如Android联系人列表。

非常感谢@pearsonatphoto的回复!我尝试了你的解决方案,但没有成功。我正在考虑使用ListView,因为我没有处理太多的数据集,所以我决定改用TableLayout。我在代码的两个不同部分尝试了您的方法:onCreate内部方法和doInBackground方法。在第一个例子中,应用程序崩溃,抛出NullPointerException,但在doBackground中它什么也不做。尝试使用TextView命令,看看发生了什么,特别是尝试找出null指针是TextView的位置或设置TextCiew。我相信代码方法会起作用。我将通过编辑向您展示。你不应该在doInBackground中玩UI线程。如果您有一个AsyncTask,您可能希望将代码放在onPostExecute中以设置TextView。我尝试了您编辑的解决方案,但没有成功。在tv.setTextSome文本上引发NullPointerException。所以我删除了TableLayout table=tablelayoutfindviewbydr.id.tablelayout1;并使用TextView tv=TextViewfindViewByIdR.id.tv1_1;成功了!!再次非常感谢您对Pearsonatorphoto的帮助!
TableLayout table = (TableLayout)findViewById(R.id.tablelayout1);
TextView tv=((TextView)table.findViewById(R.id.tv1_1));
tv.setText("Some Text");