从一个JSON的Andr​​oid喂食饲料的图像到一个列表 [英] Android Feeding images from a Json Feed into a list

查看:109
本文介绍了从一个JSON的Andr​​oid喂食饲料的图像到一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个名单上有它填补了一个标题,从一个JSON提要,现在我想将图像添加到其继承人我single_list布局

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =机器人:ATTR /列表preferredItemHeight

    机器人:填充=6dip>

    < ImageView的
        机器人:ID =@ + ID /图标
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_alignParentBottom =真
        机器人:layout_alignParentTop =真/>

<的TextView
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
    机器人:背景=@可绘制/ ic_gr_arrow
    机器人:ID =@ + ID / textView1
   机器人:文字颜色=@色/蓝色
   机器人:填充=20dp
   机器人:TEXTSIZE =10dp

     />


< / RelativeLayout的>
 

继承人从JSON提要一篇新闻故事,我认为这image_small我需要一个例子

  {code:200,错误:空,数据:{新闻:[{news_id:8086,标题 :门票的在市场上销售的年度最佳球员,口号:事件举行星期日3月25日在假日酒店,巴恩斯利,内容:门票年的首届EVO-STIK NPL播放器现在事件公开发售给公众。\ r \ñ\ r \ nPriced在\ u00a335,门票包括三道菜,再加上从著名男高音马丁·特里和演讲嘉宾弗雷德·爱娱乐。\ r \ñ\ r \ nAwards在活动包括年度最佳球员和年轻球员的各部门,以及今年的分区球队,球迷\ u2019年度最佳球员和联赛的优异奖。\ r \ñ\ r \ n要购买机票发送支付支票\ u00a335到\ u201cNorthern premier联赛\ u201d艾伦Ogley,21 Ibberson大道,Mapplewell,巴恩斯利,S75 6BJ。请包括返回地址的门票将被发送到,和的名字那些参加\ r \ñ\ r \ n有关详细信息,电子邮件汤姆Snee或联系活动组织者艾伦·Ogley上07747 576 415 \ r \ñ\ r \ n注意:俱乐部仍可以订购门票通过电子邮件发送安吉弗斯 - ,场地简单说明您需要多少张门票,您将得到相应发票\​​ r \ñ\ r \ nAddress:巴恩斯利路,Dodworth的,巴恩斯利S75 3JT(就在M1公路37),创造:2012- 02-29 12:00:00,CATEGORY_ID:1,IMG:4539337,类别:常规,全名:汤姆Snee","user_id":"170458","image_user_id":"170458","image_file":"1330519210_0.jpg","image_width":"600","image_height":"848","sticky":"1","tagged_division_id":null,"tagged_team_id":null,"tagged_division":null,"tagged_team":null,"full_image":"http:\/\/images.pitchero.com\/up\/league-news-default-full.png","image_primary":"http:\/\/images.pitchero.com\/ui\/170458\/lnl_1330519210_0.jpg","image_secondary":"http:\/\/images.pitchero.com\/ui\/170458\/lns_1330519210_0.jpg","image_original":"http:\/\/images.pitchero.com\/ui\/170458\/1330519210_0.jpg","image_small":"http:\/\/images.pitchero.com\/ui\/170458\/sm_1330519210_0.jpg"}
 

和我的继承人code到目前为止

  JSON字符串= reader.readLine();





            //从请求响应实例化一个JSON对象
            JSONObject的OBJ =新的JSONObject(JSON);
            名单<字符串>项目=新的ArrayList<字符串>();


            JSONObject的objData = obj.getJSONObject(数据);

            JSONArray jArray = objData.getJSONArray(新闻);


            的for(int i = 0; I< jArray.length();我++)
            {JSONObject的oneObject = jArray.getJSONObject(我);
                items.add(oneObject.getString(标题));
                 Log.i(项目,项目);
            }

            setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.single_item,项目));
            ListView控件列表= getListView();
            list.setTextFilterEnabled(真正的);


            list.setOnItemClickListener(新OnItemClickListener(){

                公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                        长ARG3){
                    // TODO自动生成方法存根
                    Toast.makeText(getApplicationContext(),((TextView的)ARG1).getText(),1000).show();
                }



            });


        }赶上(例外五){
            //在生产code处理任何错误,赶上个别例外
            e.printStackTrace();
        }
 

