Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,
I want the full control over an ExpandableListView.So I have added one by one all
the codes from net[some codes from code project //http://stackoverflow.com/questions/7700223/how-to-delete-elements-from-a-list-in-android
] to create full control over it but still I have a problem .I am not able to delete a child from Sub list.
I am trying to delete a child once I touch the child.its deleting everything but the check box is not deleting from it and so the sub child is not deleting from the view.

here is my code:www.xtreamprogrammer.com\codeproject\expandablelistandcheckbox.rar

here is some code

Class:ElistCBox.java
package aexp.elistcbox;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;





import android.util.Log;

public class ElistCBox extends ExpandableListActivity
{
    private static final String LOG_TAG = "ElistCBox";

    static final String colors[] = {
	  "Grey",
	  "Blue",
	  "Yellow",
	  "Red"
	};

	static final String shades[][] = {
// Shades of grey
	  {
		"lightgrey","#D3D3D3",
		"dimgray","#696969",
		"sgi gray 92","#EAEAEA"
	  },
// Shades of blue
	  {
		"dodgerblue 2","#1C86EE",
		"steelblue 2","#5CACEE",
		"powderblue","#B0E0E6"
	  },
// Shades of yellow
	  {
		"yellow 1","#FFFF00",
		"gold 1","#FFD700",
		"darkgoldenrod 1","	#FFB90F"
	  },
// Shades of red
	  {
		"indianred 1","#FF6A6A",
		"firebrick 1","#FF3030",
		"maroon","#800000"
	  }
    };
	 CustomExpandableListView expListAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
       // setGroupIndicator(null);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setContentView(R.layout.main);
	
        expListAdapter=new CustomExpandableListView(
				this,
				createGroupList(),	// groupData describes the first-level entries
				R.layout.group_row,	// Layout for the first-level entries
				null,	// Key in the groupData maps to display
				new int[] {},		// Data under "colorName" key goes into this TextView
				createChildList(),	// childData describes second-level entries
				 0,
	                null,
	                new int[] {}
			);
		setListAdapter(expListAdapter );
    }

    public void  onContentChanged  () {
        super.onContentChanged();
        Log.d( LOG_TAG, "onContentChanged" );
    }
    public void onGroupCollapse(int groupPosition)
    {
    	
    	Toast.makeText(getBaseContext(), "collapsed groupPosition= "+groupPosition, Toast.LENGTH_SHORT).show();    	
    }
    
    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
    	Toast.makeText(getBaseContext(), "onChildClick groupPosition= "+groupPosition+" childPosition="+childPosition, Toast.LENGTH_SHORT).show();
    	expListAdapter.removeChild(groupPosition, childPosition,v,parent);
        Log.d( LOG_TAG, "onChildClick: "+childPosition );
       // v.setVisibility(View.GONE);
        

        if(v==null)
        	Toast.makeText(getBaseContext(), "gone= "+childPosition, Toast.LENGTH_SHORT).show();
        else
        {
        	Toast.makeText(getBaseContext(), "not gone= "+childPosition, Toast.LENGTH_SHORT).show();
	        	
	     /*   CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
	        if( cb != null )
	            cb.toggle();*/
        }
        return false;
    }
    public void  onGroupExpand  (int groupPosition) {
    	Toast.makeText(getBaseContext(), "expand groupPosition= "+groupPosition, Toast.LENGTH_SHORT).show();
        Log.d( LOG_TAG,"onGroupExpand: "+groupPosition );
    }

/**
  * Creates the group list out of the colors[] array according to
  * the structure required by SimpleExpandableListAdapter. The resulting
  * List contains Maps. Each Map contains one entry with key "colorName" and
  * value of an entry in the colors[] array.
  */
	private List createGroupList() {
	  ArrayList result = new ArrayList();
	  for( int i = 0 ; i < colors.length ; ++i ) {
		HashMap m = new HashMap();
		 Drawable photo= (Drawable) this.getResources().getDrawable(R.drawable.ic_launcher);
	    m.put( "colorName",colors[i] );
	    m.put("img1",photo);
		result.add( m );
	  }
	  return (List)result;
    }

/**
  * Creates the child list out of the shades[] array according to the
  * structure required by SimpleExpandableListAdapter. The resulting List
  * contains one list for each group. Each such second-level group contains
  * Maps. Each such Map contains two keys: "shadeName" is the name of the
  * shade and "rgb" is the RGB value for the shade.
  */
  private List createChildList() {
	ArrayList result = new ArrayList();
	for( int i = 0 ; i < shades.length ; ++i ) {
// Second-level lists
	  ArrayList secList = new ArrayList();
	  for( int n = 0 ; n < shades[i].length ; n += 2 ) {
	    HashMap child = new HashMap();
	    Drawable photo= (Drawable) this.getResources().getDrawable(R.drawable.ic_launcher);
		child.put( "shadeName", shades[i][n] );
	    child.put( "rgb", shades[i][n+1] );
	    child.put("img",photo );
	    child.put("check1","true" );
		secList.add( child );
	  }
	  result.add( secList );
	}
	return result;
  }

}


Class:CustomExpandableListView.java
XML
package aexp.elistcbox;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
//http://stackoverflow.com/questions/7700223/how-to-delete-elements-from-a-list-in-android
//http://stackoverflow.com/questions/5188196/how-to-write-custom-expandablelistadapter
public class CustomExpandableListView extends SimpleExpandableListAdapter {

