android - 如何在 Android 的 Listview 中以 blob 形式显示存储在数据库中的图像?

标签 android database image listview

我的问题是,我正在创建一个小测验,因此我希望图像位于顶部,答案位于其下方。这一切都是在我想显示测验答案的新 Activity 中完成的。大约有 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 的新手,所以请在解释时更详细。

最佳答案

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

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

Since: API Level 1 Constructor Parameters

  • context The context where the View associated with this SimpleAdapter is running
  • data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
  • resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
  • from A list of column names that will be added to the Map associated with each item.
  • to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

你不能在 SimpleAdapter 中使用 Bitmap,它太简单了:)

关于android - 如何在 Android 的 Listview 中以 blob 形式显示存储在数据库中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5865724/

相关文章:

java - 访问其他应用内部数据

mongodb - 使用 Mongo 的 "Object ID"作为其唯一标识符可以吗?如果是这样,如何将其转换为字符串并按字符串查找?

c# - 在数据库或数据表中查询一列中的重复记录

image - Flutter 图像裁剪和变换。

php - PHP 中高效的 JPEG 图像大小调整

android - 如何检测 Android ListView 滚动停止?

android - 从SD卡读取并插入SQL表非常慢

java - 奇怪的程序流程

mysql - 将复选框值从 jTable 保存到数据库中

image - 不是JPEG文件:开头为0x0a 0x4c错误