2016-04-28 69 views
-1

我有问题从文件json中读取图像url并在listview中显示图像。我不知道如何使用通用图像装载机放于hashmap.This我的代码:如何从listview中的文件json中的URL加载图像?

文件JSON: { “profile_pic”: “https://my2-cdn.pgimgs.com/agent/432446/APHO.84210479.V120B.jpg”, “AGENT_NAME”: “卢WHIS” , “agent_city”: “19”, “agent_company”: “Novaland不动产”, “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“: ”皇朝家地产“, ”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(); 
      } 
     } 
    } 

如何使用LIB毕加索的HashMap ? TAG_pic是URL从文件的Json

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

你有什么问题? –

+0

请提供一些细节什么主要问题?你想要什么? –

+0

检查此示例代码https://github.com/arshadkazmi42/ListView-Using-Volley这可能会帮助你 – Arshad

回答

0

从JSON获取网址,并使用毕加索加载图像。 通用图像加载器更简单。 只需添加依赖关系并使用单行代码即可简单地显示图像并将其缓存。 http://square.github.io/picasso/