長名稱的道歉。Android - 從getView函數中設置ImageView適配器內的ImageView的源代碼無法正常工作
我正在構建一個活動包含自定義listView的Android應用程序。在我的自定義listView的佈局中,我有一個圖像視圖和三個textView。我正在從數據庫中提取數據,因爲它們是準確的,所以我對行數據沒有任何問題。但是,我有顯示ImageViews的問題。
我做什麼來填充imageView是我從數據庫中獲取imagePath,解析該URL(僅限本地服務器),然後設置ImageViews位圖。
在我的customListView類的getView
函數期間,我創建了一個處理數據庫中的url的線程,然後解析它,然後將其設置爲ImageView的位圖。
這裏是我的getView
功能:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.nominees_list_layout, null);
holder = new ViewHolder();
holder.firstName = (TextView) convertView.findViewById(R.id.nomineesListFirstName);
holder.middleName = (TextView) convertView.findViewById(R.id.nomineesListMiddleName);
holder.lastName = (TextView) convertView.findViewById(R.id.nomineesListLastName);
holder.photo = (ImageView) convertView.findViewById(R.id.nomineesListCandidatePhoto);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.firstName.setText(listData.get(position).getFirst_name());
holder.middleName.setText(listData.get(position).getMiddle_name());
holder.lastName.setText(listData.get(position).getLast_name());
new setDisplayPhoto().execute(listData.get(position).getPicture_path());
return convertView;
}
正如你所看到的,我初始化視圖組件然後根據從數據庫中獲取的listData設置它們的值。顯示的數據都是正確的。然後,我爲imageView所做的工作是創建一個AsyncTask並在那裏提供數據。
這裏是我的AsyncTask:
private class setDisplayPhoto extends AsyncTask<String, Void, String> {
String toastMessage = "Downloading nominees photo finished.";
Bitmap mIcon_val;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
Log.d("","doInBackGround Started");
String photo_url_str = "http://192.168.0.10:8888/server/path/here/"
+ params[0];
try {
Log.d("","photo_url_str = " + photo_url_str);
newurl = new URL(photo_url_str);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
holder.photo.setImageBitmap(mIcon_val);
} catch (IOException e) {
e.printStackTrace();
}
return "Executed!";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
}
然而,這些照片不會出現,如果他們這樣做,有時這些照片是混亂的。
有人可以幫助我如何正確設置imageView的圖像源期間customViewView適配器的getView函數從本地服務器獲取源時?感謝您的任何幫助。
網絡操作始終在輔助線程運行....所以,如果你給一個URL路徑,並嘗試下載它會下載一些其他線程,因此,最好是有自己的AsyncTask嘗試下載圖像在下載它之後回到你的適配器。取決於位置的可視性顯示圖像。 –
我試圖取消實際下載文件,因爲我希望系統能夠通過從服務器獲取文件而無需下載它而變得靈活。 – Razgriz
在你的情況下,保持listitems的位置是非常重要的...原因listview是基於回收的概念..所以,如果你通過imageview對象,無論哪個位置是可見的,你的線程將嘗試將圖像設置爲該圖像視圖 –