使用JSON字符串填充表布局 [英] Populating Table Layout using JSON String

查看:112
本文介绍了使用JSON字符串填充表布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JSON String returned by my web service as follows:

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

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

上面的String代表我的数据集.我已经将String转换为JSON对象,现在我想将String中的数据放入TableLayout中.

The above String represents my dataset. I have converted the String to a JSON Object and I now want to put the data in the String into a TableLayout.

我希望表格在Android应用上显示时是这样的:

I want the table to be like this when displayed on android app:

    rollno       percentage    attended    missed
     abc2          40              12        34

这是我到目前为止使用的代码:

This is the code I am using as of now :

    //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.

My TableLayout contains a button and a EditText as the first TableRow. After I click the button I get the JSON String. So I need to insert 2 TableRows dynamically after that as per my requirement.

到目前为止,我的xml如下:

My xml as of now is as follows:

   <?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,以便使用String值填充它们?

So my problem is how to add those 2 extra TableRows so as to populate them with the String values ?

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

My code isn't working fine. I am not able to get the required output.

有人可以帮我吗?

谢谢

推荐答案

您可以根据需要使用多列列表视图

you can use multicolumn listview for your purpose,

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

Parse the JSON response and get values into different strings and use this blog for your purpose

多列Listview

这篇关于使用JSON字符串填充表布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