【问题标题】:How to display images stored in database as a blob, in the Listview in Android?如何在 Android 的 Listview 中将存储在数据库中的图像显示为 blob?
【发布时间】:2011-05-03 06:10:36
【问题描述】:

我的问题是,我正在创建一个小测验,以便我希望图像位于顶部,其答案位于其下方。这一切都是在一个新活动中完成的,我想在其中显示测验的答案。大约有 40 个问题,每个问题都有一张图片。因此,我尝试使用 HashMap 如下:-

   ListView lv = (ListView)findViewById(R.id.list1);
   String[] from = new String[] {"ques","ans"};
   int[] to = new int[] {R.id.ques, R.id.ans};

           // prepare the list of all records
    List<HashMap<String,Bitmap>> fillMaps = new ArrayList<HashMap<String,Bitmap>>();
     Cursor c1 = db.getQues(4);
     byte[] bb = c1.getBlob(0);
     Bitmap image = BitmapFactory.decodeByteArray(bb, 0, bb.length);
     //Cursor c2 = db.getAns(4);
    // String ans1 ="Ans"+") "+c2.getString(0);
     HashMap<String,Bitmap> map = new HashMap<String, Bitmap>();
    // HashMap<String,String> map1 = new HashMap<String, String>();
     map.put("ques",image);
    // map1.put("ans",ans1);
     fillMaps.add(map);      

     SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.itemsign, from, to);
     lv.setAdapter(adapter);

但我找不到正确实现它的方法。此代码不起作用。它只是显示一个空白页。因此,任何帮助将不胜感激。由于我是android新手,所以请在解释时更详细。

【问题讨论】:

    标签: android database image listview


    【解决方案1】:

    嗯,我不认为 blob 是你的问题。这是SimpleAdapter 构造函数的javadoc:

    public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

    自:API 级别 1 构造函数 参数

    • context 与此 SimpleAdapter 关联的 View 正在运行的上下文
    • data 地图列表。列表中的每个条目对应于列表中的一行。 Maps 包含每一行的数据,并应包含“from”中指定的所有条目
    • resource 视图布局的资源标识符,用于定义此列表项的视图。布局文件应至少包含“to”中定义的那些命名视图
    • 来自将添加到与每个项目关联的地图的列名列表。
    • to 应在“from”参数中显示列的视图。 这些都应该是 TextView。 此列表中的前 N ​​个视图被赋予 from 参数中前 N 列的值。

    你不能在SimpleAdapter 中使用Bitmaps,这太简单了:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多