如何通过EditText过滤ListView

时间:2021-01-26 18:26:53

Im making an eBook like application for android and i want to filter the title of the book but everytime that you put a word or sentence in the edittext it will search the content of the books... can someone help me with this...

我正在制作一本类似Android的应用程序的电子书,我希望过滤书籍的标题,但每次你在编辑文本中放置一个单词或句子,它将搜索书籍的内容...有人可以帮我这个...

3 个解决方案

#1


14  

try this one,whenever u enter text in the edittext ,the list will show filtered result as i have shown the stuff in images

尝试这个,每当你在edittext中输入文本时,列表将显示过滤结果,因为我已经在图像中显示了东西

Initial:

初始:

如何通过EditText过滤ListView

Filtered:

过滤:

如何通过EditText过滤ListView
this is main.xml

这是main.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" >

<EditText 
    android:id="@+id/etSearchbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvFirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

this is FilterListActivity.java

这是FilterListActivity.java

package com.filterlist;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListActivity extends Activity{

EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);

adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);   
lvFirst.setAdapter(adapter1);

etSearchbox.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        FilterListActivity.this.adapter1.getFilter().filter(arg0);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }
});




}
}

#2


4  

I have a working example, try this:

我有一个工作示例,试试这个:

filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);

TextWatcher filterTextWatcher = new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {  

        }  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
            adapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub              
        }  
    };

The adapter must implements Filterable

适配器必须实现Filterable

            @Override
            public Filter getFilter() {
//              Filter filter = null;

                if(filter == null)
                    filter = new CheeseFilter();
                return filter;
            }

And the filter class:

和过滤器类:

        public class CheeseFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                constraint = constraint.toString().toLowerCase();

                FilterResults newFilterResults = new FilterResults();

                if (constraint != null && constraint.length() > 0) {


                    ArrayList<String> auxData = new ArrayList<String>();

                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).toLowerCase().contains(constraint))
                            auxData.add(data.get(i));
                    }

                    newFilterResults.count = auxData.size();
                    newFilterResults.values = auxData;
                } else {

                    newFilterResults.count = data.size();
                    newFilterResults.values = data;
                }

                return newFilterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                ArrayList<String> resultData = new ArrayList<String>();

                resultData = (ArrayList<String>) results.values;

                EfficientAdapter adapter = new EfficientAdapter(context, resultData);
                list.setAdapter(adapter);

//              notifyDataSetChanged();
            }

        }

You can check this post for more info:

您可以查看此帖子了解更多信息:

Filter expandableList

过滤expandableList

Filter ListView

过滤ListView

#3


0  

You need write textChangedlistener (or) TextWatcher for the edittext. Inside the listener you need to write search logic. Here is APIdoc. Here is an example on textwatcher.

您需要为edittext编写textChangedlistener(或)TextWatcher。在监听器内部,您需要编写搜索逻辑。这是APIdoc。这是textwatcher的一个例子。

#1


14  

try this one,whenever u enter text in the edittext ,the list will show filtered result as i have shown the stuff in images

尝试这个,每当你在edittext中输入文本时,列表将显示过滤结果,因为我已经在图像中显示了东西

Initial:

初始:

如何通过EditText过滤ListView

Filtered:

过滤:

如何通过EditText过滤ListView
this is main.xml

这是main.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" >

<EditText 
    android:id="@+id/etSearchbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvFirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

this is FilterListActivity.java

这是FilterListActivity.java

package com.filterlist;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListActivity extends Activity{

EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);

adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);   
lvFirst.setAdapter(adapter1);

etSearchbox.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        FilterListActivity.this.adapter1.getFilter().filter(arg0);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }
});




}
}

#2


4  

I have a working example, try this:

我有一个工作示例,试试这个:

filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);

TextWatcher filterTextWatcher = new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {  

        }  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
            adapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub              
        }  
    };

The adapter must implements Filterable

适配器必须实现Filterable

            @Override
            public Filter getFilter() {
//              Filter filter = null;

                if(filter == null)
                    filter = new CheeseFilter();
                return filter;
            }

And the filter class:

和过滤器类:

        public class CheeseFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                constraint = constraint.toString().toLowerCase();

                FilterResults newFilterResults = new FilterResults();

                if (constraint != null && constraint.length() > 0) {


                    ArrayList<String> auxData = new ArrayList<String>();

                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).toLowerCase().contains(constraint))
                            auxData.add(data.get(i));
                    }

                    newFilterResults.count = auxData.size();
                    newFilterResults.values = auxData;
                } else {

                    newFilterResults.count = data.size();
                    newFilterResults.values = data;
                }

                return newFilterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                ArrayList<String> resultData = new ArrayList<String>();

                resultData = (ArrayList<String>) results.values;

                EfficientAdapter adapter = new EfficientAdapter(context, resultData);
                list.setAdapter(adapter);

//              notifyDataSetChanged();
            }

        }

You can check this post for more info:

您可以查看此帖子了解更多信息:

Filter expandableList

过滤expandableList

Filter ListView

过滤ListView

#3


0  

You need write textChangedlistener (or) TextWatcher for the edittext. Inside the listener you need to write search logic. Here is APIdoc. Here is an example on textwatcher.

您需要为edittext编写textChangedlistener(或)TextWatcher。在监听器内部,您需要编写搜索逻辑。这是APIdoc。这是textwatcher的一个例子。