Skip to main content

Expandable listview in android


Expandable listview in android

public class MainActivity extends Activity {
   
    private SettingsListAdapter adapter;
    private ExpandableListView categoriesList;
    private ArrayList<Category> categories;
   
    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        categoriesList = (ExpandableListView)findViewById(R.id.categories);
        categories = Category.getCategories();
        adapter = new SettingsListAdapter(this,
                categories, categoriesList);
        categoriesList.setAdapter(adapter);
       
        categoriesList.setOnChildClickListener(new OnChildClickListener() {
           
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

               
                CheckedTextView checkbox = (CheckedTextView)v.findViewById(R.id.list_item_text_child);
                checkbox.toggle();
               
               
                // find parent view by tag
                View parentView = categoriesList.findViewWithTag(categories.get(groupPosition).name);
                if(parentView != null) {
                    TextView sub = (TextView)parentView.findViewById(R.id.list_item_text_subscriptions);
                   
                    if(sub != null) {
                        Category category = categories.get(groupPosition);
                        if(checkbox.isChecked()) {
                            // add child category to parent's selection list
                            category.selection.add(checkbox.getText().toString());
                           
                            // sort list in alphabetical order
                            Collections.sort(category.selection, new CustomComparator());
                        }
                        else {
                            // remove child category from parent's selection list
                            category.selection.remove(checkbox.getText().toString());
                        }       
                       
                        // display selection list
                        sub.setText(category.selection.toString());
                    }
                }               
                return true;
            }
        });
    }
   
    public class CustomComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }
   
   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
---------------------------------------------------------------------------------------------------------------------

public class SettingsListAdapter extends BaseExpandableListAdapter {


    private LayoutInflater inflater;
    private ArrayList<Category> mParent;
    private ExpandableListView accordion;
    public int lastExpandedGroupPosition;   
   

    public SettingsListAdapter(Context context, ArrayList<Category> parent, ExpandableListView accordion) {
        mParent = parent;       
        inflater = LayoutInflater.from(context);
        this.accordion = accordion;      
       
    }


    @Override
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int i) {
        return mParent.get(i).children.size();
    }

    @Override
    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).name;
    }

    @Override
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).children.get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
       
        if (view == null) {
            view = inflater.inflate(R.layout.settings_list_item_parent, viewGroup,false);
        }
        // set category name as tag so view can be found view later
        view.setTag(getGroup(i).toString());
       
        TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
       
        //"i" is the position of the parent/group in the list
        textView.setText(getGroup(i).toString());
       
        TextView sub = (TextView) view.findViewById(R.id.list_item_text_subscriptions);       
       
        if(mParent.get(i).selection.size()>0) {
            sub.setText(mParent.get(i).selection.toString());
        }
        else {
            sub.setText("");
        }
       
        //return the entire view
        return view;
    }
   

    @Override
    //in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.settings_list_item_child, viewGroup,false);
        }

       
        CheckedTextView textView = (CheckedTextView) view.findViewById(R.id.list_item_text_child);
       
        //"i" is the position of the parent/group in the list and
        //"i1" is the position of the child
        textView.setText(mParent.get(i).children.get(i1).name);       

        // set checked if parent category selection contains child category
        if(mParent.get(i).selection.contains(textView.getText().toString())) {
            textView.setChecked(true);
        }
        else {
            textView.setChecked(false);
        }
       
        //return the entire view
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
   
    @Override
    /**
     * automatically collapse last expanded group
     * @see http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview
     */   
    public void onGroupExpanded(int groupPosition) {
       
        if(groupPosition != lastExpandedGroupPosition){
            accordion.collapseGroup(lastExpandedGroupPosition);
        }
       
        super.onGroupExpanded(groupPosition);
    
        lastExpandedGroupPosition = groupPosition;
       
    }
   
   
   
}

 

Comments

Popular posts from this blog

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {         return this.mCamera;     }     public void init() {         // Install a SurfaceHolder.Callback so we get notified when the         // underlying surface is created and destroyed.         mHolder = getHolder();         mHolder.addCallback(this);         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     }     public

Get Android phone call history/log programmatically

Get Android phone call history/log programmatically To get call history programmatically first add read conact permission in Manifest file : <uses-permission android:name="android.permission.READ_CONTACTS" /> Create xml file. Add the below code in xml file : <Linearlayout android:layout_height="fill_parent"  android:layout_width="fill_parent" android:orientation="vertical"> <Textview android:id="@+id/call" android:layout_height="fill_parent" android:layout_width="fill_parent"> </Textview> </Linearlayout> Now call the getCallDetails() method in java class : private void getCallDetails() { StringBuffer sb = new StringBuffer(); Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null); int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE ); int date = managedCur

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *            Height of destination area      * @param scalingLogic      *            Logic to use to avoid image stretching      * @return Decoded bitmap      */     public static Bitmap decodeResource(Resources res, int resId, int dstWidth,             int dstHeight, ScalingLogic scalingLogic) {         Options options = new Options();         options.inJustDecodeBounds = true;         BitmapFactory.decodeResource(res, resId, options);