Android 如何在适配器类中取消选中radiobutton

Android 如何在适配器类中取消选中radiobutton,android,listview,radio-button,Android,Listview,Radio Button,我有一个自定义对话框,其中有一个listview和一个微调器项。listview每行包含textview和一个单选按钮。借助单选按钮,我一次只能选择一行。这种情况正在发生。 当我选择微调器项目,然后选择单选按钮时,问题出现了,上一个单选按钮仍然处于选中状态。现在,当我选择微调器项目时,应该取消选中上一个单选按钮 注意:我的微调器在主类中,单选按钮在我在主类中调用的CustomAdapterClass中 下面是我的代码:适配器类: public class APTRequestCustomAdap

我有一个自定义对话框,其中有一个listview和一个微调器项。listview每行包含textview和一个单选按钮。借助单选按钮,我一次只能选择一行。这种情况正在发生。 当我选择微调器项目,然后选择单选按钮时,问题出现了,上一个单选按钮仍然处于选中状态。现在,当我选择微调器项目时,应该取消选中上一个单选按钮

注意:我的微调器在主类中,单选按钮在我在主类中调用的CustomAdapterClass中

下面是我的代码:适配器类:

public class APTRequestCustomAdapter extends BaseAdapter{

       Context context;
    ArrayList<APTRequestCustomdetails> APTRequestCustomitems;
    private  static String display_aptstatus1="";  
    public static String adapterbookingid="";
     private boolean userSelected = false;
     public static RadioButton mCurrentlyCheckedRB;
     String patientid1="";
     public static String from2="";
     public static String to2="";
     public static String testing2="";
    public static String cancelbookid="";
      public static String radiostring="";
      public static String troubleradiobutton="";
      private int selectedItemIndex=-1;

      public APTRequestCustomAdapter(Context context,
            ArrayList<APTRequestCustomdetails> aPTRequestCustomitems) {
        super();
        this.context = context;
        this.APTRequestCustomitems = aPTRequestCustomitems;
    }

