2016-01-31 61 views
-2

我想过滤与搜索查看JSON数据和我已经加入我的列表顶部的搜索查看如下图所示的画面......这是我的代码:如何使用searchView过滤来自Json的数据?

Allitems.java

public class Allitems extends ListActivity { 

private ProgressDialog pDialog; 
JSONParser jParser = new JSONParser(); 
ArrayList<HashMap<String, String>> itemList; 

private static String url_all_items = "http://192.168.1.12/items.php"; 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_ITEMS = "item"; 
private static final String TAG_NAME = "name"; 
private static final String TAG_PHARMACY = "pharmacy"; 

JSONArray items = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.all_items); 

    itemList = new ArrayList<HashMap<String, String>>(); 

    new LoadAllProducts().execute(); 
} 

class LoadAllProducts extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Allitems.this); 
     pDialog.setMessage("Loading ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    protected String doInBackground(String... args) { 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 

     JSONObject json = jParser.makeHttpRequest(url_all_items, "GET", params); 

     if (json != null) { 

      Log.d("All items: ", json.toString()); 

      try { 

       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 

        items = json.getJSONArray(TAG_ITEMS); 

        for (int i = 0; i < items.length(); i++) { 
         JSONObject c = items.getJSONObject(i); 

         String name = c.getString(TAG_NAME); 
         String pharmacy = c.getString(TAG_PHARMACY); 

         HashMap<String, String> map = new HashMap<String, String>(); 

         map.put(TAG_NAME, name); 
         map.put(TAG_PHARMACY, pharmacy); 

         itemList.add(map); 
        } 
       } else { 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 

    } 

    public void onPostExecute(String file_url) { 

     pDialog.dismiss(); 

     runOnUiThread(new Runnable() { 
      public void run() { 
       /** 
       * Updating parsed JSON data into ListView 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         Allitems.this, itemList, 
         R.layout.list_item, new String[]{ 
         TAG_NAME, TAG_PHARMACY}, 
         new int[]{R.id.name, R.id.pharmacy}); 
       setListAdapter(adapter); 

      } 

     }); 


    } 

} 

这里是我的列表视图看起来像 enter image description here

回答