Don't know if people still need this. Just in case, for future searchers. I had a problem with my SearchView's suggestions, which did not show up. After searched around, I found a solution for this. I used Volley for my content provider class (to get the suggestions from web api), which seemed not to fit naturally into Android's content provider framework. I also found that my content provider DOES run in the UI thread. Therefore, if I used Volley's Future synchronously in it, it slowed down (blocked) the UI until the request returned (timeout). In addition, I found information around the web that Volley's Future request should be run in other thread, such as in an Async task, for it to work well. Thus, it didn't solve my problem, because if I'd have to use it (in an async task), i would have used Volley's normal (async) request in the first place instead (which was what I used then).
What I did was this:
In my ContentProvider subclass, I define a listener interface:
public interface ResultListener {
void onProviderResult(Cursor mCursor);
void onProviderError(String errorMsg);
}
In my activity (which implemented LoaderCallbacks), I implemented also above interface.
In my singleton application class, I define a static variable which is used as transient data, together with its getter/setter methods:
private static HashMap transientData = new HashMap();
public static Object getTransientData(String objectName) {
return transientData.get(objectName);
}
public static void setTransientData(String objectName, Object object) {
transientData.put(objectName, object);
}
- now the logic:
In the activity, before calling getSupportLoaderManager().initLoader(...),
I called MyApplication.setTransientData("requestor", this) :
In my content provider's class, in the volley request's onResponse callback, I did this:
public void onResponse(JSONArray response){
...
ResultListener requestor = (ResultListener)TheApplication.getTransientData("requestor");
if (requestor!=null) requestor.onProviderResult(mCursor);
}
So that when the volley request returned, it will trigger the requestor's callback method, passing it the cursor filled with data from the response, and in turn the requestor (the activity) notified the cursor adapter by calling:
adapter.swapCursor(c);
adapter.notifyDataSetChanged();
Hope this helps someone.
Be blessed.