如何从listview中的文件json中的url加载图像?

时间:2016-04-28 10:42:00

标签: android listview

我有问题从文件json读取图像url并在listview中显示图像。我不知道如何使用通用图片加载器放入hashmap。这是我的代码:

档案Json:  {         " profile_pic":" https://my2-cdn.pgimgs.com/agent/432446/APHO.84210479.V120B.jpg",         " agent_name":" whis lew",         " agent_city":" 19",         " agent_company":" Novaland Realty",         " agent_phone":" +60 12 301 8966"     },     {         " profile_pic":" https://my1-cdn.pgimgs.com/agent/43067/APHO.2785412.V120B.jpg",         " agent_name":" SP Whong",         " agent_city":" 34",         " agent_company":" Dynasty Home Realty",         " agent_phone":" +60 17 450 2223"     } ]

MainActivity.java:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        oslist = new ArrayList<HashMap<String, Object>>();

        Btngetdata = (Button)findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new JsonParses().execute();

            }
        });
    }

    private class JsonParses extends AsyncTask<String, String, String> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pic = (ImageView) findViewById(R.id.pic);
            name = (TextView) findViewById(R.id.name);
            city = (TextView) findViewById(R.id.city);
            company = (TextView) findViewById(R.id.company);
            phone = (TextView) findViewById(R.id.phone);
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            //read file Json
            StringBuffer sb = new StringBuffer();
            BufferedReader br = null;

            try {
                br = new BufferedReader(new InputStreamReader(getAssets().open(file)));
                String temp;
                while ((temp = br.readLine()) != null)
                    sb.append(temp);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    br.close(); // stop reading
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            String myjsonstring = sb.toString();


            //JsonParser jParser = new JsonParser();

            // Getting JSON from URL
            //JSONObject json = jParser.getJsonfromfile(file,msg);
            return myjsonstring;
        }

        @Override
        protected void onPostExecute(String jObj) {
            pDialog.dismiss();
            try {
                // Getting JSON Array from URL
                android= new JSONArray(jObj);
                for (int i = 0; i < android.length(); i++) {
                    JSONObject c = android.getJSONObject(i);
                    // Storing  JSON item in a Variable
                    String pic = c.getString(TAG_pic);
                    String name = c.getString(TAG_name);
                    String city = c.getString(TAG_city);
                    String company = c.getString(TAG_company);
                    String phone = c.getString(TAG_phone);
                    // Adding value HashMap key => value
                    //HOW TO LOAD IMAGE IN LISTVIEW
                    HashMap<String, Object> map = new HashMap<String, Object>();

                    map.put(TAG_pic, pic);
                    map.put(TAG_name, name);
                    map.put(TAG_city, city);
                    map.put(TAG_company, company);
                    map.put(TAG_phone, phone);

                    oslist.add(map);
                    list = (ListView) findViewById(R.id.list);

                    ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                            R.layout.ist_v,
                            new String[]{TAG_pic, TAG_name, TAG_city,TAG_company,TAG_phone}, new int[]{
                            R.id.pic, R.id.name, R.id.city,R.id.company,R.id.phone});

                    list.setAdapter(adapter);


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

如何在HashMap中使用lib Picasso? TAG_pic是来自Json文件的URL

HashMap<String, Object> map = new HashMap<String, Object>();

                    map.put(TAG_pic, pic);
                    map.put(TAG_name, name);
                    map.put(TAG_city, city);
                    map.put(TAG_company, company);
                    map.put(TAG_phone, phone);

                    oslist.add(map);
                    list = (ListView) findViewById(R.id.list);

                    ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                            R.layout.ist_v,
                            new String[]{TAG_pic, TAG_name, TAG_city,TAG_company,TAG_phone}, new int[]{
                            R.id.pic, R.id.name, R.id.city,R.id.company,R.id.phone});

1 个答案:

答案 0 :(得分:0)

从json获取网址并使用 Picasso 加载图片。 它比通用图像加载器更简单。 只需添加依赖项并使用单行代码,您只需显示图像,甚至可以缓存它。 http://square.github.io/picasso/