    final LayoutInflater layoutInflater ;
    List <? extends Map<String, ?>> grp;
    List <? extends List<? extends Map<String, ?>>> child;
    View v;


    public CustomExpandableListView(Context context,
            List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
            int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
            List<? extends List<? extends Map<String, ?>>> childData,
            int childLayout, String[] childFrom, int[] childTo) {
        super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,
                groupTo, childData, childLayout, childFrom, childTo);
        grp=groupData;
        child=childData;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated constructor stub
    }



    public CustomExpandableListView(Context context,
            List<? extends Map<String, ?>> groupData, int groupLayout,
            String[] groupFrom, int[] groupTo,
            List<? extends List<? extends Map<String, ?>>> childData,
            int childLayout, String[] childFrom, int[] childTo) {
        super(context, groupData, groupLayout, groupFrom, groupTo, childData,
                childLayout, childFrom, childTo);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated constructor stub
    }



    public CustomExpandableListView(Context context,
            List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
            int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
            List<? extends List<? extends Map<String, ?>>> childData,
            int childLayout, int lastChildLayout, String[] childFrom,
            int[] childTo) {
        super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,
                groupTo, childData, childLayout, lastChildLayout, childFrom, childTo);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated constructor stub
    }



         @Override
           public View newGroupView(boolean isExpanded, ViewGroup parent){
                return layoutInflater.inflate(R.layout.group_row, null, false);
           }
        @Override
       public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent)
       {
            // final View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);

             View v = null;
                if( convertView != null )
                    v = convertView;
                else
                    v = layoutInflater.inflate(R.layout.group_row, parent, false);

               // Populate your custom view here
               ((TextView)v.findViewById(R.id.childname1)).setText((String) ((Map<String,Object>)getGroup(groupPosition)).get("colorName")  );
               ((ImageView)v.findViewById(R.id.test_image1)).setImageDrawable( (Drawable) ((Map<String,Object>)getGroup(groupPosition)).get("img1") );

                           return v;
            // return layoutInflater.inflate(R.layout.group_row, null, false);
       }

        @SuppressWarnings("unchecked")
        public void removeChild(int groupPosition, int childPosition,View v,ExpandableListView parent )
        {


        //HERE IS MY PROBLEM
            //Its not deleting the whole child view only one checkbox is still staying in all the child.
            ((Map<String,Object>)getChild(groupPosition, childPosition)).clear();
            notifyDataSetChanged();
            //HERE IS MY PROBLEM
            }

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

           View v = null;
            if( convertView != null )
                v = convertView;
            else
                v = layoutInflater.inflate(R.layout.child_row, parent, false);



           // Populate your custom view here
           ((TextView)v.findViewById(R.id.childname)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("shadeName") );
           ((TextView)v.findViewById(R.id.rgb)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("rgb") );
           ((ImageView)v.findViewById(R.id.test_image)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("img") );
           String str=(String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get("check1");
           if(str=="true")
               ((CheckBox)v.findViewById(R.id.check1)).setChecked( true);
           else if(str=="false")
           ((CheckBox)v.findViewById(R.id.check1)).setChecked( false);
           //else
             //  v.invalidate();


           return v;
       }

       @Override
       public View newChildView(boolean isLastChild, ViewGroup parent) {
            return layoutInflater.inflate(R.layout.child_row, null, false);
       }
   }


please give me some suggestion.
Thanks in advance.
Posted
Comments
shankha2010 29-Apr-12 12:05pm    
here is the code of that 2nd class
======================================
package aexp.elistcbox;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
//http://stackoverflow.com/questions/7700223/how-to-delete-elements-from-a-list-in-android
//http://stackoverflow.com/questions/5188196/how-to-write-custom-expandablelistadapter
public class CustomExpandableListView extends SimpleExpandableListAdapter {

final LayoutInflater layoutInflater ;
List >
List > grp;
List >> child;
View v;


public CustomExpandableListView(Context context,
List> groupData, int expandedGroupLayout,
int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,
groupTo, childData, childLayout, childFrom, childTo);
grp=groupData;
child=childData;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}



public CustomExpandableListView(Context context,
List> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupFrom, groupTo, childData,
childLayout, childFrom, childTo);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}



public CustomExpandableListView(Context context,
List> groupData, int expandedGroupLayout,
int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List>> childData,
int childLayout, int lastChildLayout, String[] childFrom,
int[] childTo) {
super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom,
groupTo, childData, childLayout, lastChildLayout, childFrom, childTo);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// TODO Auto-generated constructor stub
}



@Override
public View newGroupView(boolean isExpanded, ViewGroup parent){
return layoutInflater.inflate(R.layout.group_row, null, false);
}
@Override
public View getGroupView(int groupPosition,boolean isExpanded,View convertView,ViewGroup parent)
{
// final View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);

View v = null;
if( convertView != null )
v = convertView;
else
v = layoutInflater.inflate(R.layout.group_row, parent, false);

// Populate your custom view here
((TextView)v.findViewById(R.id.childname1)).setText((String) ((Map<string,object>)getGroup(groupPosition)).get("colorName") );
((ImageView)v.findViewById(R.id.test_image1)).setImageDrawable( (Drawable) ((Map<string,object>)getGroup(groupPosition)).get("img1") );

return v;
// return layoutInflater.inflate(R.layout.group_row, null, false);
}

@SuppressWarnings("unchecked")
public void removeChild(int groupPosition, int childPosition,View v,ExpandableListView parent )
{

// ((Map<string,object>)getChild(grou

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900