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

Volley for client and server interaction (Android)

Volley provides client and server interaction with QUEUE basis, each and every request process separately, we can cancel our request from QUEUE. HERE is a code sample for Volley with json request format. GenerateRequest.java -------------------------------------------------------------------- package com.entrata.maintenance.network_communication.networkutils; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.entrata.maintenance.application.ERPUserDefaults; import com.entrata.maintenance.network_communication.networkconstant.NetworkConstant; import com.entrata.maintenance.network_communication.networkexception.Error; import com.entrata.maintenance.network_communication.networkutil.SubdomainChange; import com.entrata.maintenance.utils.Logger; import com.google.gson.Gson; import org.json.JSONException; import org.js...
Java Code Examples for javax.net.ssl.HttpsURLConnection The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks for your votes! Code Example 1:   9  From project ADFS , under directory /adfs-hdfs-project/adfs-hdfs/src/main/java/org/apache/hadoop/hdfs/ . Source HsftpFileSystem.java @Override protected HttpURLConnection openConnection ( String path , String query ) throws IOException { query = addDelegationTokenParam ( query ); final URL url = new URL ( "https" , nnAddr . getHostName (), nnAddr . getPort (), path + '?' + query ); HttpsURLConnection conn =( HttpsURLConnection ) url . openConnection (); conn . setHostnameVerifier ( new DummyHostnameVerifier ()); return ( HttpURLConnection ) conn ; } Code Example 2:  ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...