我的searchview过滤器不起作用

时间:2017-03-05 07:25:13

标签: java android

当我输入时,我想在下面的列表视图中显示过滤结果,其中仅以M开头,依此类推。但现在它不起作用。 当我输入v时,然后返回开始菜单

mycode.java

package com.example.acerpredator.biologisma;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;


public class penemu extends AppCompatActivity implements SearchView.OnQueryTextListener{
    ListView list;
    SearchView search_view;
    ArrayAdapter<String> adapter;

    String[] NamaNegara = {
            "Indonesia",
            "Malasyia",
            "Thailand",
            "Filipina",
            "Kamboja",
            "Vietnam",
            "Singapura",
            "Laos"
    };
    Integer[] GbrBendera={
            R.drawable.istilah,
            R.drawable.biologix,
            R.drawable.biologix,
            R.drawable.istilah,
            R.drawable.biologix,
            R.drawable.biologix,
            R.drawable.biologix,
            R.drawable.biologix
    };


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

        search_view = (SearchView) findViewById(R.id.search_view);


        BenderaAdapter adapter = new BenderaAdapter(this, NamaNegara, GbrBendera);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String Pilihitem = NamaNegara[+position];
                Toast.makeText(getApplicationContext(), Pilihitem, Toast.LENGTH_SHORT).show();

            }
        });
        search_view.setOnQueryTextListener(this);
        //menquery data text dari widget searchview di class saat ini this = MainActivity
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        adapter.getFilter().filter(newText);//memfilter adapter pada textbaru
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false; //submit query
    }

}

我的menu.xml

<?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="10dp" >

    <!-- menampilkan widget searchview -->
    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/search_view"
        android:iconifiedByDefault="false"
        android:padding="2dp"
        android:queryHint="Cari disini...." />


    <!-- menampilkan listview -->
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</LinearLayout>

0 个答案:

没有答案