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[][] = {
{
"lightgrey","#D3D3D3",
"dimgray","#696969",
"sgi gray 92","#EAEAEA"
},
{
"dodgerblue 2","#1C86EE",
"steelblue 2","#5CACEE",
"powderblue","#B0E0E6"
},
{
"yellow 1","#FFFF00",
"gold 1","#FFD700",
"darkgoldenrod 1"," #FFB90F"
},
{
"indianred 1","#FF6A6A",
"firebrick 1","#FF3030",
"maroon","#800000"
}
};
CustomExpandableListView expListAdapter;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
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(),
R.layout.group_row,
null,
new int[] {},
createChildList(),
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 );
if(v==null)
Toast.makeText(getBaseContext(), "gone= "+childPosition, Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(getBaseContext(), "not gone= "+childPosition, Toast.LENGTH_SHORT).show();
}
return false;
}
public void onGroupExpand (int groupPosition) {
Toast.makeText(getBaseContext(), "expand groupPosition= "+groupPosition, Toast.LENGTH_SHORT).show();
Log.d( LOG_TAG,"onGroupExpand: "+groupPosition );
}
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;
}
private List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < shades.length ; ++i ) {
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
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 > 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 )
{
//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.