android json布局,android – 使用JSON字符串填充表格布局

我有一个由我的Web服务返回的

JSON字符串,如下所示:

{“checkrecord”[{“rollno”:”abc2″,”percentage”:40,”attended”:12,”missed”:34}],”Table1″:[]}

上面的String表示我的数据集.我已将String转换为JSON对象,现在我想将String中的数据放入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包含一个按钮和一个EditText作为第一个TableRow.单击按钮后,我获得了JSON字符串.所以我需要根据我的要求动态插入2个TableRows.

我现在的xml如下:

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">

android:id="@+id/edittext"

android:width="200px" />

android:id="@+id/button"

android:text="Check Record"/>

所以我的问题是如何添加这两个额外的TableRows以便用String值填充它们?

我的代码工作不正常.我无法获得所需的输出.

任何人都可以帮助我吗?

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值