gpt4 book ai didi

android - 使用 JSON 字符串填充表格布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:54 27 4
gpt4 key购买 nike

我的 Web 服务返回了一个 JSON 字符串,如下所示:

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

上面的字符串代表我的数据集。我已将字符串转换为 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 包含一个按钮和一个 EditText 作为第一个 TableRow。单击按钮后,我得到了 JSON 字符串。所以我需要根据我的要求在此之后动态插入 2 个 TableRow。

我现在的 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 个额外的 TableRow,以便用字符串值填充它们?

我的代码运行不正常。我无法获得所需的输出。

有人可以帮我吗?

谢谢

最佳答案

您可以根据需要使用多列 ListView ,

解析 JSON 响应并将值转换为不同的字符串,然后将此博客用于您的目的

Multicolumn Listview

关于android - 使用 JSON 字符串填充表格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8366911/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com