2016-06-18 70 views
0

我編寫的程序從json獲取數據(文本)並在列表視圖中顯示,我編寫代碼及其工作,但現在我用json獲取圖像的url,但是我不知道如何顯示在圖像視圖列表視圖中的圖像, 請幫我如何在android列表視圖中顯示帶有json url的圖像

這裏是我的代碼

MainActivity:

public class MainActivity extends ListActivity { 

private ProgressDialog pd; 
JSONParser jParser=new JSONParser(); 
ArrayList<HashMap<String,String>> P; 
JSONArray s=null; 
private final String url="http://192.168.1.4:81/upload/travel.php"; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    P = new ArrayList<>(); 
    new travel().execute(); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    String id_code=((TextView)v.findViewById(R.id.id_code)).getText().toString(); 
    Intent in=new Intent(MainActivity.this,ADD.class); 
    in.putExtra("id",id_code); 
    startActivity(in); 

} 

class travel extends AsyncTask<String,Void,String>{ 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pd=new ProgressDialog(MainActivity.this); 
     pd.setMessage("login"); 
     pd.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     List<NameValuePair> parms=new ArrayList<>(); 
     JSONObject json=jParser.makeHTTPRequest(url,"GET"); 

     try { 
      int t=json.getInt("t"); 
      if(t==1){ 
       s=json.getJSONArray("travel"); 
       for(int i=0;i<s.length();i++){ 
        JSONObject c=s.getJSONObject(i); 
        String id=c.getString("id"); 
        String companyname=c.getString("companyname"); 
        String cod=c.getString("cod"); 
        String bign=c.getString("bign"); 
        String stop=c.getString("stop"); 
        String date=c.getString("date"); 
        String time=c.getString("time"); 
        String price=c.getString("price"); 
        String url_image=c.getString("url_image"); 

        HashMap<String,String>map=new HashMap<String,String>(); 
        map.put("id",id); 
        map.put("companyname",companyname); 
        map.put("cod",cod); 
        map.put("bign",bign); 
        map.put("stop",stop); 
        map.put("date",date); 
        map.put("time",time); 
        map.put("price",price); 
        map.put("url_image",url_image); 

        P.add(map); 

       } 
      }else { 
       Toast.makeText(MainActivity.this,"No Data Found",Toast.LENGTH_SHORT).show(); 
      } 


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

     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     pd.dismiss(); 
     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       ListAdapter adapter = new SimpleAdapter(MainActivity.this, P, R.layout.item_list, 
         new String[]{"url_image","companyname", "cod", "bign", "stop", "date", "time", "price"}, 
         new int[]{R.id.id_code,R.id.companyname, R.id.cod, R.id.bign, R.id.stop, R.id.date, R.id.time1, R.id.price}); 


       setListAdapter(adapter); 
      } 
     }); 

    } 

} 

}

+0

你應該嘗試畢加索或滑行庫 – ManishMenaria

回答

0

以顯示URL圖像到ImageView的使用這個類:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 
    } 

爲了使用它只是在你的代碼中使用此命令:

new DownloadImageTask(imageView).execute(imageLinkURL); 
+0

我應該使用此代碼「new DownloadImageTask(imageView).execute(imageLinkURL); 「在oncreate函數中就是這樣嗎? – alireza

+0

不是,獲取圖片的URL後使用它,插入你想要顯示圖片的地方的imageView和你在執行中收到的URL,它會異步下載圖片從網址,當它完成它會顯示它在imageview –

+0

我使用我的imageview,但它顯示我的錯誤,我的代碼=> new DownloadImageTask(R.id.imageView).execute(url); – alireza

1

我會建議你用最好的圖書館的圖像加載異步 PICASSO LIBRARY LINK

現在你應該寫代碼

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
相關問題