重写ListView控件实现RadioButton单选

  @Override

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

            convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);  //解决办法: 每次都重新获取View

            Button button = (Button) convertView.findViewById(R.id.button);

            button.setText(s[position]);

            RadioButton radioButton = (RadioButton) convertView.findViewById(R.id.radioButton);

            radioButton.setId(position);  //把position设为radioButton的id

            radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                

                @Override

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    

                    if(isChecked){

                        //这段代码来实现单选功能

                        if(temp != -1){

                            RadioButton tempButton = (RadioButton) MainActivity.this.findViewById(temp);

                            if(tempButton != null){

                               tempButton.setChecked(false);

                            }

                            

                        }

                        

                        temp = buttonView.getId();

                        Log.i(TAG,"you are women- -   " + isChecked + "   " + temp);

                        

                    }

                }

            });

            

            //这里实现单选框选的回显,解决了单选框移出屏幕范围未选中状态

            if(temp == position){

                radioButton.setChecked(true);

            }

            return convertView;

        }

//            Holder holder;

//            if(convertView == null){    //1,当第一次加载ListView控件时  convertView为空 

//                convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null); //所以当ListView控件没有滑动时都会执行这条语句

//                holder = new Holder();

//                convertView.setTag(holder);

//            }else{

//                holder = (Holder) convertView.getTag();

//            }

//            

//            holder.button = (Button) convertView.findViewById(R.id.button);

//            holder.button.setText(s[position]);

//            

//            holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton);   //

//            holder.radioButton.setId(position);  //2,因为这里对radioButton的id进行重新设置,滑动ListView时convertView不为空,上面的语句就没法得到radioButton对象,这条语句就会报空指针异常

          

//            holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

//                

//                @Override

//                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

//                    

//                    if(isChecked){

//                        if(temp != -1){

//                            RadioButton tempButton = (RadioButton) MainActivity.this.findViewById(temp);

//                            tempButton.setChecked(false);

//                            

//                        }

//                        

//                        temp = buttonView.getId();

//                        Log.i(TAG,"you are women- -   " + isChecked + "   " + temp);

//                        

//                    }

//                }

//            });

//            return convertView;

//        }

//        private class Holder{

//            private Button button;

//            private RadioButton radioButton;

//        }

    }

 

你可能感兴趣的:(RadioButton)