2016-11-04 78 views
-2

请不要说这是重复的问题,因为我搜索谷歌,并尝试每一个答案,然后也没有得到结果。在我的Listview行中包含一个TextView和三个单选按钮。

问题是
当我在特定行点击对方,然后列表视图setOnItemClickListener工作正常,但问题是,当我在单选按钮,然后单击不setOnItemClickListener叫我不知道为什么?

我希望当我点击或选择任何redioButton然后调用一种方法,我知道这是可能的适配器,但我想单选按钮单击事件在列表视图onItemClick()方法。

对不起,我的英语不好,在此先感谢,并帮助将欣赏ListView setOnItemClickListener不工作时,ListView行包含RadioButton

xml文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="45dp" 
     android:padding="5dp" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:background="@drawable/student_row_background"> 

     <TextView 
      android:id="@+id/tv_attendance_studentName" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:layout_weight=".5" 
      android:gravity="center_vertical" 
      android:text="Name" 
      android:textColor="@color/headerActionbar" 
      android:textSize="15dp" 
      android:focusable="false" 
      android:focusableInTouchMode="false"/> 

     <RadioGroup 
      android:id="@+id/rg_1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".5" 
      android:orientation="horizontal" 
      android:gravity="center|right" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:descendantFocusability="blocksDescendants" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true"> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="21dp" 
       android:text="P" 
       android:id="@+id/radioButton_present" 
       android:textColor="@color/headerActionbar" 
       android:textStyle="bold" 
       android:gravity="center" 
       android:paddingRight="10dp" 
       android:textSize="12dp" 
       android:buttonTint="@color/headerActionbar" 
       android:focusable="false" 
       android:focusableInTouchMode="false" /> 

      <View 
       android:layout_width="1dp" 
       android:layout_height="21dp" 
       android:background="@color/dialogTextColor"/> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="21dp" 
       android:text="A" 
       android:textColor="@color/headerActionbar" 
       android:textStyle="bold" 
       android:paddingRight="10dp" 
       android:gravity="center" 
       android:textSize="12dp" 
       android:id="@+id/radioButton_absent" 
       android:buttonTint="@color/headerActionbar" 
       android:focusable="false" 
       android:focusableInTouchMode="false"/> 

      <View 
       android:layout_width="1dp" 
       android:layout_height="21dp" 
       android:background="@color/dialogTextColor"/> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="21dp" 
       android:text="L" 
       android:textColor="@color/headerActionbar" 
       android:textStyle="bold" 
       android:paddingRight="10dp" 
       android:gravity="center" 
       android:textSize="12dp" 
       android:id="@+id/radioButton_leave" 
       android:buttonTint="@color/headerActionbar" 
       android:focusable="false" 
       android:focusableInTouchMode="false"/> 

     </RadioGroup> 

    </LinearLayout> 

</RelativeLayout> 

这是的java文件

 lv_studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

        RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present); 
        RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent); 
        RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave); 

        if (!radioButton_present.isChecked() && !radioButton_absent.isChecked() 
        && !radioButton_leave.isChecked()) 
        { 
         constant.showSnackBar("Please select at least one attendance option"); 
        } 
        else if(radioButton_present.isChecked()) 
        { 
         constant.showSnackBar("Student is present"); 
        } 
        else if (radioButton_absent.isChecked()) 
        { 
         constant.showSnackBar("Student is absent"); 
        } 
        else if (radioButton_leave.isChecked()) 
        { 
         constant.showSnackBar("Student is leave"); 
        } 
      } 
     }); 
+0

什么是'lv_studentList'? –

+0

它的列表视图对象 – Ninja

+0

我没有在你的xml中看到一个ListView。关于为什么你不在'RadioGroup'上设置OnCheckedListener? –

回答

1

使用View参数onItemClick()方法找到RadioGroupRadioButton查看。然后根据需要使用该视图。

@Override 
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

      RadioGroup group = group = (RadioGroup) view.findViewById(R.id.rg_1); 
      final RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present); 
      final RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent); 
      final RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave); 
      group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup radioGroup, int i) { 
        if (!radioButton_present.isChecked() && !radioButton_absent.isChecked() 
          && !radioButton_leave.isChecked()) 
        { 
         Toast.makeText(MainActivity.this,"Please select at least one attendance option",Toast.LENGTH_LONG).show(); 
        } 
        else if(radioButton_present.isChecked()) 
        { 
         Toast.makeText(MainActivity.this,"Student is present",Toast.LENGTH_LONG).show(); 
        } 
        else if (radioButton_absent.isChecked()) 
        { 
         Toast.makeText(MainActivity.this,"Student is absent",Toast.LENGTH_LONG).show(); 
        } 
        else if (radioButton_leave.isChecked()) 
        { 
         Toast.makeText(MainActivity.this,"Student is leave",Toast.LENGTH_LONG).show(); 
        } 
       } 
      }); 


     } 
+0

我尝试但不工作。请帮助我 – Ninja

+0

请检查我更新的问题 – Ninja

+0

@Ninja,我已经用我试过的工作代码更新了答案 – Sanjeet