2014-11-24 39 views
-2

我想顯示的默認PIC當結果爲空,這裏是我的嘗試:如何在Android中設置列表中的默認圖片?

getBitmapFromURL和使用它的類:

public static Bitmap getBitmapFromURL(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      connection.disconnect(); 
      return myBitmap; 
     } catch (IOException e) { 
//   tasvirdimg.setImageResource(R.drawable.p3_books); 
      e.printStackTrace(); 
      return null; 
     } 
    } 
} 
class RetrievePic extends AsyncTask<String, String, Bitmap>{ 

    @Override 
    protected Bitmap doInBackground(String... urls) { 
     String url = urls[0]; 
     return JSONCommands.getBitmapFromURL(url); 
    } 

} 

執行類ListAdapter

//  tasvirlimg.setImageResource(R.drawable.p3_books); 
     RetrievePic retPic = new RetrievePic(); 
     retPic.execute(JSONCommands.firstURL + MainActivity.books_array.get(position).tasvir); 
     Bitmap img = null; 
     try { 
      img = retPic.get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      tasvirlimg.setImageResource(R.drawable.p3_books); 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      tasvirlimg.setImageResource(R.drawable.p3_books); 
      e.printStackTrace(); 
     } 

     tasvirlimg.setImageBitmap(img); 

當書籍圖片爲空時,它不顯示任何東西,但我想顯示默認圖片來源R.drawable.p3_books。請幫忙。謝謝。

+0

您可以使用[Universal Image Loader](https://github.com/nostra13/Android-Universal-Image-Loader)或[Picasso](http://square.github.io/picasso/)下載你的圖像,並設置一個默認圖像到你的佔位符。 – 2014-11-24 23:08:21

+0

我想知道人們來到這裏,編輯問題等得到星星!請求只是幫助我的概率! – 2014-11-24 23:09:38

+0

@MohammadRahchamani坦克你,但你可以幫我用我自己的代碼?! – 2014-11-24 23:10:45

回答

0

你可以試試這個:

if(img == null) 
{ 
    tasvirlimg.setImageDrawable(R.drawable.p3_books); 
} 
else 
{ 
    tasvirlimg.setImageBitmap(img); 
} 
+0

它有錯誤親愛的! tasvirlimg是一個ImageView,並沒有setImageBitmap類。 – 2014-11-24 23:16:03

+0

嘗試tasvirling.setImageDrawable(R.drawable.p3_books);而不是setImageBitmap(..)。如果這不適合你。你可以嘗試傑弗里布拉特曼發佈的例子,這也應該工作。 – Xry 2014-11-24 23:19:53

+0

你看到我的代碼親愛的了嗎?因爲R.drawable.p3_books是資源我試過tasvirdimg.setImageResource(R.drawable.p3_books);謝謝 – 2014-11-24 23:37:37

0

爲什麼不設置項目的默認PIC在XML,然後默認PIC會被替換時,結果是不爲空

<ImageView 
    ... 
     android:src="@drawable/p3_books" 
     /> 

編輯:

 class RetrievePic extends AsyncTask<String, String, Bitmap>{ 

    final image = (ImageView) findViewById(R.id.limg_tasvir); 

      @Override 
      protected Bitmap doInBackground(String... urls) { 
       String url = urls[0]; 
       return JSONCommands.getBitmapFromURL(url); 
      } 

      @Override 
        protected void onPreExecute() { 
         runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
         image.setBackgroundResource(R.drawable.p3_books); 
           } 
          }); 

     } 
} 
+0

這是我的ImageView: 你可以看到我設置了這個選項,但它不起作用! – 2014-11-24 23:18:49

+0

runOnUiThread和tasvirdimg是2錯誤。我在另一個類中有tasvirdimg,我不知道runOnUiThread。 – 2014-11-24 23:31:45

+0

@NiloofarHakkaki - 我只是注意到,並將其更改爲tasvirlimg - 如果runonuithread給出錯誤,將其取出 - 保護無效onPreExecute(){ta​​svirlimg.setBackgroundResource(R.drawable.p3_books); } – Wildroid 2014-11-24 23:32:43

0

你想要做的是在你實現你的適配器的getView()方法,somethi ng這樣,

... 
final imageView = ...; 
imageView.setImageBitmap(defaultBitmap); 
new AsyncTask<Void,Void,Bitmap>() { 
    public Bitmap doInBackground(Void... params) { 
    // get bitmap from `net, or return null if failure 
    } 

    public void onPostExecute(Bitmap result) { 
    if (result != null) { 
     imageView.setBitmap(result); 
    } 
    } 
}.execute(); 

這樣,默認設置。如果您碰巧遇到網絡結果,請更改默認值。

+0

我真的無法理解:)))請用示例解釋一下15聲望來理解!!!謝謝 – 2014-11-24 23:33:20

相關問題