Android 如何在现有ExpandableListView活动中实现SearchView?

Android 如何在现有ExpandableListView活动中实现SearchView?,android,android-studio,expandablelistview,searchview,Android,Android Studio,Expandablelistview,Searchview,我需要从我的expandableListView创建一个SearchView,在ListView中显示结果 我已经在我的活动布局中创建了一个searchView: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:lay

我需要从我的expandableListView创建一个SearchView,在ListView中显示结果

我已经在我的活动布局中创建了一个searchView:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="@drawable/sky">

<SearchView android:id="@+id/search_it"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:iconifiedByDefault="false" />

<ExpandableListView
    android:id="@+id/accomodation_exp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#00000000"
    android:dividerHeight="6dp" />
</LinearLayout>

这是我的Java类:

package com.teamamazing.appsalinph.specialsactivities;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import com.teamamazing.appsalinph.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Accomodation extends AppCompatActivity {
private static ExpandableListView expandableListView;
private static Accomodation_Adapter adapter;

MediaPlayer mp;

SearchView search;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accomodation);

        expandableListView = (ExpandableListView) findViewById(R.id.accomodation_exp);

    // Setting group indicator null for custom indicator
    expandableListView.setGroupIndicator(null);

    setItems();
    setListener();

}

// Setting headers and childs to expandable listview
void setItems() {

    // Array list for header
    List<String> Headings = new ArrayList<String>();
    List<String> L1 = new ArrayList<String>();
    List<String> L2 = new ArrayList<String>();
    List<String> L3 = new ArrayList<String>();
    List<String> L4 = new ArrayList<String>();
    List<String> L5 = new ArrayList<String>();
    List<String> L6 = new ArrayList<String>();


    // Hash map for both header and child
    HashMap<String, List<String>> ChildList = new HashMap<String, List<String>>();
    String heading_items[] = getResources().getStringArray(R.array.accomodation);
    String l1[] = getResources().getStringArray(R.array.accomodation_h1);
    String l2[] = getResources().getStringArray(R.array.accomodation_h2);
    String l3[] = getResources().getStringArray(R.array.accomodation_h3);
    String l4[] = getResources().getStringArray(R.array.accomodation_h4);
    String l5[] = getResources().getStringArray(R.array.accomodation_h5);
    String l6[] = getResources().getStringArray(R.array.accomodation_h6);
    for (String title : heading_items) {
        Headings.add(title);
    }
    for (String title : l1) {
        L1.add(title);
    }
    for (String title : l2) {
        L2.add(title);
    }
    for (String title : l3) {
        L3.add(title);
    }
    for (String title : l4) {
        L4.add(title);
    }
    for (String title : l5) {
        L5.add(title);
    }
    for (String title : l6) {
        L6.add(title);
    }

    // Adding header and childs to hash map
    ChildList.put(Headings.get(0), L1);
    ChildList.put(Headings.get(1), L2);
    ChildList.put(Headings.get(2), L3);
    ChildList.put(Headings.get(3), L4);
    ChildList.put(Headings.get(4), L5);
    ChildList.put(Headings.get(5), L6);
    adapter = new Accomodation_Adapter(Accomodation.this, Headings, ChildList);

    // Setting adpater over expandablelistview
    expandableListView.setAdapter(adapter);
}

// Setting different listeners to expandablelistview
void setListener() {

    // This listener will show toast on group click
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView listview, View view,
                                    int group_pos, long id) {

            return false;
        }
    });

    // This listener will expand one group at one time
    // You can remove this listener for expanding all groups
    expandableListView
            .setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                // Default position
                int previousGroup = -1;

                @Override
                public void onGroupExpand(int groupPosition) {
                    if (groupPosition != previousGroup)

                        // Collapse the expanded group
                        expandableListView.collapseGroup(previousGroup);
                    previousGroup = groupPosition;
                }

            });

    // This listener will show toast on child click

    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;

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

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

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

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

            return false;
        }
    });

    search=(SearchView) findViewById(R.id.search_it);
    search.setQueryHint("Search");

    //*** setOnQueryTextFocusChangeListener ***
    search.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
                    Toast.LENGTH_SHORT).show();
        }
    });

    //*** setOnQueryTextListener ***
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), query,
                    Toast.LENGTH_SHORT).show();

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), newText,
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.accom_menu, menu);
    return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#77bfb8"
android:weightSum="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<TextView
    android:id="@+id/child_items"
    android:text="Hello World"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:layout_weight="0.23"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center_vertical"
    android:paddingLeft="5dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="13dp"/>
    </LinearLayout>
