从Web服务向SearchView添加建议数据

时间:2014-09-27 12:13:21

标签: android web-services searchview

我想在我的Android应用中添加一个SearchView,我想用这种方式建议设备:

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

此外,我从自己的网络服务中获取JSON数据以填写列表。

我该怎么做?因为我一直在寻找互联网,而且我已经看到我需要一个ContentProvider和一个SQLite数据库来做这件事,而且我不知道该怎么做。我只创建了一个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>

如果您需要更多源代码,请找我。

谢谢!

0 个答案:

没有答案