开发者

How to display images stored in database as a blob, in the Listview in Android?

开发者 https://www.devze.com 2023-03-02 01:30 出处:网络
My question here is that, I am creating a small quiz such that I want an image at the top and its answer right below it. This is all done in a new activity where I want to show the answers for the qui

My question here is that, I am creating a small quiz such that I want an image at the top and its answer right below it. This is all done in a new activity where I want to show the answers for the quiz. There are about 40 questions each with an image. Hence, I tried using HashMap as follows:-

   Lis开发者_运维知识库tView 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);

But I couldn't find a way to correctly implement it. This code does not work. It just shows a blank page. So, any help will greatly be appreciated. As I am new to android so please be more detailed while explaining.


Well, I don't think that the blob is your problem. Here is the javadoc for the SimpleAdapter constructor:

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.

You can't use Bitmaps in a SimpleAdapter, it's too simple for that :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号