我正在尝试使用来自 MYSQL 数据库的 JSON 格式的数据填充使用 xml 构建的 Android 应用程序中的表格,这是我尝试填充表格单元格的代码段:
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.getJSONObject(0)
返回 [{"id":"2222222", "c":"some text"}, "foo":"some other text"} ]
和
jobj.getString("c")
返回“一些文本”
。
我无法使用“某些文本”在条目 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>
然后像这样的代码可以设置它
TableLayout table = (TableLayout)findViewById(R.id.tablelayout1);
TextView tv=((TextView)table.findViewById(R.id.tv1_1));
tv.setText("Some Text");
根据您的数据,您可能实际上想要使用 ListView ,但我会在下次再解释。它适用于处理长数据集,例如 Android 联系人列表。
关于android - 如何在android中的表中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109649/