package com.teamazing.appsalinph.specialsactivities;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.SearchView;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ExpandableListView;
导入android.widget.Toast;
导入com.teamazing.appsalinph.R;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
公务舱住宿扩展活动范围{
私有静态ExpandableListView ExpandableListView;
专用静电调节适配器;
MediaPlayer mp;
搜索视图搜索;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动和住宿);
expandableListView=(expandableListView)findViewById(R.id.acomodation\u exp);
//为自定义指示符设置组指示符null
expandableListView.setGroupIndicator(空);
setItems();
setListener();
}
//将标题和子项设置为可展开的listview
void setItems(){
//标题的数组列表
列表标题=新的ArrayList();
列表L1=新的ArrayList();
列表L2=新的ArrayList();
列表L3=新的ArrayList();
列表L4=新的ArrayList();
列表L5=新的ArrayList();
列表L6=新的ArrayList();
//头文件和子文件的哈希映射
HashMap ChildList=新建HashMap();
字符串标题_items[]=getResources().getStringArray(R.array.accomodation);
字符串l1[]=getResources().getStringArray(R.array.accomodation_h1);
字符串l2[]=getResources().getStringArray(R.array.accomodation_h2);
字符串l3[]=getResources().getStringArray(R.array.accomodation_h3);
字符串l4[]=getResources().getStringArray(R.array.accomodation_h4);
字符串l5[]=getResources().getStringArray(R.array.accomodation_h5);
字符串l6[]=getResources().getStringArray(R.array.accomodation_h6);
用于(字符串标题:标题\u项){
标题.添加(标题);
}
用于(字符串标题:l1){
L1.添加(标题);
}
for(字符串标题:l2){
L2.添加(标题);
}
用于(字符串标题:l3){
L3.增加(标题);
}
用于(字符串标题:l4){
L4.增加(标题);
}
用于(字符串标题:l5){
L5.增加(标题);
}
用于(字符串标题:l6){
L6.添加(标题);
}
//将头文件和子文件添加到哈希映射
ChildList.put(headers.get(0),L1);
ChildList.put(标题.get(1),L2);
ChildList.put(标题.get(2),L3);
ChildList.put(标题.get(3),L4);
ChildList.put(标题.get(4),L5);
ChildList.put(标题.get(5),L6);
adapter=新住宿\适配器(acomodation.this,标题,儿童列表);
//在expandablelistview上设置适配器
expandableListView.setAdapter(适配器);
}
//将不同的侦听器设置为expandablelistview
void setListener(){
//此侦听器将在组单击时显示toast
expandableListView.setOnGroupClickListener(新的expandableListView.OnGroupClickListener(){
@凌驾
public boolean onGroupClick(可展开列表视图、列表视图、,
内部组(位置,长id){
返回false;
}
});
//此侦听器将一次扩展一个组
//您可以删除此侦听器以扩展所有组
可扩展列表视图
.setOnGroupExpandListener(新的ExpandableListView.OnGroupExpandListener(){
//默认位置
int-previousGroup=-1;
@凌驾
public void onGroupExpand(int groupPosition){
if(groupPosition!=previousGroup)
//瓦解扩大的集团
expandableListView.collapseGroup(上一个组);
previousGroup=groupPosition;
}
});
//此侦听器将在单击子对象时显示toast
expandableListView.setOnChildClickListener(新的expandableListView.OnChildClickListener(){
@凌驾
public boolean onChildClick(ExpandableListView、listview、,
int groupPosition、int childPosition、long id){
if(groupPosition==0){
开关(儿童位置){
//塞布阿诺
案例0:
mp=MediaPlayer.create(getApplicationContext(),R.raw.anyhotel1);
mp.start();
打破
//卡帕潘干
案例1:
mp=MediaPlayer.create(getApplicationContext(),R.raw.anyhotel2);
mp.start();
打破
//伊洛卡诺
案例2:
mp=MediaPlayer.create(getApplicationContext(),R.raw.anyhotel);
mp.start();
打破
}
}else if(groupPosition==1){
开关(儿童位置){
//塞布阿诺
案例0:
mp=MediaPlayer.create(getApplicationContext(),R.raw.anyrestaurant1);
mp.start();
打破
//卡帕潘干
案例1:
mp=MediaPlayer.create(getApplicationContext(),R.raw.anyrestau2);
mp.start();
打破
//伊洛卡诺
案例2:
mp=MediaPlayer.create(getApplicationContext(),R.raw.any
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#7be6f4"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp">

<TextView
    android:id="@+id/accomodation_headings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello World"
    android:textColor="#000"
    android:textSize="15dp"
    android:gravity="center_vertical"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:paddingLeft="5dp"
    android:paddingBottom="5dp"/></LinearLayout>