访问android中expandablelistview内的复选框

时间:2013-02-04 08:10:03

标签: android listview checkbox

我正在使用教程中的消耗列表视图。

      public class ExpandableListDemo extends ExpandableListActivity {
private LayoutInflater inflater;
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
    try{
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

    SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
                this,
                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.
            );
        setListAdapter( expListAdapter );       // setting the adapter in the list.

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


}

/* Creating the Hashmap for the row */
@SuppressWarnings("unchecked")
private List createGroupList() {
      ArrayList result = new ArrayList();
      for( int i = 0 ; i < 15 ; ++i ) { // 15 groups........
        HashMap m = new HashMap();
        m.put( "Group Item","Group Item " + i ); // the key and it's value.
        result.add( m );
      }
      return (List)result;
}

/* creatin the HashMap for the children */
@SuppressWarnings("unchecked")
private List createChildList() {

    ArrayList result = new ArrayList();
    for( int i = 0 ; i < 15 ; ++i ) { // this -15 is the number of groups(Here it's fifteen)
      /* each group need each HashMap-Here for each group we have 3 subgroups */
      ArrayList secList = new ArrayList(); 
      for( int n = 0 ; n < 3 ; n++ ) {
        HashMap child = new HashMap();
        child.put( "Sub Item", "Sub Item " + n );           
        secList.add( child );
      }
     result.add( secList );
    }        
    return result;
}
public void  onContentChanged  () {
    System.out.println("onContentChanged");
    super.onContentChanged();         
}
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
    System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
    return true;
}

/* This function is called on expansion of the group */

}

我在子项目中使用复选框,我搜索了很多但是找不到解决方案来访问子项目中的复选框。我想在选中的复选框上祝酒消息。此外,我想获得按钮点击检查的复选框。请帮助我。

1 个答案:

答案 0 :(得分:0)

您需要在getChildView方法中创建自定义适配器并访问子视图,例如

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ...

        holder.checkBox.setVisibility(View.VISIBLE);
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //

            }
        });

        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });


        holder.checkBox.setChecked(episode.isChecked());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });



            return convertView;
        }

查看完整示例here 您还可以在ApiDemos中看到ExpandableList示例