2016-11-04 77 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