    @Override 
    public int getCount() {
        // TODO Auto-generated method stub
        return APTRequestCustomitems.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return APTRequestCustomitems.get(arg0);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;   
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v=convertView;
        final int pos=position;
        if(v==null){
            v = LayoutInflater.from(context).inflate(R.layout.backupcustomdailoguniversalappointment,null);
        }
            TextView patientname=(TextView)v.findViewById(R.id.txtpatientname);
        TextView tag=(TextView)v.findViewById(R.id.txttype);
        TextView phone=(TextView)v.findViewById(R.id.txtphone);
        TextView age=(TextView)v.findViewById(R.id.txtage);

        TextView Apptstatus=(TextView) v.findViewById(R.id.txtapptstatus);
        TextView bookid=(TextView) v.findViewById(R.id.txtbookingid);
        ImageView remove=(ImageView)v.findViewById(R.id.txtremove);
    RadioButton radio=(RadioButton)v.findViewById(R.id.txtRadiobutton);

    patientname.setPaintFlags(patientname.getPaintFlags() |Paint.UNDERLINE_TEXT_FLAG);
        patientname.setText(APTRequestCustomitems.get(position).getPatient_Name());

        tag.setText(APTRequestCustomitems.get(position).getTag());
        phone.setText(APTRequestCustomitems.get(position).getPhone());
        age.setText(APTRequestCustomitems.get(position).getAge());
        Apptstatus.setText(APTRequestCustomitems.get(position).getAppointmentstatus());
        bookid.setText(APTRequestCustomitems.get(position).getBookingId());

        Apptstatus.setVisibility(View.INVISIBLE);
        bookid.setVisibility(View.INVISIBLE);

        //APT_CustomRequestResponse.radiostring="";
    //   adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();// wrong code
        patientname.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) { 
                // TODO Auto-generated method stub
                System.out.println("Hello buddy");
                Intent io=new Intent(context, PatientSummaryActivity.class);
                DoctorGlobal.patid=APTRequestCustomitems.get(pos).getPatient_ID();
                DoctorGlobal.patname=APTRequestCustomitems.get(pos).getPatient_Name();
                System.out.println("PATID"+DoctorGlobal.patid);
                context.startActivity(io); 
            }
        });
        if (position==getCount()-1 && userSelected==false) {
         //   radio.setChecked(true);
            mCurrentlyCheckedRB = radio;
        } else { 
            radio.setChecked(false);
        }
        if(APTRequestCustomitems.get(position).getAppointmentstatus().equals("1")){
            remove.setVisibility(View.INVISIBLE);
            radio.setVisibility(View.INVISIBLE);
            v.setBackgroundColor(Color.parseColor("#1569C7"));


        }else
        {
            v.setBackgroundColor(Color.parseColor("#FFFFFF"));
            remove.setVisibility(View.VISIBLE);
            radio.setVisibility(View.VISIBLE);

            radio.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    APTRequestCustomAdapter.radiostring="checkedtrue";

                     if (mCurrentlyCheckedRB !=null) {
                         if (mCurrentlyCheckedRB ==null)
                                mCurrentlyCheckedRB = (RadioButton) v;
                         mCurrentlyCheckedRB.setChecked(true);

                       //  Toast.makeText(context, ""+pos, Toast.LENGTH_LONG).show();
                         adapterbookingid=APTRequestCustomitems.get(pos).getBookingId().toString();

                     }

                     if (mCurrentlyCheckedRB == v)
                         return;

                     mCurrentlyCheckedRB.setChecked(false);

                     ((RadioButton) v).setChecked(true);
                     mCurrentlyCheckedRB = (RadioButton) v; 

                }
            });

        }




        return v;
    }





}  
公共类APTRequestCustomAdapter扩展了BaseAdapter{
语境;
ArrayList APTRequestCustomitems;
私有静态字符串显示_aptstatus1=“”;
公共静态字符串适配器bookingId=“”;
私有布尔userSelected=false;
公共静态单选按钮mCurrentlyCheckedRB;
字符串1=“”;
公共静态字符串from2=“”;
公共静态字符串to2=“”;
公共静态字符串测试2=“”;
公共静态字符串cancelbookid=“”;
公共静态字符串radiostring=“”;
公共静态字符串troubleradiobutton=“”;
private int selectedItemIndex=-1;
公共APTRequestCustomAdapter(上下文,
ArrayList aPTRequestCustomitems){
超级();
this.context=上下文;
this.APTRequestCustomitems=APTRequestCustomitems;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回APTRequestCustomitems.size();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回APTRequestCustomitems.get(arg0);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图v=转换视图;
最终int pos=位置;
如果(v==null){
v=布局展开器。从(上下文)。充气(R.layout.backupCustomDailogUniversalAppoint,null);
}
TextView patientname=(TextView)v.findViewById(R.id.txtpatientname);
TextView标记=(TextView)v.findViewById(R.id.txttype);
TextView电话=(TextView)v.findviewbyd(R.id.txtphone);
TextView年龄=(TextView)v.findViewById(R.id.txtage);
TextView-Apptstatus=(TextView)v.findViewById(R.id.txtapptstatus);
TextView bookid=(TextView)v.findViewById(R.id.txtbookingid);
ImageView移除=(ImageView)v.findViewById(R.id.txtremove);
RadioButton单选=(RadioButton)v.findViewById(R.id.txtRadiobutton);
patientname.setPaintFlags(patientname.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
patientname.setText(APTRequestCustomitems.get(position.getPatient_Name());
tag.setText(APTRequestCustomitems.get(position.getTag());
setText(APTRequestCustomitems.get(position.getPhone());
age.setText(APTRequestCustomitems.get(position.getAge());
setText(APTRequestCustomitems.get(position.getAppointmentstatus());
bookid.setText(APTRequestCustomitems.get(position.getBookingId());
Apptstatus.setVisibility(View.INVISIBLE);
bookid.setVisibility(视图不可见);
//APT_CustomRequestResponse.radiostring=“”;
//adapterbookingid=APTRequestCustomitems.get(pos.getBookingId().toString();//错误代码
patientname.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
System.out.println(“你好,伙计”);
意向io=新意向(上下文,PatientSummaryActivity.class);
DoctorGlobal.patid=APTRequestCustomitems.get(pos.getPatient_ID();
DoctorGlobal.patname=APTRequestCustomitems.get(pos.getPatient_Name();
System.out.println(“PATID”+DoctorGlobal.PATID);
背景。起始触觉(io);
}
});
if(position==getCount()-1&&userSelected==false){
//radio.setChecked(正确);
mCurrentlyCheckedRB=无线电;
}否则{
收音机。设置已检查(错误);
}
if(APTRequestCustomitems.get(position.getAppointStatus().equals(“1”)){
移除.setVisibility(视图.不可见);
radio.setVisibility(视图.不可见);
v、 setBackgroundColor(Color.parseColor(#1569C7”);
}否则
{
v、 setBackgroundColor(Color.parseColor(#FFFFFF”);
remove.setVisibility(View.VISIBLE);
radio.setVisibility(View.VISIBLE);
radio.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
APTRequestCustomAdapter.radiostring=“checkedtrue”;
if(mCurrentlyCheckedRB!=null){
if(mCurrentlyCheckedRB==null)
mCurrentlyCheckedRB=(单选按钮)v;
mCurrentlyCheckedRB.setChecked(true);
//Toast.makeText(上下文,“+pos,Toast.LENGTH_LONG.show();
adapterbookingid=APTRequestCustomitems.get(pos.getBookingId().toString();
}
if(mCurrentlyCheckedRB==v)
返回;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton)v).setChecked(真);
mCurrentlyCheckedRB=(单选按钮)v;
}
});
}
返回v;
}
}  
这是我调用适配器类的代码: 此方法位于PostExecu上的异步类中
public void showCustomDialog() {
        // TODO Auto-generated method stub
         final Dialog dialog = new Dialog(context);

         List<String> list=new ArrayList<String>();
        list.add("Normal");
        list.add("Low");  
        list.add("High"); 

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.customdialoguniversalappointment);
        ListView listcustomuniversalappt=(ListView) dialog.findViewById(R.id.listcustomuniversalappt);

        LinearLayout layoutsubject=(LinearLayout) dialog.findViewById(R.id.layoutsubject);
        LinearLayout layoutappt=(LinearLayout) dialog.findViewById(R.id.layoutappt);
        spinnerappt=(Spinner)dialog.findViewById(R.id.permissionspinner);

        ImageView cancel=(ImageView)dialog.findViewById(R.id.imgcancel);
        Button cancelappt=(Button)dialog.findViewById(R.id.btncancelappt);
        Button confirmappt=(Button)dialog.findViewById(R.id.btnconfirmappt); 
        EditText subject=(EditText) dialog.findViewById(R.id.edtsubject);


         ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, list);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerappt.setAdapter(dataAdapter);

            spinnerappt.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    int index = arg0.getSelectedItemPosition();
                 selected_item=arg0.getItemAtPosition(arg2).toString();
                 APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);//this line of code is not executing,the radio button remains checked in the view
                 if(APTRequestCustomAdapter.mCurrentlyCheckedRB.isChecked()){// this lines of codes are not executing even though the radio button is checked ,i don't know why
                    APTRequestCustomAdapter.mCurrentlyCheckedRB.setChecked(false);// And this line is also not executing

                 }

                } 

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub


                } 
            });  


       APTRequestCustomAdapter adap=new APTRequestCustomAdapter(context,run_custom_universal_apptdata());
            listcustomuniversalappt.setAdapter(adap);
            /*//runapptstatus_method();
            listcustomuniversalappt.setChoiceMode(1);
            /

                dialog.show();

    }
public class MyRadioAdapter extends BaseAdapter
{
    private Context mContext;
    private ArrayList<Variation> mVariations;
    private int mSelectedVariation;


    public MyRadioAdapter(Context context, ArrayList<Variation> variations, int selectedVariation)
    {
        mContext = context;
        mVariations = variations;
        mSelectedVariation = selectedVariation;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if(view==null) 
        {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.my_radio_adapter_item, null);          
        }

        final Variation variation = mVariations.get(position);

        TextView name = (TextView) view.findViewById(R.id.name);
        RadioButton radio = (RadioButton) view.findViewById(R.id.radio);

        name.setText(variation.getName());
        if(position==mSelectedVariation) radio.setChecked(true);
        else radio.setChecked(false);

        view.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                mSelectedVariation = position;
                MyRadioAdapter.this.notifyDataSetChanged();
            }
        });

        return view;
    }