Android自定义ListView:添加搜索功能

时间:2014-03-11 15:21:44

标签: android listview search android-edittext

我有一个问题,我希望我的ListView上方的EditText在该ListView中执行搜索。有可能吗?

这是活动代码:

public static class BirreChiareListView extends Main implements AdapterView.OnItemClickListener {

    ListView list;
    String [] birrechiare;
    int [] images={R.drawable.ichnusa, R.drawable.ichnusa_speciale, R.drawable.ichnusa_cruda};
    private MyAdapter adapter;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(birrechiare_listview);

        Resources res=getResources();
        birrechiare=res.getStringArray(R.array.birre_chiare);
        list = (ListView)findViewById(R.id.listViewBirreChiare);
        MyAdapter adapter = new MyAdapter(this,birrechiare, images);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapter, View view, int position, long l) {
        String item = adapter.getItemAtPosition(position).toString();
        Toast.makeText(getApplicationContext(), "Hai selezionato: " + item, Toast.LENGTH_SHORT).show();
        if (position == 0){
            Intent gotoclassica = new Intent(BirreChiareListView.this,Ichnusa_Classica.class);
            startActivity(gotoclassica);
        }
        if (position == 1){
            Intent gotospeciale = new Intent(BirreChiareListView.this,Ichnusa_Speciale.class);
            startActivity(gotospeciale);
        }
        if (position == 2){
            Intent gotocruda = new Intent(BirreChiareListView.this,Ichnusa_Cruda.class);
            startActivity(gotocruda);
        }
    }
}

static class MyAdapter extends ArrayAdapter<String> {

    Context context;
    int[] images;
    String[] titleArray;

    MyAdapter(Context c, String[] titles, int imgs []){
        super (c, single_row, R.id.Titletxt, titles);
        this.context = c;
        this.images=imgs;
        this.titleArray=titles;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(single_row, parent,false);
        ImageView myimage = (ImageView) row.findViewById(R.id.imageView);
        TextView mytitle = (TextView) row.findViewById(R.id.Titletxt);
        myimage.setImageResource(images[position]);
        mytitle.setText(titleArray[position]);

        return row;

    }
}

好的,这是ListView的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:background="@drawable/background_listview">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Birre Chiare"
    android:singleLine="false"
    android:textColor="#FFFFFF"
    android:gravity="center"/>

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>

<ListView
    android:id="@+id/listViewBirreChiare"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

</LinearLayout>

最后,这是single_row.xml文件:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/ichnusa"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/Titletxt"
    android:layout_alignBottom="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_marginBottom="35dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
 </RelativeLayout>

1 个答案:

答案 0 :(得分:5)

是的,这绝对是可以的。 Android为Filterable提供了ArrayAdapter界面。界面提供了getFilter()方法,您可以在其中挂钩过滤过程。

EditText中的文字发生变化时,请致电adapter.getFilter().filter("filterString")EditText的文字更改行为是使用TextWatcher上的EditText完成的。以下教程详细解释了它:

http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html

编辑:

要实施可过滤的界面,只需在ArrayAdapter

中执行此操作即可
static class MyAdapter extends ArrayAdapter<String> implements Filterable {
    private Filter myFilter;

    @Override
    public Filter getFilter() {
        if (myFilter == null)
            myFilter = new MyFilter();

        return myFilter;
    }
}

其中MyFilter是您自己的Filter类。