How to filter user list in android

I use one user-defined list view to extract data from json parsing, now I have one edittext that is used to search for items listed in the list, how can I achieve seach functionality. Any help would be appreciated.

ListView_res_name_main_cetegires = (ListView) findViewById(R.id.list_costome_list_main_cetegories); Custome_res_main_cetegories customAdapter_res_name_main_cetegiores; if(language.equalsIgnoreCase("English")) { customAdapter_res_name_main_cetegiores = new Custome_res_main_cetegories(getApplicationContext(), array_str_name_eng,language); }else { customAdapter_res_name_main_cetegiores = new Custome_res_main_cetegories(getApplicationContext(), array_str_name_arab,language); } ListView_res_name_main_cetegires.setAdapter(customAdapter_res_name_main_cetegiores ); 
+4
source share
3 answers

If you use EditText for search, you need to write your own filtering method. Here is an example of a Custom ListView Search . If you cannot get it, let me know, I will help you.

0
source

You should not filter the ListView, but the adapter,

 Adapter adapter = new CustomAdapter(); ListView_res_name_main_cetegires.setAdapter(adapter); 

for downloaded data:

 adapter.setData(your data); 

when searching:

 adapter.filter(searchQuery) 

in setData, filter methods do not forget to call notifyDataSetChanged ()

0
source

The following is an example implementation of the search function as a list. First, add the edit text, which you will enter into the text that will be searched as a list. Below is the rest of the code:

 **main.xml** <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="Search"> </EditText> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </LinearLayout> 

Action Code:

 import java.util.ArrayList; 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 ListViewSearchExample extends Activity { private ListView lv; private EditText et; private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN" }; private ArrayList<String> array_sort= new ArrayList<String>(); int textlength=0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) findViewById(R.id.ListView01); et = (EditText) findViewById(R.id.EditText01); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listview_array)); et.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { // Abstract Method of TextWatcher Interface. } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // Abstract Method of TextWatcher Interface. } public void onTextChanged(CharSequence s, int start, int before, int count) { textlength = et.getText().length(); array_sort.clear(); for (int i = 0; i < listview_array.length; i++) { if (textlength <= listview_array[i].length()) { if(et.getText().toString().equalsIgnoreCase( (String) listview_array[i].subSequence(0, textlength))) { array_sort.add(listview_array[i]); } } } lv.setAdapter(new ArrayAdapter<String> (ListViewSearchExample.this, android.R.layout.simple_list_item_1, array_sort)); } }); } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1501633/