How to add a search to List View

Initially design a xml file as show below:


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

Then write the following code in your activity:-


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 LSearch extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"Android","Cupcake","Donut","Eclairs","AndroidPeople","Froyo","Apple","Samsung","Storeage","Sony"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv1=(ListView)findViewById(R.id.ListView01);
        ed=(EditText)findViewById(R.id.EditText01);
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
        ed.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
        int count) {

        textlength=ed.getText().length();
        arr_sort.clear();
        for(int i=0;i<lv_arr.length;i++)
        {
        if(textlength<=lv_arr[i].length())
        {
        if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength)))
        {
        arr_sort.add(lv_arr[i]);
        }
        }
        }

        lv1.setAdapter(new ArrayAdapter<String>(LSearch.this,android.R.layout.simple_list_item_1 , arr_sort));

        }
        });
    }
}

your output will look as shown below:




Search & Download lots of Eclipse-Android Tutorials:-
  Using Pdf Wallet you can find useful and free Android Tutorials.


Search 

Please Select File-Format

PDF  - eBooks in pdf format.
PPT  - Presentations:2003.
PPTX- Presentations:2007+.
DOC - Microsoft Word Files:2003.
DOCXMicrosoft Word Files:2007+.
XLS  - Excel File format:2003.
XLSX - Excel File Format:2007+.
CHM - Compiled Help Files.
RTF  - Rich Text format.
TXT  - Text Files. 

No comments:

Post a Comment