Android : Add suggested data to a SearchView from a web service

on Saturday, September 27, 2014


I want to add a SearchView in my android app, and I want to suggested devices in this way:


http://queforum.com/android-applications/182251-android-how-build-gmail-like-search-box-action-bar.html


Also, I get JSON data to fill out the list from an own web service.


How can I do that? Because I've been looking for Internet and I've seen that I need a ContentProvider an a SQLite database to do it, and I'm not sure how to do it. I've only created a SearchableActivity.


SearchableActivity.java:



public class SearchableActivity extends ListActivity implements IWebServiceTaskResult{

/**
*
* */
private ListView listSuggestedDevices;
/**
*
* */
private ArrayAdapter<String> adapter;
/**
*
* */
private List<String> suggestedDevices = new ArrayList<String>();
/**
*
* */
private static String GET_SUGGESTED_DEVICES_SERVICE;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
handleIntent(getIntent());
setServiceDirections();

}

/**
*
* */
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
System.out.println("PASA onNewIntent");
setIntent(intent);
handleIntent(intent);
}

/**
*
* */
private void handleIntent(Intent intent){
System.out.println("PASA handleIntent");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}

/**
*
* */
private void doSearch(String query){
listSuggestedDevices = getListView();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, suggestedDevices);
listSuggestedDevices.setAdapter(adapter);

if (isNetworkAvailable()){
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, this);
wst.addNameValuePair("query", query);
wst.execute(GET_SUGGESTED_DEVICES_SERVICE);
}

}

@Override
public void handleResponse(String response) {
System.out.println(response);
JSONObject jso = null;
if (!response.equals("null")){
try {
jso = new JSONObject(response);
if (jso.has("status")){
return;
}

doAutoComplete(jso);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

@Override
public Context getContext() {
// TODO Auto-generated method stub
return this;
}

/**
*
* */
private void doAutoComplete(JSONObject jso){
suggestedDevices.clear();

Object obj;
try {
obj = jso.get("smartphonesList");

if (obj != null) {
if (obj instanceof JSONArray) {
JSONArray smartphones = jso.getJSONArray("smartphonesList");
for (int i = 0; i < smartphones.length(); i++) {
JSONObject smartphone = new JSONObject(smartphones.getString(i));
if (!smartphone.getString("model").equals("")) {
suggestedDevices.add(smartphone.getString("model"));
}
}
} else {
if (!jso.getString("smartphonesList").equals("")){
JSONObject smartphone = new JSONObject(jso.getString("smartphonesList"));
suggestedDevices.add(smartphone.getString("model"));
}
}

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

adapter.notifyDataSetChanged();
}


MainActivity.java:



/**
*
* */
private void configureSearchView(Menu menu){
MenuItem searchItem = menu.findItem(R.id.action_search);

SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setQueryHint(getResources().getString(R.string.searchView));
searchView.setSubmitButtonEnabled(false);
searchView.setIconifiedByDefault(true);
}


AndroidManifest.xml



<activity
android:name="findyourdevice.activities.CompareActivity"
android:screenOrientation="landscape" />
<activity android:name="findyourdevice.activities.SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>


If you need more source code, just ask for me.


Thank you!!


0 comments:

Post a Comment