解决方案

我假设你要加载的图像在您的列表视图每一个项目,如果是这种情况,那么你可以实现任何解决方案,从如下:

  1. <一个href="http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview">Android - 如何做我做的一个图像延迟加载ListView中
  2. 的Andr​​oid通用通过诺斯特拉的图像装载机。

Hi i have a list i have it filling in a title from a Json Feed and now i want to add an image to it heres my single_list layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"

    android:padding="6dip">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true" />

<TextView 
    android:orientation="vertical"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    android:background="@drawable/ic_gr_arrow"
    android:id="@+id/textView1" 
   android:textColor="@color/blue"
   android:padding="20dp"
   android:textSize="10dp" 

     />


</RelativeLayout>

heres the an example of one news story from the JSON Feed i think it image_small i need

{"code":200,"error":null,"data":{"news":[{"news_id":"8086","title":"Tickets for Player of the Year award on general sale","tagline":"Event to be held Sunday 25th March at Holiday Inn, Barnsley","content":"Tickets for the inaugural Evo-Stik NPL Player of the Year event are now on sale to the general public.\r\n\r\nPriced at \u00a335, tickets include a three course meal, plus entertainment from renowned tenor Martin Toal and guest speaker Fred Eyre.\r\n\r\nAwards at the event include the Player and Young Player of the Year for each division, as well as divisional teams of the year, the Fans\u2019 Player of the Year and a League Merit award.\r\n\r\nTo purchase your ticket, send a cheque for \u00a335 payable to \u201cNorthern Premier League\u201d to Alan Ogley, 21 Ibberson Avenue, Mapplewell, Barnsley, S75 6BJ. Please include a return address for the tickets to be sent to, and the names of those attending. \r\n\r\nFor more information, e-mail Tom Snee or contact Event organiser Alan Ogley on 07747 576 415\r\n\r\nNote: Clubs can still order tickets by e-mailing Angie Firth - simply state how many tickets you require and you will be invoiced accordingly.\r\n\r\nAddress of venue: Barnsley Road, Dodworth, Barnsley S75 3JT (just off Junction 37 of M1)","created":"2012-02-29 12:00:00","category_id":"1","img":"4539337","category":"General","fullname":"Tom Snee","user_id":"170458","image_user_id":"170458","image_file":"1330519210_0.jpg","image_width":"600","image_height":"848","sticky":"1","tagged_division_id":null,"tagged_team_id":null,"tagged_division":null,"tagged_team":null,"full_image":"http:\/\/images.pitchero.com\/up\/league-news-default-full.png","image_primary":"http:\/\/images.pitchero.com\/ui\/170458\/lnl_1330519210_0.jpg","image_secondary":"http:\/\/images.pitchero.com\/ui\/170458\/lns_1330519210_0.jpg","image_original":"http:\/\/images.pitchero.com\/ui\/170458\/1330519210_0.jpg","image_small":"http:\/\/images.pitchero.com\/ui\/170458\/sm_1330519210_0.jpg"}

and heres my code so far

String json = reader.readLine();





            // Instantiate a JSON object from the request response
            JSONObject obj = new JSONObject(json);
            List<String> items = new ArrayList<String>();


            JSONObject objData = obj.getJSONObject("data");

            JSONArray jArray = objData.getJSONArray("news");


            for (int i=0; i < jArray.length(); i++)
            {    JSONObject oneObject = jArray.getJSONObject(i);
                items.add(oneObject.getString("title"));
                 Log.i("items", "items");
            }

            setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
            ListView list = getListView();
            list.setTextFilterEnabled(true);


            list.setOnItemClickListener(new OnItemClickListener(){

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
                }



            });


        } catch(Exception e){
            // In your production code handle any errors and catch the individual exceptions
            e.printStackTrace();
        }

解决方案

I assume you want to load Image for every items in your listview, if this is the case then you can implement any solution from below:

  1. Android - How do I do a lazy load of images in ListView
  2. Android Universal Image Loader by Nostra.

这篇关于从一个JSON的Andr​​oid喂食饲料的图像到一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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