Search code examples
androidfilterexpandablelistviewsearchview

ExpandableListView, OnChildClickListener


I have list of groups with populated child items inside of each group. I already implemented a searchview with filtered ressults and myexpandablelistview` can be expanded and collapsed. The problem is, I don't know how to deal with the OnChildClickListener, Once I added the OnChildClickListener function the application suddenly stopped working. Can someone help me get through with this?

This is my code:

package com.teamamazing.search;


import java.util.ArrayList;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.ExpandableListView.OnChildClickListener;

public class Accomodation extends Activity implements
        SearchView.OnQueryTextListener, SearchView.OnCloseListener{

    private SearchView search;
    private Accomodation_Adapter accomodationAdapter;
    private ExpandableListView expandableListView;
    private ArrayList<AccomodationHeaderRow> headerRows = new ArrayList<AccomodationHeaderRow>();
    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accomodation);
        expandableListView.setOnChildClickListener(expandableListViewClickedItem);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        search = (SearchView) findViewById(R.id.search);
        search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        search.setIconifiedByDefault(false);
        search.setOnQueryTextListener(this);
        search.setOnCloseListener(this);

        //display the list
        displayList();
        //expand all Groups
        expandAll();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.acco_search, menu);
        return true;
    }

    //method to expand all groups
    private void expandAll() {
        int count = accomodationAdapter.getGroupCount();
        for (int i = 0; i < count; i++){
            expandableListView.expandGroup(i);
        }
    }

//this is my current problem, there's no error yet the application isn't working.
    private OnChildClickListener expandableListViewClickedItem =  new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            if (groupPosition == 0) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel);
                        mp.start();
                        break;

                }
            } else if (groupPosition == 1) {
                switch (childPosition) {
                    //cebuano
                    case 0:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestaurant1);
                        mp.start();
                        break;
                    //kapampangan
                    case 1:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau2);
                        mp.start();
                        break;
                    //ilocano
                    case 2:
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau);
                        mp.start();
                        break;

                }
            }
            return false;
        }

    };



    //method to expand all groups
    private void displayList() {

        //display the list
        loadSomeData();

        //get reference to the ExpandableListView
        expandableListView = (ExpandableListView) findViewById(R.id.accomodation_exp);
        //create the adapter by passing your ArrayList data
        accomodationAdapter = new Accomodation_Adapter(Accomodation.this, headerRows);
        //attach the adapter to the list
        expandableListView.setAdapter(accomodationAdapter);

    }

    private void loadSomeData() {

        ArrayList<AccomodationChildRow> accomodationChildRows = new ArrayList<AccomodationChildRow>();

        AccomodationChildRow accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag unsa nga hotel nga duol?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a hotel kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga ot-otel ditoy nga asideg?");
        accomodationChildRows.add(accomodationChildRow);

        AccomodationHeaderRow accomodationHeaderRow = new AccomodationHeaderRow("Are there any hotels near here? | Mayroon bang anumang malapit na hotel dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);


        accomodationChildRows = new ArrayList<AccomodationChildRow>();
        accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag-unsa nga ristorant duol dinhi?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Kapampangan:\tAtin bang malapit a pipanganan kene?");
        accomodationChildRows.add(accomodationChildRow);
        accomodationChildRow = new AccomodationChildRow("Ilocano:\tAdda kadi ti uray ania nga asideg ditoy nga resrestauran?");
        accomodationChildRows.add(accomodationChildRow);


        accomodationHeaderRow = new AccomodationHeaderRow("Are there any restaurants near here? | Mayroon bang anumang malapit na restawran dito?", accomodationChildRows);
        headerRows.add(accomodationHeaderRow);
    }


    @Override
    public boolean onClose() {
        accomodationAdapter.filterData("");
       // expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        accomodationAdapter.filterData(query);
        expandAll();
        return false;
    }

}

Thanks!


Solution

  • I already found out the correct answer. this is what I do...

    package com.teamamazing.search;
    
    
    import java.util.ArrayList;
    
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.SearchManager;
    import android.content.Context;
    import android.view.Menu;
    import android.view.View;
    import android.widget.ExpandableListView;
    import android.widget.SearchView;
    import android.widget.ExpandableListView.OnChildClickListener;
    
    
    
    
    public class Accomodation extends Activity implements
            SearchView.OnQueryTextListener, SearchView.OnCloseListener {
    
        private SearchView search;
        private Accomodation_Adapter accomodationAdapter;
        private ExpandableListView expandableListView;
        private ArrayList<AccomodationHeaderRow> headerRows = new ArrayList<AccomodationHeaderRow>();
        MediaPlayer mp;
    
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_accomodation);
    
    
            //searchview
    
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            search = (SearchView) findViewById(R.id.search);
            search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            search.setIconifiedByDefault(false);
            search.setOnQueryTextListener(this);
            search.setOnCloseListener(this);
    
            //display the list
            displayList();
            //expand all Groups
            expandAll();
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.acco_search, menu);
            return true;
        }
    
        //method to expand all groups
        private void expandAll() {
            int count = accomodationAdapter.getGroupCount();
            for (int i = 0; i < count; i++){
                expandableListView.expandGroup(i);
            }
        }
    
    
        //method to expand all groups
        private void displayList() {
    
            //display the list
            loadSomeData();
    
            //get reference to the ExpandableListView
            expandableListView = (ExpandableListView) findViewById(R.id.accomodation_exp);
            //create the adapter by passing your ArrayList data
            accomodationAdapter = new Accomodation_Adapter(Accomodation.this, headerRows);
            //attach the adapter to the list
            expandableListView.setAdapter(accomodationAdapter);
    
    
            setListener();
    
        }
    
    
        void setListener() {
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView listview, View view,
            int groupPosition, int childPosition, long id) {
    
                if (groupPosition == 0) {
                    switch (childPosition) {
                        //cebuano
                        case 0:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel1);
                            mp.start();
                            break;
                        //kapampangan
                        case 1:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel2);
                            mp.start();
                            break;
                        //ilocano
                        case 2:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyhotel);
                            mp.start();
                            break;
    
                    }
                } else if (groupPosition == 1) {
                    switch (childPosition) {
                        //cebuano
                        case 0:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestaurant1);
                            mp.start();
                            break;
                        //kapampangan
                        case 1:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau2);
                            mp.start();
                            break;
                        //ilocano
                        case 2:
                            mp = MediaPlayer.create(getApplicationContext(), R.raw.anyrestau);
                            mp.start();
                            break;
    
                    }
                }
    
                return false;
            }
        });
    }
    
    
    
    
    
        private void loadSomeData() {
    
            ArrayList<AccomodationChildRow> accomodationChildRows = new ArrayList<AccomodationChildRow>();
    
            AccomodationChildRow accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag unsa nga hotel nga duol?");
            accomodationChildRows.add(accomodationChildRow);
            accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a hotel kene?");
            accomodationChildRows.add(accomodationChildRow);
            accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga ot-otel ditoy nga asideg?");
            accomodationChildRows.add(accomodationChildRow);
    
            AccomodationHeaderRow accomodationHeaderRow = new AccomodationHeaderRow("Are there any hotels near here? | Mayroon bang anumang malapit na hotel dito?", accomodationChildRows);
            headerRows.add(accomodationHeaderRow);
    
    
            accomodationChildRows = new ArrayList<AccomodationChildRow>();
            accomodationChildRow = new AccomodationChildRow("Binisaya: Aduna bay bisag-unsa nga ristorant duol dinhi?");
            accomodationChildRows.add(accomodationChildRow);
            accomodationChildRow = new AccomodationChildRow("Kapampangan: Atin bang malapit a pipanganan kene?");
            accomodationChildRows.add(accomodationChildRow);
            accomodationChildRow = new AccomodationChildRow("Ilocano: Adda kadi ti uray ania nga asideg ditoy nga resrestauran?");
            accomodationChildRows.add(accomodationChildRow);
    
    
            accomodationHeaderRow = new AccomodationHeaderRow("Are there any restaurants near here? | Mayroon bang anumang malapit na restawran dito?", accomodationChildRows);
            headerRows.add(accomodationHeaderRow);
        }
    
    
        @Override
        public boolean onClose() {
            accomodationAdapter.filterData("");
           // expandAll();
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String query) {
            accomodationAdapter.filterData(query);
            expandAll();
            return false;
        }
    
        @Override
        public boolean onQueryTextSubmit(String query) {
            accomodationAdapter.filterData(query);
            expandAll();
            return false;
        }
    
            }