ExpandableListView,OnChildClickListener

时间:2017-02-28 16:36:03

标签: android filter expandablelistview searchview

我有每个组中包含已填充子项的组列表。我已经实现了searchview filtered ressults and my expandablelistview`可以展开和折叠。问题是,我不知道如何处理OnChildClickListener,一旦我添加了OnChildClickListener函数,应用程序突然停止工作。有人可以帮我解决这个问题吗?

这是我的代码:

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;
    }

}

谢谢!

1 个答案:

答案 0 :(得分:0)

我已经找到了正确的答案。 这就是我做的......

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;
    }

        }