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

时间:2017-02-24 02:15:46

标签: 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: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>

这是我的父级布局

<?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>

此外,我没有发现我的代码有任何错误。然而,当我试图运行apk时,应用程序突然停止了。

谢谢你提前。 :)

0 个答案:

没有答案