我的JSON只显示一个图像和文字 [英] my json show only single image and text

查看:109
本文介绍了我的JSON只显示一个图像和文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON文件只显示一个图像不显示所有图像和文字下面是我的code部份是我的屏幕截图

图像

和我想要像这样

图像

告诉我,什么是错在我的code?为什么它没有显示所有三个图像和TextView的?????

  {
    世界人口: [{
            等级:1,
            名:早餐,
            URL:HTTP://niel986.files.word$p$pss.com/2012/07/fast-food.jpg
        },{
            等级:2,
            NAME2:午餐,
            URL2:http://www.bubblews.com/assets/images/news/1107772406_1370520219.gif
        },{
            等级:3,
            NAME3:晚餐,
            URL3:http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ / S1600 /鸡 - biryani.jpg
        }
    ]
 }


  ImageView的IMG1 =(ImageView的)findViewById(R.id.img1);    TextView的TXT1 =(的TextView)findViewById(R.id.txt1);    ImageView的IMG2 =(ImageView的)findViewById(R.id.img2);    TextView的TXT2 =(的TextView)findViewById(R.id.txt2);    ImageView的IMG3 =(ImageView的)findViewById(R.id.img3);    TextView的txt3 =(的TextView)findViewById(R.id.txt3);    cla3 =新CategoryListAdapter3(thirdstep.this);    MenuSelect = Utils.MenuSelect;     尝试{        HttpClient的客户端=新DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(),
 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(),15000);
        HttpUriRequest要求=新HTTPGET(MenuSelect);
        HTT presponse响应= client.execute(请求);
        InputStream的atomInputStream = response.getEntity()的getContent()。
        在的BufferedReader =新的BufferedReader(新
 InputStreamReader的(atomInputStream));        串线;
        字符串str =;
        而((行= in.readLine())!= NULL){
            STR + =行;
        }
            JSONObject的JSON =新的JSONObject(STR);
            JSONArray数据= json.getJSONArray(世界人口);              的for(int i = 0; I< data.length();我++)
                {
                    JSONObject的jsonOrder =(JSONObject的)data.get(I)
                    INT rabk = jsonOrder.getInt(等级);
                    字符串名称= jsonOrder.getString(名称);
                    字符串为url1 = jsonOrder.getString(URL);
                    txt1.setText(名);
                     位图位图=
   BitmapFactory.de codeStream((InputStream的)新的URL(网址1).getContent());
                 img1.setImageBitmap(位图);                 INT rabk2 = jsonOrder.getInt(rank2);
                    字符串名称2 = jsonOrder.getString(名2);
                    串URL2 = jsonOrder.getString(URL2);
                    txt2.setText(名2);
                     位图bitmap2 =
     BitmapFactory.de codeStream((InputStream的)新的URL(URL2).getContent());
                 img2.setImageBitmap(bitmap2);                 INT rabk3 = jsonOrder.getInt(rank3);
                    字符串NAME3 = jsonOrder.getString(NAME3);
                    串URL3 = jsonOrder.getString(URL3);
                    txt3.setText(NAME3);
                     位图bitmap3 =
      BitmapFactory.de codeStream((InputStream的)新的URL(URL3).getContent());
                 img3.setImageBitmap(bitmap3);                }

下面是我的XML code:

 <的LinearLayout 机器人:layout_width =match_parent
 机器人:layout_height =match_parent
 机器人:layout_gravity =中心
 机器人:比重=中心
 机器人:方向=横向><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>    < ImageView的
         机器人:ID =@ + ID / IMG1
        机器人:layout_width =70dp
        机器人:layout_height =80dp
        机器人:contentDescription =@空
        机器人:背景=@绘制/ imagebgborder
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
         机器人:ID =@ + ID / TXT1
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_marginTop =5DP
        机器人:文字样式=大胆
        机器人:contentDescription =@空
        机器人:文字=@字符串/参考hello world/>
