使用json响应创建android布局

时间:2012-01-19 06:25:30

标签: android jsonp

我需要在android中使用这个json响应创建一个游戏信息列表:

 {"count":"2","useruid":"100003264774181","games":[{"lastupdate":"2012-01-17 13:55:39","gametype":"R","players":{"2":"100002913406085,Prabir Kumar D,6sq6","1":"100003264774181,Avinash S,twyq"},"turnuid":"100002913406085","score":{"2":"0","1":"18"},"playerscount":2,"dic":"twl","active":"y","canforcewin":"n","showmsg":"n","lastmove":"HIDE,r,18","gameid":"85030616","tilesinbag":"68"},{"lastupdate":"2012-01-17 13:50:55","gametype":"R","players":{"2":"100000145226351,Sayak K,0r9o","1":"100003264774181,Avinash S,kltv"},"turnuid":"100000145226351","score":{"2":"0","1":"26"},"playerscount":2,"dic":"twl","active":"y","canforcewin":"n","showmsg":"n","lastmove":"BOX,r,26","gameid":"85030764","tilesinbag":"68"}]} 

我的xml是linearlayout>>框架布局>>滚动视图>>线性布局 - android:orientation =“horizo​​ntal”>>文字视图

目前我正在使用它,但卡住了..

JSONObject jresponse=response.getJsonResponseFor(myRequest);

                String result=jresponse.toString();
                System.out.println(" jresponse is here  "+jresponse.toString());


                JSONArray jArray = jresponse.getJSONArray("games");
                int max=jArray.length();
                System.out.println("array length::"+max);

                for (int j = 0; j < max; j++)
                   {

                JSONObject obj = jArray.getJSONObject(j);
                JSONArray names = obj.names();

                for (int k = 0; k < names.length(); k++) 
                    {
                        String name = names.getString(k);
                      String value= obj.getString(name);
                           createtableLayout();  
                    }

                   }

createtableLayout()应该将数据动态添加到xml。

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

您无需动态向xml添加数据。您可以在XML中创建一些基本布局。

您可以使用findviewbyid获取该布局,然后继续使用该布局的java添加数据。

您可以在java中找到许多创建/添加布局的示例。

答案 1 :(得分:0)

好的,然后在xml中添加一个布局,并将此布局的引用作为活动中的字段, 创建如下视图:

布局

LinearLayout ll = new LinearLayout(this);

在视图或布局使用上设置布局参数:

ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

创建视图的示例,

TextView txt = new TextView(this); txt.setText(名称);

要添加视图以使用布局: ll.addView(TXT);

将布局添加到父布局,使用: parent.addView(LL);

答案 2 :(得分:0)

使用如下方法,

holder=(LinearLayout)findViewById(R.id.GameListHolder);
JSONObject jresponse=response.getJsonResponseFor(myRequest);

                String result=jresponse.toString();
                System.out.println(" jresponse is here  "+jresponse.toString());


                JSONArray jArray = jresponse.getJSONArray("games");
                int max=jArray.length();
                System.out.println("array length::"+max);

                for (int j = 0; j < max; j++)
                   {

                JSONObject obj = jArray.getJSONObject(j);
                JSONArray names = obj.names();

                for (int k = 0; k < names.length(); k++) 
                    {
                        String name = names.getString(k);
                        String value= obj.getString(name);

                         //Fetch values here      
                         createtableLayout(opponentName, lastMove,points, time, sinceLastMove, whose turn);  
                    }

                   }


    public void createtableLayout(String opponentName, String lastMove, String points, 
                   String time, String sinceLastMove, String whoseTurn)
    {
         LinearLayout ll=new LinearLayout(this);
         LinearLayout.LayoutParams params=new                                                     LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
         TextView txtOppName=new TextView(this);
         txtOppName.setText(txtOppName);
         ll.addView(txtOppName);

TextView txtOppName=new TextView(this);
         txtOppName.setText(txtOppName);
         ll.addView(txtOppName);
          ......

       holder.addView(ll);
}
    }