2013-04-23 34 views
0

從我的MainViewExpandableListView)我得到RadioButton定義child_row.xml,我設置聽衆,但OnCheckChanged不會被調用。如果我將RadioButton移動到當前視圖,它會被調用。RadioButton OnCheckChanged不叫

我該如何解決這個問題?

我....

  1. mainListView.xml // expandableListView
  2. group_row.xml // TextView的
  3. child_row.xml //定製RADIOBUTTON

public class Main_ListView extends Activity implements OnCheckedChangeListener{ 
public void onCreate(Bundle savedInstanceState) { 
try{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainListView); 

     ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView1); 
     SimpleExpandableListAdapter expListAdapter = 
       new SimpleExpandableListAdapter(
         getApplicationContext(), 
         createGroupList(),    // Creating group List. 
         R.layout.group_row,    // Group item layout XML. 
         new String[] { "Group Item" }, // the key of group item. 
         new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView. 
         createChildList(),    // childData describes second-level entries. 
         R.layout.child_row,    // Layout for sub-level entries(second level). 
         new String[] {"Sub Item"},  // Keys in childData maps to display. 
         new int[] { R.id.grp_child}  // Data under the keys above go into these TextViews. 
        ); 
       elv.setAdapter(expListAdapter);  // setting the adapter in the list. 

     }catch(Exception e){ 
       System.out.println("Errrr +++ " + e.getMessage()); 
     } 


    final LayoutInflater factory = getLayoutInflater(); 
    final View childview = factory.inflate(R.layout.child_row, null); 

    answersGroup = (SegmentedRadioGroup) childview.findViewById(R.id.answersGroup); 
    if(answersGroup != null) 
     answersGroup.setOnCheckedChangeListener(this); 

} 
@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) { 
    if (group == answersGroup) { 
     if (checkedId == R.id.radio0) { 
      mToast.setText("One"); 
      mToast.show(); 
     } else if (checkedId == R.id.radio1) { 
      mToast.setText("Two"); 
      mToast.show(); 
     } else if (checkedId == R.id.radio2) { 
      mToast.setText("Three"); 
      mToast.show(); 
     } 
    } 

} 
+0

它必須被調用,也許更改條件「if(group == answersGroup)」作爲測試如果2個對象在java中是完全相同的方法 – Benoit 2013-04-23 20:10:58

+0

當我從不同的佈局獲取ID時缺少某些東西 - 導致它不會調用該方法。就像我說的,如果我將收音機移動到currentView的佈局,它會調用它。你的支票在方法中 - 但方法沒有被調用。 – user1529412 2013-04-23 20:14:12

回答

0

您沒有提供您使用的完整代碼。如果您以後將充值的childView添加到Activity的佈局,那麼您應該提供主佈局以查看您是否不以某種方式阻止CheckBox。如果你只是誇大佈局,那麼這是正常的,你沒有得到檢查的事件,因爲childView是沒有在屏幕上的位置被採取行動。

從代碼的外觀看來,您似乎要爲ExpandableListView的子代中的CheckBox設置偵聽器。如果這是你想要的,那麼這是一個不正確的嘗試,而做到這一點的方法是使用你的ExpandableListView的自定義適配器。有許多教程(和問題在這裏)構建一個自定義適配器並將偵聽器設置爲子元素。

+0

是的,我想要的是監聽兒童內部的複選框。自定義適配器是否是正確的方式?在實際的複選框偵聽器代碼去適配器? – user1529412 2013-04-24 14:20:07

+0

@ user1529412是的,自定義適配器是設置偵聽器的正確方法。在它的'getChildView()'方法中膨脹你的佈局,然後設置CheckBox的偵聽器。 – Luksprog 2013-04-24 14:27:10

+0

謝謝 - 工作就像一個魅力。 – user1529412 2013-04-24 20:21:54