Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can anyone guide me on how to update a custom list view with search result data while using baseadapter.

My code is

                public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                String searchString = editText.getText().toString();
                int textLength = searchString.length();

                searchResults.clear();
                for (int i = 0; i < name1.size(); i++) {
                    String peoplename = name1.get(i).toString();
                    Log.d(TAG, "onTextChanged:" + peoplename.length());
                    if (textLength <= peoplename.length()) {
                        Log.d(TAG, "people left: " + peoplename.length());
                        if (searchString.equalsIgnoreCase(peoplename.substring(
                                0, textLength))) {
                            searchResults.add(name1.get(i));
                            Log.d(TAG, "searchresult: " + searchResults);
}

Any help is appreciated.. Thanks

share|improve this question
For searching in listView refer this link – CRUSADER yesterday
No i can already search in the listView. I can display the result in the logcat but cannot update it in the list view – user2511882 yesterday
After efery search result, you have to update base adapter source list. – tgrll yesterday
can you post a snippet of how to do that? – user2511882 yesterday

2 Answers

You should add this code snippet

private TextWatcher searchTextWatcher = new TextWatcher() {
    @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // ignore
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // ignore
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d(Constants.TAG, "*** Search value changed: " + s.toString());
            adapter.getFilter().filter(s.toString());
        }
    };

and then..

@Override
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint);
                myData = (List<MyDataType>) results.values;
                MyCustomAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint);
                List<MyDataType> filteredResults = getFilteredResults(constraint);

                FilterResults results = new FilterResults();
                results.values = filteredResults;

                return results;
            }
        };
    }

This will manipulate data in your adapter as per text changed..

referance

Hope this is helpful

share|improve this answer
do you mean something like this? public void onTextChanged(CharSequence s, int start, int before,int count) { contacts.this.ma.getFilter().filter(s); String searchString = editText.getText().toString(); int textLength = searchString.length(); – user2511882 yesterday
That was for simple adapter,, I just went thorgh your code which read baseadapter.. updated the answer – CRUSADER yesterday

Take an AutoCompleteTextView and set an adapter to it.

Then in the adapter ,

       package com.example.sample.Adapters;

       import java.util.ArrayList;

        import android.content.Context;
        import android.util.Log;
         import android.view.View;
        import android.view.ViewGroup;
       import android.widget.ArrayAdapter;
       import android.widget.Filter;

      import com.example.sample.Webservice.ResponseBean.GetAllLocationResponse.Location;
         public class LocationAdapters extends ArrayAdapter<String> {

       private MyFilter mFilter; // my personal filter: this is very important!!

       ArrayList<Location> mObjects ;
       ArrayList<String> list = new ArrayList<String>();
       ArrayList<Location> postCode = new ArrayList<Location>();

      public LocationAdapters(Context context, int resource,
        int textViewResourceId, ArrayList<Location> objects) {

    super(context, resource, textViewResourceId);

    mObjects = objects;

        }

      @Override
     public int getCount() {
    return list.size();
     }

      @Override
      public String getItem(int position) {
    try{
        return list.get(position);  
    }
    catch (Exception e) {
        notifyDataSetChanged();
        return "";
    }

}




   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    if(position < postCode.size())
    {
        view.setTag(postCode.get(position));
    }

    return view;
  }

   @Override
   public View getDropDownView(int position, View convertView, ViewGroup parent) {
    View view =  super.getDropDownView(position, convertView, parent);

    if(position < postCode.size())
    {
        view.setTag(postCode.get(position));
    }

    return view ;
    }

    @Override
    public Filter getFilter() {
    if(mFilter == null){
        mFilter = new MyFilter();
    }

    return mFilter;
        }

   // the trick is here!
    private class MyFilter extends Filter {

    ArrayList<String> tempResult = null;

    // "constraint" is the string written by the user!
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        // no constraint => nothing to return

        if ((constraint == null) || (constraint.length() == 0)) {
            synchronized (tempResult) {
                tempResult = new ArrayList<String>();
                postCode = new ArrayList<Location>(); 
                results.values = tempResult;
                results.count = tempResult.size();
            }
        } else {
            String constr = constraint.toString();
            tempResult = new ArrayList<String>();
            postCode = new ArrayList<Location>();
            for(Location location : mObjects){
                                                       if(location.postcode.toLowerCase().contains(constr.toLowerCase()) || location.town.toLowerCase().contains(constr.toLowerCase())){

                String loc = location.town.toString();
                    if(loc.contains(",")){
                        String[] splittedLoc =loc.split(",");
                        tempResult.add(splittedLoc[0]+", "+location.county);
                        postCode.add(location);


                    }else {
                    tempResult.add(location.town+", "+location.county);
                    postCode.add(location);

                }
                }
            }
            results.values = tempResult;
            results.count = tempResult.size();

        }

        return results;

    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        list = (ArrayList<String>) results.values;
        if (results.count > 0) {
            notifyDataSetChanged();
            Log.e("notify", constraint.toString());
        } else {
            notifyDataSetInvalidated();
        }
    }
}

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.