在android中使用自定义BaseAdapter时,在listview中存储checkbox的状态?

时间:2011-09-30 12:20:37

标签: android listview checkbox custom-adapter

我有一个listview,它有图像,文本和复选框,我使用BaseAdapter对此进行了充气。我想只发送那些被选中的图像但是如何存储复选框的状态? 可能是我错了,因为我正在使用baseadapter。你可以建议任何其他方式来做到这一点。

1 个答案:

答案 0 :(得分:2)

请查看我的Baseadapter

class ImageInfoAdapter extends BaseAdapter{

        @Override
        public int getCount() {

            if(viewcount == 0){
                return 0;
            }
            return viewcount;
        }

        @Override
        public Object getItem(int position) {

            return isSentAlList.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            final ViewHolder viewHolder;
            View rowView=view;

            if(rowView==null){
                LayoutInflater layoutinflate =  LayoutInflater.from(ListPictures.this);
                rowView=layoutinflate.inflate(R.layout.listviewayout, parent, false);


                viewHolder = new ViewHolder();

                viewHolder.textViewisSentFlag = (TextView)rowView.findViewById(R.id.textViewisSentFlag);
                viewHolder.imageViewToSent = (ImageView)rowView.findViewById(R.id.imageViewToSent);
                viewHolder.checkBoxToSend = (CheckBox)rowView.findViewById(R.id.checkBoxToSend);

                rowView.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder) rowView.getTag();


            }

            viewHolder.ref = position;
            Log.i("InfoLog","viewHolder.ref = position; "+viewHolder.ref); 
            viewHolder.textViewisSentFlag.setText(isSentAlList.get(position)); 
            Bitmap blob = BitmapFactory.decodeByteArray(imageAlList.get(position), 0, imageAlList.get(position).length);
            viewHolder.imageViewToSent.setImageBitmap(blob);
            viewHolder.checkBoxToSend.setClickable(true);


            if(checked.containsKey(""+viewHolder.ref))   ///if this id is present as key in hashmap  
            { 
                Log.i("InfoLog","checked.containsKey "+viewHolder.ref);
            if(checked.get(""+viewHolder.ref).equals("true"))  //also check whether it is true or false to check/uncheck checkbox 
            {
                Log.i("InfoLog","checked.get(position) "+viewHolder.ref); 
                viewHolder.checkBoxToSend.setChecked(true);
            }
            else
                viewHolder.checkBoxToSend.setChecked(false);

            }
            else
                viewHolder.checkBoxToSend.setChecked(false);

            viewHolder.checkBoxToSend.setOnCheckedChangeListener(new OncheckchangeListner(viewHolder));
            return rowView;

        }//End of method getView
    }//End of class ImageInfo



    class ViewHolder{

        private TextView textViewisSentFlag = null;
        private ImageView imageViewToSent = null;
        private CheckBox checkBoxToSend = null;

        int ref;
    }//End of class ViewHolder

这是我的oncheckchangedlistener

   class OncheckchangeListner implements OnCheckedChangeListener{

            ViewHolder viewHolder = null; 
            public OncheckchangeListner(ViewHolder viHolder)
            {
                viewHolder =  viHolder;  
            }
            @Override 
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                if(viewHolder.checkBoxToSend.equals(buttonView))
                {       
                if(!isChecked)
                {
                    Log.i("InfoLog","checked.get before "+checked.get(""+viewHolder.ref));
                    checked.put(""+viewHolder.ref,"false");
                    Log.i("InfoLog","checked.get after "+checked.get(""+viewHolder.ref));
                }
                else
                    checked.put(""+viewHolder.ref,"true");


                }
                else
                    Log.i("InfoLog","i m in checkchange ");
            }

        }