Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill AutoCompleteTextView using BaseAdapter android

Tags:

android

can someone please give me an link of filling AutoCompleteTextView using BaseAdapter in android for phone contacts.

Thanks

like image 412
Rishi Avatar asked Dec 16 '22 15:12

Rishi


2 Answers

user750716 is wrong. You can fill AutoCompleteTextView with BaseAdapter. You just need to remember that BaseAdapter has to implement Filterable.

In Adapter create your ArrayList of Objects. Implement getView with whatever View you want and fill it with objects.get(position) info. Implement getItem(int position) returning a string (name of clicked object)

In the same adapter add stuff for filtering:

public Filter getFilter() {
    return new MyFilter();
}

private class MyFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence filterString) {
        // this will be done in different thread 
        // so you could even download this data from internet

        FilterResults results = new FilterResults();

        ArrayList<myObject> allMatching = new ArrayList<myObject>()

        // find all matching objects here and add 
        // them to allMatching, use filterString.

        results.values = allMatching;
        results.count = allMatching.size();

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        objects.clear();

        ArrayList<myObject> allMatching = (ArrayList<myObject>) results.values;
        if (allMatching != null && !allMatching.isEmpty()) {
            objects = allMatching;
        }

        notifyDataSetChanged();
    }

}
like image 102
Mark Avatar answered Dec 27 '22 01:12

Mark


This is not possible with an BaseAdapter but you can use a CursorAdapter with no SQLite DB in the background. In the function runQueryOnBackgroundThread you can create an MatrixCursor. Something like this:

   String[] tableCols = new String[] { "_id", "keyword" };
   MatrixCursor cursor = new MatrixCursor(tableCols);
   cursor.addRow(new Object[] { 1, "went" });
   cursor.addRow(new Object[] { 2, "gone" });

I have done this in my morphological analyzer.

like image 33
user750716 Avatar answered Dec 27 '22 01:12

user750716