问题
My problem is to show an imagen into a listview readed from JSON, I read a lot of posts about the issue but no one solved it completely. I cathc the url with String imagen = e.getString("img_preview_1"); then added it into
HashMap<String, String> map = new HashMap<String, String>();
map.put("imagen", imagen);
This is my adapter with a String and the image
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.activity_videos_categoria,
new String[] { "title", "imagen" },
new int[] { R.id.from , R.id.imageView1});
setListAdapter(adapter);
I tried chenging the type in the Map asn Object but still same problem
The error in LogCat is resolveUri failed on bad bitmap
Thank you
回答1:
The problem here is you are setting a url(Which is of String type) directly to the imageview and requesting the SimpleAdapter to bind it to the imageview. I would suggest you to use other adapters as in my below code. Because SimpleAdapter and SimpleCursorAdapter are mostly used when you have data in the local(sqlite) database to make the content reflected directly on the listview. But here you get the data from the server. So here you go with it.
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public CustomListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null); //This should be your row layout
TextView titleTextView = (TextView)vi.findViewById(R.id.title); // title
ImageView thumb_image=(ImageView)vi.findViewById(R.id.imageview1); //image
HashMap<String, String> localhash = new HashMap<String, String>();
localhash = data.get(position);
String currenttitle = localhash.get("title");
String imagepath = localhash.get("imagen");
titleTextView.setText(currenttitle);
if(!imagepath.equals(""))
{
imageLoader.DisplayImage(imagepath , thumb_image);
}
return vi;
}
}
You set the above adapter to your listview in the below way. Include the below code after you load the data from the network. To load the data from the server, make sure you don't run the network operations on UI thread. For this you can make use of AsyncTask,Handlers, Service etc., If you use AsyncTask, include the below lines in onPostExecute().
CustomListAdapter adapter = new CustomListAdapter(YourActivity.this, dataArrayList);
listView.setAdapter(adapter);
Hope this helps and btw sorry for the delay in answering.
来源:https://stackoverflow.com/questions/16059110/resolveuri-failed-on-bad-bitmap-in-listview-from-json