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) {
Hope this helps.
source share