我有一个JSON字符串我的web服务返回如下:填充表布局使用JSON字符串
{ “checkrecord”[{ “rollno”: “ABC2”, “百分比”:40,“出席“:12,”错过“:34}],”Table1“:[]}
上面的String表示我的数据集。我已将字符串转换为JSON对象,现在我想将字符串中的数据放入TableLayout中。
我想在Android应用程序时显示的表是这样的:
rollno percentage attended missed
abc2 40 12 34
这是我使用的是现在的代码:
//json processing code
public void jsonprocessing(String c)
{
try
{
JSONObject jsonobject = new JSONObject(c);
JSONArray array = jsonobject.getJSONArray("checkrecord");
int max = array.length();
for (int j = 0; j < max; j++)
{
JSONObject obj = array.getJSONObject(j);
JSONArray names = obj.names();
for (int k = 0; k < names.length(); k++)
{
name = names.getString(k);
value= obj.getString(name);
createtableLayout();
}
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//create table layout
public void createtableLayout()
{
Log.d("values",value); //I am able to see the values abc2,40,12 and 34 over here in logcat
TableLayout t= (TableLayout)findViewById(R.id.tablelayout);
TableRow r1=new TableRow(this);
r1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView t1 = new TextView(this);
t1.setText(value);
t1.setTextColor(Color.BLACK);
t1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
r1.addView(t1);
t.addView(r1, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
我TableLayout包含一个按钮和一个作为第一个TableRow的EditText。我点击按钮后,我得到JSON字符串。所以我需要按照我的要求动态插入2个TableRows。
我截至目前XML如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablelayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044">
<TableRow>
<EditText
android:id="@+id/edittext"
android:width="200px" />
<Button
android:id="@+id/button"
android:text="Check Record"/>
</TableRow>
</TableLayout>
所以我的问题是如何增加的2个额外的TableRows,以便它们与字符串值填充?
我的代码无法正常工作。我无法获得所需的输出。
任何人都可以帮助我吗?
感谢
为什么不来尝试多列listview而不是tablelayout – Abhi
可以举一个多列listview的例子吗? –
http://www.google.co.in/#sclient=psy-ab&hl=zh-CN&source=hp&q=multicolon+listview+in+android&pbx=1&oq=multicolon+listview+in+android&aq=f&aqi=q-w1&aql=&gs_sm= e&gs_upl = 1166l20720l0l20855l66l46l16l0l18l1628l26087l2-5.5.11.10.5.4.2l58l0&bav = on.2,or.r_gc.r_pw。,cf.osb&fp = 105f40160a393e37&biw = 1024&bih = 653 – Abhi