2016-04-28 10 views
-1

ファイルjsonからイメージURLを読み込み、リストビューでイメージを表示する際に問題があります。listview内のファイルjson内のURLからイメージをロードするには?

ファイルJSON:: { "profile_pic": "https://my2-cdn.pgimgs.com/agent/432446/APHO.84210479.V120B.jpg"、 "エージェント名": "whisルー" 私はhashmap.This私のコードでユニバーサルイメージ・ローダープットを使用する方法がわかりません、 "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"、 "エージェント名" : "SP Whong"、 "agent_city": "34"、 " agent_company ": "王朝ホーム不動産"、 "agent_phone": "60 17 450 2223" } ]

MainActivity.java:HashMapの中のlibピカソの使用方法

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

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

あなたはどのような問題がありますか? –

+0

いくつかの詳細を提供してください何がメインプロムですか?あなたが欲しいもの? –

+0

このサンプルコードを確認してくださいhttps://github.com/arshadkazmi42/ListView-Using-Volley – Arshad

答えて

0

ファイルJSONからURLです。 ユニバーサル画像ローダーが簡単です。 依存関係を追加するだけで、1行のコードを使用するだけでイメージを表示してキャッシュすることもできます。 http://square.github.io/picasso/

関連する問題