Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically update autocomplete box in Android?

I will like to know if we can continuously call some service for fetching results and displaying in Autocomplete list.

I have one screen with the text box and when user starts entering in that textbox the autocomplete should get filled with the data. The data will not be hardcoded and will be fetched through http connection. I think I need to call http connection in onTextChanged method of Edittext but is that the perfect solution.

Moreover, should this type of implementation done in mobile application. Since, this feature is web based. Can this be done in mobile application too?

Is this feasible?

like image 340
sunil Avatar asked Aug 22 '10 16:08

sunil


1 Answers

Write a custom SimpleCursorAdapter. Now associate this adapter to your EditText. Here is the code to construct a Cursor object and return it:

public class ValueCursorAdapter extends SimpleCursorAdapter implements Filterable
{

    ...
 // overrise the newView() to associate mCursor[1] and mCursor[2] to relevant views within
    ...

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint)
    {
        MatrixCursor mCursor = new MatrixCursor(new String[] { "_id", "uri", "label" });
        .. // result = ??
            while (result.hasNext())
            {
                mCursor.addRow(new Object[] { count, "uri", "title"});
                count++;
            }
        return mCursor;
    }
}

Here is an example for Customizing Cursor Adapter. You might need to customize it to fit your requirements.

like image 110
ankitjaininfo Avatar answered Oct 29 '22 05:10

ankitjaininfo