2015-04-29 106 views
2

雖然位圖似乎被正確提取,但變量'userBitmap'將保持爲空。然而,在我的平板電腦上向上或向下滾動時,新的列表行將包含這些圖片,但它們都是一樣的和錯誤的。真的,真的很困惑。我已經嘗試了許多不同的方法從網上獲取圖像。任何幫助是極大的讚賞。從用於ListView的自定義適配器中的URL加載圖像(Android Studio)

我的自定義適配器:

public class MessagesArrayAdapter extends ArrayAdapter<ChatData> 
{ 
    Bitmap userBitmap; 

public MessageArrayAdapter(Context context, List<ChatData> objects) 
{ 
    super(context, 0, objects); 
} 

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ChatCell chatCell = new ChatCell(); 

    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    convertView = inflater.inflate(R.layout.cell_chat, parent, false); 

    chatCell.usernameTextView = (TextView) convertView.findViewById(R.id.usernameTextView); 
    chatCell.messageTextView = (TextView) convertView.findViewById(R.id.messageTextView); 
    chatCell.userImageView = (ImageView) convertView.findViewById(R.id.userImageView); 

    ChatData chatData = getItem(position); 

    // Get user image from web 
    new loadImageAsync().execute(chatData.avatarURL); 

    chatCell.userImageView.setImageBitmap(userBitmap); 
    chatCell.usernameTextView.setText(chatData.username); 
    chatCell.messageTextView.setText(chatData.message); 

    return convertView; 
} 



private static class ChatCell 
{ 
    TextView usernameTextView; 
    TextView messageTextView; 
    ImageView userImageView; 
} 

private class loadImageAsync extends AsyncTask<String, Void, Double>{ 
    @Override 
    protected Double doInBackground(String... params) { 

     userBitmap = loadImage(params[0]); 


     return null; 
    } 
} 


public Bitmap loadImage(String str) { 

    InputStream instream = null; 
    try { 
     HttpGet httpRequest = new HttpGet(URI.create(str)); 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 
     HttpEntity entity = response.getEntity(); 
     BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
     instream = bufHttpEntity.getContent(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(instream); 

     return myBitmap; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     //close input 
     if (instream != null) { 
      try { 
       instream.close(); 
      } catch (IOException ioex) { 
       // Handle error 
      } 
     } 
    } 

} 

}

+2

我會使用[畢加索](http://square.github.io/picasso/)並過上幸福的生活。 – Emmanuel

回答

6

爲您解決問題最簡單的方法是使用一些庫,例如Picasso。它不僅更易於使用,並使代碼更易於閱讀,而且還可防止出現OutOfMemory Exceptions(圖像太大)時。載入圖像然後是一行的問題:

Picasso.with(context) 
.load(urlOfYourImage) 
.resize(50, 50) // here you resize your image to whatever width and height you like 
.into(imageView) 
+0

哦,我的。這是給我的。 – user2892437

+0

希望這有助於。簡化。 ;) – Jerry

+0

一切工作在一行代碼中。不能相信它。你認爲我應該仍然使用異步? – user2892437

0

我想你遇到了兩個問題在這裏:

1)查看回收 - 我是新來的Android自己,這在使用列表視圖時非常頻繁出現,當列表視圖的某些部分移出屏幕時,它們在滾動時可能會出錯。我個人遇到了按鈕狀態在上下滾動時出錯的問題。 This tutorial回顧了Recycling的更多細節,並在View Holder私人課程中爲我工作提供了一個解決方案。我相信SO的其他部分也會談論這一點。

2)圖像空 - 這是肯定通過爲空,或只是沒有出現在列表視圖?如果前者,LoadImage()中的某個步驟不能正常工作。如果是後者,則可能需要在設置圖像後嘗試調用notifyDataSetChanged(),以告知主線程圖像已準備好繪製。像下面的東西(在你的AsyncTask):

@Override 
protected void onPostExecute() { 
     notifyDataSetChanged(); 
    } 
} 
+0

感謝您的評論。 notifyDataSetChange();幫助。現在,所有的觀點都有圖片。但是,它們都是一樣的,每秒都會改變(而文本視圖保持相同/正確)。我認爲這與在getView()之外定義的'userBitmap'有關。有沒有更好的方法來做到這一點? – user2892437

相關問題