【问题标题】:resolveUri failed on bad bitmap in ListView from JSONresolveUri 在来自 JSON 的 ListView 中的错误位图上失败
【发布时间】:2013-04-17 11:44:53
【问题描述】:

我的问题是将图像显示到从 JSON 读取的列表视图中,我阅读了很多关于该问题的帖子,但没有人完全解决它。我用String imagen = e.getString("img_preview_1"); 将网址添加到

HashMap<String, String> map = new HashMap<String, String>();
map.put("imagen", imagen);

这是我的带有字符串和图像的适配器

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

我尝试在 Map asn Object 中更改类型,但仍然是同样的问题

LogCat 中的错误是 resolveUri failed on bad bitmap

谢谢

【问题讨论】:

  • 你的 Uri 怎么样?
  • uri: xxx/yyyy/zzzz.png 你是这个意思吗?
  • img_preview_1 的值是多少?
  • @PareshMayani JSON 中的值为 "img_preview_1":"http:\\www.yyy.zzzz.png"
  • 你在哪里使用过map

标签: android android-layout android-listview


【解决方案1】:

这里的问题是您将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); 

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 1970-01-01
  • 2020-06-30
  • 2014-07-16
  • 1970-01-01
相关资源
最近更新 更多