如何在适配器类中取消选中单选按钮

时间:2013-12-17 12:19:57

标签: android listview radio-button

我有一个自定义对话框,在这里我有一个listview和一个微调项目。列表视图包含textview和每行中的单选按钮。我可以在单选按钮的帮助下一次只选择一行。这是发生。 当我选择微调器项目然后我选择单选按钮时,问题就出现了,前一个单选按钮仍然被选中。当我选择微调器项目时,我想要的是什么,之前选择的单选按钮应该是未选中的。

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

这是我的代码:适配器类:

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;
    }





}  

这是我调用适配器类的代码:     此方法在PostExecute()方法的异步类中

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();

    }

1 个答案:

答案 0 :(得分:0)

尝试使用自定义适配器,例如

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;
    }

here单选按钮通常按照无线电组进行分组。当选择组中的一个RadioButton时,将自动取消选择所有其他单选按钮。