如何在列表视图中获取选中的单选按钮?

时间:2014-02-10 10:13:45

标签: android android-listview radio-button

我想从列表视图中的列表视图中获取选中的单选按钮,如下图所示。

enter image description here

AttendanceView.class

package com.cfms.ApplicationLayer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.cfms.cfms_project.R;

public class AttendanceView extends Fragment implements OnItemClickListener {


    ListView listEmp;
    RadioGroup radiogroup;
    TextView total_emp;
    String[] x = new String[]{"Emp1","EMP2","EMP3","EMP4","EMP5","EMP6","EMP7","EMP8","EMP9","EMP10","EMP11","EMP12","EMP13","EMP14","EMP15","EMP16","EMP17","EMP18","EMP19"};
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.attendance_attendance_fragment, null);

        radiogroup=(RadioGroup)view.findViewById(R.id.radio_attendance);


        total_emp=(TextView)view.findViewById(R.id.txtv_total_emp);
        total_emp.setText(""+x.length);
        listEmp=(ListView)view.findViewById(R.id.list_empl);
        ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getApplicationContext(),R.layout.employee_attendance_row,R.id.txt_emp_attendance,x);
        listEmp.setAdapter(test);
        listEmp.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        listEmp.setOnItemClickListener(new OnItemClickListener()
        {
            @Override 
            public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 

//          makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

}

RadioGroupAdpter.class

package com.cfms.ApplicationLayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cfms.cfms_project.R;

public class RadioGroupAdapter extends BaseAdapter{

        private Context mContext;

        public RadioGroupAdapter(Context c){
            mContext = c;

        }

        public int getCount(){
          return mThumbIds.length;
        }

        public Object getItem(int position){ 
           return null;
        }

        public long getItemId(int position){
           return 0;
        }

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

            ViewHolder holder = null; 


         if (convertView == null){
             LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
             convertView= infalInflater.inflate(R.layout.employee_attendance_row, null);
             holder = new ViewHolder();
             holder.txt=(TextView)convertView.findViewById(R.id.txt_emp_attendance);
             holder.radiogroup=(RadioGroup)convertView.findViewById(R.id.radio_attendance);

             convertView.setTag(holder);

            }
             else
             {
                 holder = (ViewHolder) convertView.getTag();
             }

         if(holder.radiogroup.getCheckedRadioButtonId()!=-1){
                int id= holder.radiogroup.getCheckedRadioButtonId();
                View radioButton = holder.radiogroup.findViewById(id);
                int radioId = holder.radiogroup.indexOfChild(radioButton);
                //RadioButton btn = (RadioButton) holder.radiogroup.getChildAt(radioId);
                System.out.println("in adpter..............."+radioId);
                //String selection = (String) btn.getText();
            }

         else{

             System.out.println("in else part of adpter...................");
         }

        holder.txt.setText(mThumbIds[position]);
        convertView.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT ,60));
        return convertView;


    }

    public static String[] mThumbIds = {"EMP1",
        "EMP2",
        "EMP3",
        "EMP4",
        "EMP5",
        "EMP6",
        "EMP7",
        "EMP8",
        "EMP9",
        "EMP10",
        "EMP11",
        "EMP12",
        "EMP13",
        "EMP14",
        "EMP15",
        "EMP16",
        "EMP17",
        "EMP18",
        "EMP19",
        "EMP20",
        "EMP21",
        "EMP22"};

    static class ViewHolder {
              TextView txt;
              RadioGroup radiogroup;

             }


        }

我想得到完全在场,缺席和半天的员工。

请帮忙。 感谢。

3 个答案:

答案 0 :(得分:2)

