在ANdroid中的ListBox中附加相机图像

时间:2013-10-01 20:09:56

标签: android android-listview

在按下相机按钮onActivityResult后调用我的代码(作为意图的结果)。在这个方法中,我编写了代码来在listView中添加捕获的图像。我想要的是,当我第二次捕捉图像时,listView中的第一张图像不应该消失。如果我已经拍摄了三次图像,则应该有三个listview个项目。我的代码每次捕获图像时只显示一个项目。我已经尝试了很多,但无法弄明白。 感谢。

ArrayList<HashMap<String, Object>> hashMapListForListView=new ArrayList<HashMap<String,Object>>();     

protected void onActivityResult(int requestCode, int resultCode, Intent data) {



        super.onActivityResult(requestCode, resultCode, data);


        if(resultCode == Activity.RESULT_OK )
        {      
  if (requestCode == take_image) {

        //get image
              final Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  

            Viewimage.setImageBitmap(thumbnail);

              Adapter=new SimpleAdapter(this,hashMapListForListView,R.layout.imageview2,new String[]{"image"}, new int[]{R.id.imageView2});


            HashMap<String, Object>temp11=new HashMap<String, Object>();
            //Drawable d = new BitmapDrawable(getResources(),thumbnail);
            temp11.put("image",thumbnail);

            hashMapListForListView.add(temp11);
            listviewattachment.setAdapter(Adapter);
            Adapter.setViewBinder(new MyViewBinder());
            Adapter.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:0)

这是因为您每次都在onActivityResult()创建适配器。第二件事是,不需要使用setAdapter()一次又一次地设置适配器,但只需要notifyDataSetChanged()

<强>更新

正如您在评论中所说,您想要从列表中删除项目,请尝试:

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);