Android custom ListView: adding search function

情到浓时终转凉″ 提交于 2019-12-20 02:31:04

问题


i've a problem, i want an EditText above my ListView that performs a search in that ListView. Is it possible to do?

This is the Activity code:

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;

    }
}

Ok, this is the XML file of the ListView:

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

At the end, this is the single_row.xml file:

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


Yes this is definitely possible to do. Android provides a Filterable interface for ArrayAdapters. The interface provides a getFilter() method where you can hook into the filtering process.

When the text changes in your EditText, you call adapter.getFilter().filter("filterString"). The text changing behaviour for the EditText is done by using a TextWatcher on the EditText. This following tutorial, explains it in good detail:

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

EDIT:

To implement the filterable interface, just do this in your ArrayAdapter:

static class MyAdapter extends ArrayAdapter<String> implements Filterable {
    private Filter myFilter;

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

        return myFilter;
    }
}

where MyFilter is your own Filter class.



来源:https://stackoverflow.com/questions/22329983/android-custom-listview-adding-search-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!