我有一个包含图像,文本和复选框的列表视图,我已经使用BaseAdapter对其进行了放大。我只想发送那些选择的图像,但是如何存储复选框的状态?
可能是我使用baseadapter时错了。您可以建议其他任何方法。

最佳答案

请看下面我的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 ");
            }

        }

关于android - 在Android中使用自定义BaseAdapter时在listview中存储复选框的状态?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7610267/

10-12 00:20