0

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

4

1 回答 1

4

这里的问题是您将 a url(属于 String 类型)直接设置为imageview并请求将SimpleAdapter其绑定到imageview. 我建议您使用下面的代码中的其他适配器。因为SimpleAdapterSimpleCursorAdapter主要用于当你有数据时local(sqlite) database,使内容直接反映在listview. 但是在这里你从服务器获取数据。所以在这里你去吧。

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;
    }

}

您通过以下方式将上述适配器设置为您listview的。从网络加载数据后包括以下代码。要从服务器加载数据,请确保您不在 UI 线程上运行网络操作。为此,您可以使用 AsyncTask、Handlers、Service 等,如果您使用 AsyncTask,请在onPostExecute().

CustomListAdapter adapter = new CustomListAdapter(YourActivity.this, dataArrayList);
listView.setAdapter(adapter); 

希望这会有所帮助,顺便说一句,对于延迟回答感到抱歉。

于 2013-04-18T03:50:19.497 回答