private void setGroupIndicatorToRight() {
/* Get the screen width */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
expListView.setIndicatorBounds(width - getDipsFromPixel(35), width- getDipsFromPixel(5));
} private int getDipsFromPixel(float pixels) { // Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f); }
}
Step6: Add adapter class with the name of ExpandableListAdapter.java and add the following code like as below.
public class ExpandableListAdapter extends BaseExpandableListAdapter
{ MainActivity mainActivity; List laptops; Map> laptopCollection; public ExpandableListAdapter(MainActivity mainActivity,List groupList, Map> laptopCollection) { this.mainActivity=mainActivity; this.laptops=groupList; this.laptopCollection=laptopCollection; }
public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return laptopCollection.get(laptops.get(groupPosition)).get(childPosition); }
public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; }
public View getChildView(final int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { String laptop=(String)getChild(groupPosition, childPosition);
LayoutInflater inflater = mainActivity.getLayoutInflater(); if(convertView==null) { convertView = inflater.inflate(R.layout.child_item, null); } TextView item = (TextView) convertView.findViewById(R.id.laptop); item.setTypeface(null, Typeface.BOLD); item.setText(laptop); ImageView delete = (ImageView) convertView.findViewById(R.id.delete); delete.setOnClickListener(new OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity); builder.setMessage("Do you want to remove?"); builder.setCancelable(false); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { List child = laptopCollection.get(laptops.get(groupPosition)); child.remove(childPosition); notifyDataSetChanged(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); return convertView; } public int getChildrenCount(int groupPosition) { return laptopCollection.get(laptops.get(groupPosition)).size(); }
public Object getGroup(int groupPosition) { return laptops.get(groupPosition); }
public int getGroupCount() { return laptops.size(); }
public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; }