Jump to content
Android Forum - A Community For Android Users and Enthusiasts

search the item in list view


Recommended Posts

i have a list view in which i am having data from data base and filling it using custom adapter now i want add search functon in it..

plz help me


here is the code what i have tried


import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.TextView;
import android.widget.Toast;

import com.virinchisoftware.adconnect.R;

public class UDUserListAdapter extends ArrayAdapter {

    private ArrayList lid, lname, lclass, lcat, ldate, ltype;
    Context ctx;
    int item;
    private LayoutInflater mInflate;
    private int mResourceId;
    TextView userId,userName, indexno, logindate,useredit,userdelete;
    private ArrayList data;
    private ArrayList originalList;
    private static LayoutInflater inflater=null;
    private Activity activity;
    SubMenuSelectedListener mListener;
    UserFilter filter;
    public UDUserListAdapter(Activity c,int a,ArrayList data){
         this.data = new ArrayList();
           this.originalList = new ArrayList();
        this.activity =c;
        mInflate = (LayoutInflater) activity

    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflate.inflate(R.layout.uduserlist, null);
        userId = (TextView) convertView.findViewById(R.id.textView1);
        userName = (TextView) convertView.findViewById(R.id.textView2);
        indexno = (TextView) convertView.findViewById(R.id.textView3);
        logindate = (TextView) convertView.findViewById(R.id.textView4);
        useredit = (TextView) convertView.findViewById(R.id.textView6);
        userdelete = (TextView) convertView.findViewById(R.id.textView7);
        try {
            mListener = (SubMenuSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnURLSelectedListener");
        UserDataBase lessondb=data.get(position);
        return convertView;

    public int getCount() {
        return data.size();

    public UserDataBase getItem(int position) {
        return null;

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;

    public Filter getFilter() {
        // TODO Auto-generated method stub
        filter=new UserFilter();
        return filter;
private class UserFilter extends Filter{

    protected FilterResults performFiltering(CharSequence arg0) {
        // TODO Auto-generated method stub
        arg0 = arg0.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if(arg0 != null && arg0.toString().length() > 0)
            ArrayList userlist= new ArrayList();
            for(int i = 0, l = originalList.size(); i             {
                UserDataBase udb = originalList.get(i);
            result.count = userlist.size();
            result.values = userlist;
          result.values = originalList;
          result.count = originalList.size();
        return result;

    protected void publishResults(CharSequence arg0, FilterResults arg1) {
        // TODO Auto-generated method stub
         data = (ArrayList)arg1.values;
            for(int i = 0, l = data.size(); i              add(data.get(i));



and this is the class where i'm using search function



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.uduser, container, false);
        informationlist = new ArrayList();
        sortByclass = (Spinner) view.findViewById(R.id.sortbyclass);
        adduser = (ImageView) view.findViewById(R.id.ivadduserIcon);
        deleteuser = (ImageView) view.findViewById(R.id.ivdeleteuserIcon);
        listItems=(ListView) view.findViewById(R.id.lvUserTableItems);
        search=(EditText) view.findViewById(R.id.etsearchuser);
        sortclassAdapter = new ArrayAdapter(getActivity(),
                android.R.layout.simple_spinner_item, sortclass);

         //enables filtering for the contents of the given ListView
        return view;

    private void User() {
        // TODO Auto-generated method stub
        Cursor cursor = adcdb.queueUser();
        if (cursor != null && cursor.getCount() > 0) {
            // startManagingCursor(cursor);
            Log.d("Select Query USer", String.valueOf(cursor.getCount()));

            for (int i = 0; i
                UserDataBase userdatabse = new UserDataBase();




            ldlessonlistAdapter = new UDUserListAdapter(getActivity(),R.layout.uduserlist, informationlist);



    public void onSaveInstanceState(Bundle outState) {

    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.ivadduserIcon:
            mListener.newUserSelected("New User");
        case R.id.etsearchuser:
            search.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub



  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...