只需更改适配器类就解决了问题。

 package com.cfms.ApplicationLayer;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.LinearLayout.LayoutParams;
    import android.widget.ListView;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;

    import com.cfms.cfms_project.R;

    public class RadioGroupAdapter extends BaseAdapter implements OnClickListener{

            private Context mContext;
            //TextView pretext;
            private int selected = -1;
            int countPre,countAbs,countHD;
            public RadioGroupAdapter(Context c){
                mContext = c;

            }

            public int getCount(){
              return mThumbIds.length;
            }

            public Object getItem(int position){ 
               return null;
            }

            public long getItemId(int position){
               return 0;
            }

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

                ViewHolder holder = null; 


             if (convertView == null){
                 LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
                 convertView= infalInflater.inflate(R.layout.employee_attendance_row, null);
                 holder = new ViewHolder();
                 holder.txt=(TextView)convertView.findViewById(R.id.txt_emp_attendance);
                 holder.radiogroup=(RadioGroup)convertView.findViewById(R.id.radio_attendance);
                 holder.pre=(RadioButton)convertView.findViewById(R.id.radio_present);
                 holder.abs=(RadioButton)convertView.findViewById(R.id.radio_absent);
                 holder.halfday=(RadioButton)convertView.findViewById(R.id.radio_half_day);
                //AttendanceAttandanceView.totalpre=(TextView).findViewById(R.id.txtv_total_pre);

                // System.out.println("index of radio.........."+radioId);

                 convertView.setTag(holder);

                }
                 else
                 {
                     holder = (ViewHolder) convertView.getTag();
                 }


             holder.radiogroup.setOnCheckedChangeListener(new  OnCheckedChangeListener() {



                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub
                    View radioButton = group.findViewById(checkedId);
                     int radioId = group.indexOfChild(radioButton);



                    if(radioId == 0)
                    {
                        countPre++;
                        AttendanceAttandanceView.totalpre.setText(""+countPre);

                    }

                    else if(radioId == 1)
                    {
                        countAbs++;
                        AttendanceAttandanceView.totalabs.setText(""+countAbs);
                    }

                    else if(radioId == 2)
                    {
                        countHD++;
                        AttendanceAttandanceView.totalHalfday.setText(""+countHD);
                    }


                    //selected = radioId;
                    //notifyDataSetChanged();

                }
            });







            holder.txt.setText(mThumbIds[position]);
            holder.pre.setTag(position);
           // holder.pre.setChecked(position == selected);

            holder.abs.setTag(position);
            //holder.abs.setChecked(position == selected);

            holder.halfday.setTag(position);
           //holder.halfday.setChecked(position == selected);

            convertView.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT ,60));
            return convertView;


        }

        public static String[] mThumbIds = {"EMP1",
            "abcdef ghijklm nopqrstu vwxyz ABCDEFGH IJKLM",
            "EMP3",
            "EMP4",
            "EMP5",
            "EMP6",
            "EMP7",
            "EMP8",
            "EMP9",
            "EMP10",
            "EMP11",
            "EMP12",
            "EMP13",
            "EMP14",
            "EMP15",
            "EMP16",
            "EMP17",
            "EMP18",
            "EMP19",
            "EMP20",
            "EMP21",
            "EMP22"};

        static class ViewHolder {
                  TextView txt;
                  RadioGroup radiogroup;
                  RadioButton pre,abs,halfday;

                 }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }


        @Override
        public int getViewTypeCount() {                 
                      //Count=Size of ArrayList.
            return mThumbIds.length;
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }


            } 

感谢大家的关注。

答案 1 :(得分:1)

public void onRadioButtonClick(View v) {
    RadioButton rb = (RadioButton) v;
    Toast.makeText(getApplicationContext(),
    rb.getText(),Toast.LENGTH_SHORT).show();
    View Rg = (View) rb.getParent();
    View LLayout = (View) Rg.getParent();
    TextView txtLvId = (TextView) LLayout.findViewById(R.id.ins_itemid);
    String LvID = txtLvId.getText().toString();
    Toast.makeText(getApplicationContext(), LvID, Toast.LENGTH_LONG).show();
}

答案 2 :(得分:0)

我正在制作拼贴画的项目,我遇到了和你一样的麻烦, 我只是将这些代码放在我的活动中解决了这些问题 代码是: -

 <meta name="viewport" content="width=device-width, initial-scale=1">