2
My Listview is crashing enough on the scroll, I tried to implement Viewholder to the Adapter but did not solve, could help me?
Adapter:
public class Adapter extends SimpleAdapter {
private Context context;
public LayoutInflater inflater = null;
public Adapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ViewHolder viewHolder;
HashMap<String, Object> data = (HashMap<String, Object>) super.getItem(position);
if(view == null){
view = inflater.inflate(R.layout.clientes, parent, false);
viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
viewHolder.title = (TextView)view.findViewById(R.id.title);
viewHolder.content = (TextView)view.findViewById(R.id.content);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
try {
viewHolder.thumbnail.setImageBitmap( new DownloadTask().execute(data.get("thumbnail").toString()).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
viewHolder.title.setText(data.get("title").toString());
viewHolder.content.setText(data.get("content").toString());
return view;
}
static class ViewHolder{
ImageView thumbnail;
TextView title;
TextView content;
}
}
Downloadtask:
public class DownloadTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap image = null;
try {
image = BitmapFactory.decodeStream( new URL(params[0]).openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
}
Already tested with the line of
new DownloadTask()
commented?– Luídne
If possible add the implementation of Downloadtask. At least its main part.
– Tin Megali
@Luídne, yes, without setting an image Listview works perfectly, the crashes occur only when using the Downloadtask class().
– Rafael Alexandre