Friday, August 7, 2015

Search in Listview Android

Search inside listview using addTextChangedListener , in listview when EditText we search then below list show related search  list data. 




See Video How it work: 



Search in Listview Using addTextChangedListener :

MainActivity.this

This Activity contain search function create listview and fill array and search them and show in new array temp arrat name. below complete source code.

public class MainActivity extends Activity{

ListView listSearch;
CustomAdapter adapter;
ArrayList<Model>  list;
EditText searchText ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listSearch=(ListView) findViewById(R.id.listSearch);

searchText=(EditText) findViewById(R.id.searchText);

         /* fill array list*/

list=new ArrayList<Model>();

list.add(new Model("Afghanistan"));
list.add(new Model("Algeria"));
list.add(new Model("American"));
list.add(new Model("Andorra"));
list.add(new Model("Anguilla"));
list.add(new Model("Australia"));
list.add(new Model("Bermuda"));
list.add(new Model("Bhutan"));
list.add(new Model("Canada North America"));
list.add(new Model("India"));
list.add(new Model("Egypt"));
list.add(new Model("Ethiopia"));
list.add(new Model("Finland"));
list.add(new Model("Indonesia"));
list.add(new Model("Iran"));
list.add(new Model("Libya"));
list.add(new Model("Japan"));
list.add(new Model("Malaysia"));
list.add(new Model("Mauritius"));
list.add(new Model("Mongolia"));
list.add(new Model("Israel"));
list.add(new Model("Ireland"));
list.add(new Model("Kuwait"));
list.add(new Model("Liberia"));
list.add(new Model("Mexico"));
list.add(new Model("Nepal"));
list.add(new Model("Kenya"));
list.add(new Model("New Zealand"));
list.add(new Model("Pakistan"));
list.add(new Model("Poland"));
list.add(new Model("Russia"));
list.add(new Model("South Africa"));
list.add(new Model("Turkey"));
list.add(new Model("Uganda"));
list.add(new Model("Tunisia"));
list.add(new Model("Taiwan"));
list.add(new Model("United States North America"));
list.add(new Model("Thailand"));
list.add(new Model("Wallis and Futuna Islands"));
list.add(new Model("Zimbabwe"));
list.add(new Model("Zambia"));
list.add(new Model("Vanuatu"));
list.add(new Model("Vietnam"));


                   /* set adapter here */
adapter=new CustomAdapter(MainActivity.this, R.layout.list_row, list);
listSearch.setAdapter(adapter);

             /* search here using addtextChangeListener*/

searchText.addTextChangedListener(new TextWatcher() {

@SuppressLint("DefaultLocale")
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
String getText=searchText.getText().toString().trim();

final ArrayList<Model> temp=new ArrayList<Model>();

            /* new temp array create and after searching it fill only sarching value*/
temp.clear();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getName().toUpperCase().contains(getText.toUpperCase())) {

Model odel=new Model(list.get(i).getName());

temp.add(odel);
}
}
                            /* adapter refresh and again set adapter*/

adapter.notifyDataSetChanged();
adapter=new CustomAdapter(MainActivity.this, R.layout.list_row, temp);
listSearch.setAdapter(adapter);

                              /* list item click that show after search*/
listSearch.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String name=temp.get(arg2).getName();

Toast.makeText(MainActivity.this, name, 1000).show();
}
});

}

@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
}
});

/* click list item */
listSearch.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String name=list.get(arg2).getName();
Toast.makeText(MainActivity.this, name, 1000).show();
}
});
}
}



CustomAdapter 

Simple Array Adapter show data in listview.

public class CustomAdapter extends ArrayAdapter<Model>{

private Context context;
private ArrayList<Model> list;
private int Layout = 0;

public CustomAdapter(Context contx, int resource,
ArrayList<Model> objects) {
super(contx, resource, objects);
// TODO Auto-generated constructor stub
context = contx;
Layout = resource;
list = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;

if (convertView == null) {
viewHolder = new ViewHolder();

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.inflate(Layout, null);

convertView.setTag(viewHolder);

viewHolder.name = (TextView) convertView
.findViewById(R.id.name);

} else {
viewHolder = (ViewHolder) convertView.getTag();
}
try {

viewHolder.name.setText(list.get(position).getName());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return convertView;
}

class ViewHolder{
TextView name;
}

}

Model  Class: value set and get here.

public class Model {

public String name="";

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Model(String name_) {
// TODO Auto-generated constructor stub
name=name_;
}

}

Listview Layout For Searching:

listview xml layout for searching EditText and ListView, 



activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#81C784" >

    <EditText
        android:id="@+id/searchText"
        android:layout_width="fill_parent"
        android:padding="15dp"
        android:textColor="#000000"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/listSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     
       android:cacheColorHint="@android:color/transparent"
        android:layout_below="@+id/searchText" >
    </ListView>
</RelativeLayout>

Listview Row Layout:

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_select"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:textColor="#000000"
        android:textSize="15sp" />

</LinearLayout>



Note: If Any Issue to implement this code then Please comment :

Thank You 

No comments :

Post a Comment