2017-02-28 12 views
0

私は、各グループ内に入力済みの子アイテムを持つグループのリストを持っています。私はすでにsearchviewfiltered ressults and myのexpandablelistviewを実装して展開して折りたたむことができます。問題は、OnChildClickListenerを処理する方法がわかりません。OnChildClickListener関数を追加すると、アプリケーションが突然停止しました。誰かが私にこれを手伝うことができますか?ExpandableListView、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; 
    } 

} 

ありがとう!

答えて

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

     }