在android中使用自定义BaseAdapter时在列表视图中存储复选框的状态?

发布于 2024-12-07 13:03:08 字数 124 浏览 1 评论 0原文

我有一个列表视图,其中包含图像、文本和复选框,我使用 BaseAdapter 对其进行了膨胀。我只想发送那些选定的图像,但如何存储复选框的状态? 可能是我错了,因为我使用的是baseadapter。您可以建议任何其他方式来做到这一点。

i have a listview which is having image, text and checkbox i have inflated this using BaseAdapter. i want to send only those images which are selected but how to store the state of checkbox ?
may be i am wrong as i am using baseadapter. you can suggest any other way to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-12-14 13:03:09

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

        }

please look below my 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

and here is my 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 ");
            }

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文