0

I'm trying to do a search bar that filter the listview according to the keywords that the user enters, the code has no error but it is not filtering at all. Any idea what the problem might be? I tried various methods but to no success.

oncreate

super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.layout_main);

// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
EditText search = (EditText)findViewById(R.id.EditText01);

mListAppInfo.setTextFilterEnabled(true);

// create new adapter
final AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());


// set adapter to list view  
mListAppInfo.setAdapter(adapter);


search.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) {
        Log.e("TAG", "ontextchanged");
       adapter.getFilter().filter(s); //Filter from my adapter
       adapter.notifyDataSetChanged(); //Update my view
    }
});

ArrayAdapter class

public class AppInfoAdapter extends ArrayAdapter<ApplicationInfo> {

    private Context mContext;
    PackageManager mPackManager;

    public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
        super(c, 0, list);
        mContext = c;
        mPackManager = pm;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // get the selected entry
        ApplicationInfo entry = (ApplicationInfo) getItem(position);

        Log.e("TAG", entry.toString());

        // reference to convertView
        View v = convertView;

        // inflate new layout if null
        if(v == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            v = inflater.inflate(R.layout.layout_appinfo, null);
        }

        // load controls from layout resources
        ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
        TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
        TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);

        // set data to display
        ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
        tvAppName.setText(entry.loadLabel(mPackManager));
        tvPkgName.setText(entry.packageName);

        // return view
        return v;
    }
}

2 Answers 2

1

There are two possible ways of Resolving this

1. Use your own Filtering Algorithm to filter the adapter. Refer to this blogpost for further information http://www.mokasocial.com/2010/07/arrayadapte-filtering-and-you/

2. The second and much simpler method is to override the tostring method in the Custom RowItem class you might have defined

 @Override
        public String toString() {
            return name + "\n" + description;
        }

and use the adapter.getFilter().filter(s); as such you were using it will work now because your adapter now returns a valid string to filter

Sign up to request clarification or add additional context in comments.

4 Comments

This method is not working for me and I am not sure why. Would you be willing to take a look?
Sure Tell me what is the problem you are facing
I Upvoted your answer. It's working after doing some tweaking of my own :)
The second solution does not work for me. Oddly, it does not search all fields.
0

Calling adapter.getFilter().filter(s) uses ArrayAdapter's default String filtering logic but since your list type is of ApplicationInfo, the ArrayAdapter is using ApplicationInfo#toString() for item comparison
and that doesn't look like something you want to filter.

3 Comments

So i should make my own filtering algorithm in AppInfoAdapter and override a getFilter() method?
Close but not exactly. If you want full control over the filtering algorithm your AppInfoAdapter should extend a BaseAdapter and implement the Filterable interface (lots of samples available on this site) but instead you might want to try extending the ApplicationInfo class and overriding it's toString()
What would that roughly look like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.