< / LinearLayout中><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:方向=垂直>    < ImageView的
         机器人:ID =@ + ID / IMG2
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:contentDescription =@空
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
         机器人:ID =@ + ID / TXT2
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_marginTop =5DP
        机器人:contentDescription =@空
        机器人:文字=@字符串/参考hello world/>
  < / LinearLayout中> <的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>    < ImageView的
         机器人:ID =@ + ID / IMG3        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:contentDescription =@空
        机器人:SRC =@绘制/ ic_launcher/>    <的TextView
         机器人:ID =@ + ID / txt3
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_marginTop =5DP
        机器人:contentDescription =@空
        机器人:文字=@字符串/参考hello world/>
 < / LinearLayout中>
  < / LinearLayout中>


解决方案

您试图访问到您的JSON某些键不存在像rank2和rank3。
因此,基本上在每个周期中分配一个新的值,但只是到第一个TextView的和ImageView的。
这也是为什么它的最后淡水河谷所显示的。

my json file show only single image not show all images and text below is my code ths is my screen shot

and i want to make like this

tell me whats wrong in my code? why its not show all three images and textview?????

 {
    "worldpopulation": [{
            "rank": 1,
            "name": "BREAKFAST",
            "url": "http://niel986.files.wordpress.com/2012/07/fast-food.jpg"
        }, {
            "rank": 2,
            "name2": "LUNCH ",
            "url2": "http://www.bubblews.com/assets/images/news/1107772406_1370520219.gif"
        }, {
            "rank": 3,
            "name3": "SUPPER",
            "url3": "http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ / s1600 / chicken - biryani.jpg "
        }
    ]
 }


    ImageView img1 = (ImageView) findViewById(R.id.img1);

    TextView txt1 = (TextView) findViewById(R.id.txt1);

    ImageView img2 = (ImageView) findViewById(R.id.img2);

    TextView txt2 = (TextView) findViewById(R.id.txt2);

    ImageView img3 = (ImageView) findViewById(R.id.img3);

    TextView txt3 = (TextView) findViewById(R.id.txt3);

    cla3 = new CategoryListAdapter3(thirdstep.this);

    MenuSelect = Utils.MenuSelect;

     try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(), 
 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(MenuSelect);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new   
 InputStreamReader(atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null){
            str += line;
        }


            JSONObject json = new JSONObject(str);
            JSONArray data = json.getJSONArray("worldpopulation");

              for ( int i = 0; i < data.length(); i++ )
                {
                    JSONObject jsonOrder = (JSONObject)data.get(i);
                    int rabk = jsonOrder.getInt("rank");
                    String name = jsonOrder.getString("name");
                    String url1 = jsonOrder.getString("url");


                    txt1.setText(name);
                     Bitmap bitmap = 
   BitmapFactory.decodeStream((InputStream)new URL(url1).getContent());
                 img1.setImageBitmap(bitmap);



                 int rabk2 = jsonOrder.getInt("rank2");
                    String name2 = jsonOrder.getString("name2");
                    String url2 = jsonOrder.getString("url2");


                    txt2.setText(name2);
                     Bitmap bitmap2 = 
     BitmapFactory.decodeStream((InputStream)new URL(url2).getContent());
                 img2.setImageBitmap(bitmap2);

                 int rabk3 = jsonOrder.getInt("rank3");
                    String name3 = jsonOrder.getString("name3");
                    String url3 = jsonOrder.getString("url3");


                    txt3.setText(name3);
                     Bitmap bitmap3 = 
      BitmapFactory.decodeStream((InputStream)new URL(url3).getContent());
                 img3.setImageBitmap(bitmap3);



                }

below is my xml code:

 <LinearLayout 

 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:gravity="center"
 android:orientation="horizontal" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img1"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:contentDescription="@null"
        android:background="@drawable/imagebgborder"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
  </LinearLayout>

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img3"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
 </LinearLayout>
  </LinearLayout>

解决方案

You are trying to access to some keys in your json that don't exist like "rank2" and "rank3". So basically in every cycle you assign a new value but just to the first textview and imageview. That also why it's the last vale the one that is shown.

这篇关于我的JSON只显示一个图像和文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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