top of page

Expandable ListView Android - Android App Coursework Help

Updated: Dec 26, 2021

Welcome to Android ExpandableListView Example Tutorial. In this tutorial we’ll implement an ExpandableListView which is used to group list data by categories. It’s sort of menu and submenus in a Android ListView.


output :




Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.

ExpandableListViewAdapter in android loads the data into the items associated with this view.


MainActivity




ExpandableListView expa;
ArrayList<String> list=newArrayList<>();
HashMap<String, ArrayList<String>> listChild=newHashMap<>();
expa = findViewById( R.id.exlist );
list.add( "Machine Learning" ); 
       list.add( "Android" );   
       list.add( "Java" );  
       list.add( "C++" );   
       list.add( "Programming" ); 
       list.add( "R language " );
ArrayList<String> arraylist = new ArrayList<>();
        for(int i=0;i<=4;i++)
 {            
arraylist.add( "10:20 AM"+i );  
 }
Adapter.java
public class MainAdapter extends BaseExpandableListAdapter
 { 
List<String> list;HashMap<String, ArrayList<String>> listChild
;
public Main Adapter( List<String>list, HashMap<String, ArrayList<String>>  listChild)    {
this.list=list;
this.listChild=listChild;   
 }
@OverridepublicintgetGroupCount() 
{return list.size();    }
@Override
 public  intgetChildrenCount(intgroupPosition) 
{ 
return listChild.get( list.get( groupPosition ) ).size();   
 }
@Override
public Objectget Group(intgroupPosition) 
{ return list.get( groupPosition );    
}
@Override
public Object getChild(intgroupPosition, intchildPosition)
 {
 return listChild.get( list.get( groupPosition ) ).get( childPosition ); 
   }
@Override
public longgetGroupId(intgroupPosition) 
{
 return groupPosition;  
  }
@Override
public longgetChildId(intgroupPosition, intchildPosition)
 {
return childPosition;  
 }
@Override
public boolean hasStableIds()
 { 
return false;    
}

@Override

public  ViewgetGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent)
 {// initialize value  
      convertView=LayoutInflater.from( parent.getContext() ).inflate( 
   android.R.layout.simple_expandable_list_item_1,parent,false );
TextView  textView=convertView.findViewById( android.R.id.text1 );
//initialise 
stringString sGroup =String.valueOf( getGroup( groupPosition ) );
//set Text view   
   textView.setText( sGroup );  
     textView.setTypeface( null, Typeface.BOLD );   
     textView.setTextColor( Color.BLUE );return convertView;    }
@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, finalViewGroup parent) 

{//initialise view 
 convertView=LayoutInflater.from( parent.getContext()).inflate(android.R.layout.simple_selectable_list_item,parent,false);
TextView textView=convertView.findViewById( android.R.id.text1 );
finalString schild=String.valueOf( getChild( groupPosition,childPosition ) );
// set on text view        
textView.setText( schild );   

     textView.setOnClickListener( newView.OnClickListener() 
{

@Override
publicvoidonClick(View)
 {
Toast.makeText( parent.getContext(), schild, Toast.LENGTH_SHORT ).show();        
    }     
   } );
return convertView;
    }
@Override
  public boolean isChildSelectable(intgroupPosition, intchildPosition) 
{
return true;  
  }

}

Contact us for andriod assignment Solutions by Codersarts Specialist who can help you mentor and guide for such assignments. CONTACT US TODAY



Comments


bottom of page