+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get a child click in ExpandableListView?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    How to get a child click in ExpandableListView?

    I am having a ExpandableListView that extands adapter BaseExpandableListAdapter . I want to add a view to child when any of a child is clicked. So please tell me that how can i get proper position of the child when it is clicked or how can I add layout to it (or one more childto it).

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    Code:
    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 */
        public void  onGroupExpand  (int groupPosition) {
            try{
                 System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
            }catch(Exception e){
                System.out.println(" groupPosition Errrr +++ " + e.getMessage());
            }
        }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts