0
我想在後臺加載圖像來填充ListView中的圖像。 如果我在後臺加載圖像並將其加載到不在ListView中的圖像,它可以正常工作。當我嘗試加載位於ListView中的ImageView中的位圖時,它會崩潰。當我在後臺加載圖像,它工作正常,崩潰時,我加載到一個ListView裏面的ImageView
這是我做的:
- 創造了的AsyncTask
- 超越控制的子類的GetView的標誌性adpter。
- 在
getView()
,得到的ImageView的圖像加載到通過(ImageView) findViewById(R.id.icon)
,其中的圖標是new MyBackground((ImageView) findViewById(R.id.icon)).execute()
在爲ListView - 啓動後臺處理XML文件中的ID時,它會嘗試加載位圖崩潰到
(ImageView) findViewById(R.id.icon)
現在,如果我嘗試將背景圖像加載到ImageView的,是不是在正常工作清單,即MyBackground((ImageView) findViewById(R.id.MyImage)).execute()
代碼:
class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, LinkName);
this.context=context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.row, null);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(LinkName.get(position));
ImageView icon=(ImageView)row.findViewById(R.id.icon);
icon.setImageResource(R.drawable.icon);
//new MyBackground((ImageView) findViewById(R.id.icon)).execute();
// new MyBackground((ImageView) findViewById(R.id.myimage)).execute();
return(row);
}
}
class MyBackground extends AsyncTask<Void, Void, Bitmap>
{
int p1;
String p2;
ImageView myimage;
// @Override
public MyBackground(ImageView in)
{
// super.MyBackground
myimage=in;
}
@Override
protected Bitmap doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
try {
String uri = "http://www.besttechsolutions.biz/icon.png";
HttpGet request = new HttpGet(uri);
HttpResponse response = client.execute(request);
return BitmapFactory.decodeStream(response.getEntity().getContent());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap image) {
if(image == null){
Log.d("ted", "could not download image");
// Toast.makeText(Main.this, "Download failed", Toast.LENGTH_LONG).show();
}
else{
// losd into ListView
// this will crash if image is in the list vue
myimage.setImageBitmap(image);
}
}
};