【问题标题】:create android layout using json response使用 json 响应创建 android 布局
【发布时间】:2012-02-13 20:48:28
【问题描述】:

我需要在 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是线性布局>>框架布局>>滚动视图>>线性布局--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。

请帮我解决这个问题。

【问题讨论】:

  • 我的游戏列表应该是这样的.. |对手名称| |最后一步| |积分| |距离上次移动的时间| |轮到谁了?|

标签: android jsonp


【解决方案1】:

您不需要将数据动态添加到 xml。您可以在 XML 中创建一些基本布局。

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

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

【讨论】:

    【解决方案2】:

    好的,然后在您的 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);

    【讨论】:

    • 这是我的xml
    【解决方案3】:

    使用如下方法,

    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);
    }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2017-05-16
      相关资源
      最近更新 更多