已删除的图像再次出现在ListView中

时间:2013-10-11 05:51:13

标签: android listview

在我的应用程序中,我将图像添加到listview,并在转换为字符串后将这些图像传输到servelet。但是从listview中删除的图像也会传输。它从listview中删除,但字符串正在转移到servelet。这很烦人。我无法在BaseAdapter中添加这些行。

                 Bitmap image=(Bitmap)getItem(position);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
            image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            //encode image
            byte[] b = bytes.toByteArray();
             encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
    //encodedImageString is sent to servlet, it's showing deleted image string also

以下是BaseAdapter的完整代码

    public class MyAdapter extends BaseAdapter {
      ArrayList<HashMap<String, String>> imageHashMap=new ArrayList<HashMap<String,String>>();
        private LayoutInflater mInflater;

        public MyAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        public int getCount() {
           // return myItems.size();
            return images.size();
        }

        public Bitmap getItem(int position) {
            return images.get(position);
            //return myItems.get(position);
        }

        public long getItemId(int position) {
           return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {

              final Bitmap image=(Bitmap)getItem(position);


            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.imageview2, null);


                holder.image = (ImageView) convertView
                        .findViewById(R.id.imageView2);



                holder.Delete=(Button)convertView.findViewById(R.id.buttonClose);

                convertView.setTag(holder);


            } else {
                holder = (ViewHolder) convertView.getTag();



            }




            BitmapFactory.Options factoryOptions = new BitmapFactory.Options();

            int imageWidth = factoryOptions.inDensity=70;
            int imageHeight = factoryOptions.inDensity=65;

       Bitmap Scaled = Bitmap.createScaledBitmap(images.get(position), imageWidth, imageHeight, true);
            holder.image.setImageBitmap(Scaled);

            holder.image.setTag(position);

            String me=  holder.image.getTag().toString();
            holder.Delete.setTag(position);

            ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
            image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            //encode image
            final ArrayList<HashMap<String, String>> imageHashMap=new ArrayList<HashMap<String,String>>();
            byte[] b = bytes.toByteArray();
             encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
            // StringImages.add(encodedImageString); 

            imageHashMap.putString(String.valueOf(image.getTag()), encodedImageString);



            holder.image.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub


                    final Dialog imgDialog = new Dialog(view.getContext(),android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

                  imgDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
                  imgDialog.setCancelable(false);
                  //   layout imageview2 is used because when i use simple imageview layout dialogue with imageview and closebutton,  
                  //   every taken image at instance will not be shown in dialogue.
                  imgDialog.setContentView(R.layout.imageview);
                  Button btnClose = (Button)imgDialog.findViewById(R.id.btnIvClose);
                  ImageView ivPreview = (ImageView)imgDialog.findViewById(R.id.image1);






                  ivPreview.setImageBitmap(images.get(position));

                  btnClose.setOnClickListener(new OnClickListener() {
                      @Override
                      public void onClick(View arg0) {

                          imgDialog.dismiss();
                      }
                  });
                  imgDialog.show();




                       myAdapter.notifyDataSetChanged();
                        listviewattachment.setSelection(myAdapter.getCount()+1 );



                }
            });



            holder.Delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    int tag = (Integer) view.getTag();
                    if (tag != (images.size() )) {
                        images.remove(tag);

                        Log.d("GCM", "Item removed from " + tag);
                        myAdapter.notifyDataSetChanged();


                }
                    if(imageHashMap.contains(tag)) { 
                        imageHashMap.remove(tag); }


                }
            });


            return convertView;
        }

    }

1 个答案:

答案 0 :(得分:0)

举个例子,使用hashmap看看:

Bitmap Scaled = Bitmap.createScaledBitmap(images.get(position), imageWidth,imageHeight,true);
holder.image.setImageBitmap(Scaled);
holder.image.setTag(position);
holder.Delete.setTag(position);
Bitmap image=(Bitmap)getItem(position);
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//encode image
byte[] b = bytes.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);

StringImages.add(encodedImageString)-----&gt;而不是这一行,使用像这样的

的散列图
imageHashMap.put(String.valueOf(image.getTag()), encodedImageString);

并在delete.onclicklistener代码中删除图像,例如images.remove(image),

添加这些行

if(imageHashMap.contains(image.getTag())) {
    imageHashMap.remove(image.getTag());
}

我希望这会有所帮助。 PS:在使用之前在构造函数中声明hashmap,否则你可能会得到一个空指针异常