2012-11-13 55 views
0

我使用simpleAdapter在listView中顯示名稱。我想添加一個edittext小部件,以允許用戶篩選他們想要查找的名稱。但是,我不知道如何做到這一點。有人可以解釋我如何做到這一點。這裏是我的代碼:使用SimpleAdapter篩選listView數據

import java.util.ArrayList; 
    import java.util.HashMap; 
    import java.util.jar.Attributes.Name; 

    import org.apache.http.NameValuePair; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.app.Dialog; 
    import android.app.ListActivity; 
    import android.app.ProgressDialog; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.EditText; 
    import android.widget.ListAdapter; 
    import android.widget.ListView; 
    import android.widget.SimpleAdapter; 
    import android.widget.SimpleCursorAdapter; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class Patients extends ListActivity{ 

    private static final String ID = "id"; 
    private static final String NAME = "name"; 
    private static final String PATIENTS = "patients"; 


    private ProgressDialog dialog; 

     private EditText search; 

    //URL to get the patient info 
    private static final String URL = "http://10.0.2.2:8080/HMS2/patients.php"; 

    ListAdapter adapter; 

    //for getting the parse data 
     JSONParser jParser = new JSONParser(); 


    // patients JSON ARRAY 
    JSONArray patients = null; 

    ArrayList<HashMap<String, String>> patientsList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.all_patients); 

    search = (EditText) findViewById(R.id.inputSearch); 

    new LoadPatients().execute(); 

    //selecting single listView 
    ListView list = getListView(); 

    list.isTextFilterEnabled(); 

    list.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View view, int position, 
       long id) { 

      String name = ((TextView) findViewById(R.id.name)).getText().toString(); 
     } 

    }); 

} 

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

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     dialog = new ProgressDialog(Patients.this); 
     dialog.setMessage("Loading patients info..."); 
     dialog.setIndeterminate(false); 
     dialog.setCancelable(false); 
     dialog.show(); 
    } 


    @Override 
    protected String doInBackground(String... params) { 

     //HashMap for ListView 
     patientsList = new ArrayList<HashMap<String,String>>(); 

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


     ArrayList<NameValuePair> query = new ArrayList<NameValuePair>(); 

     //gets the JSON object 
     JSONObject json = jParser.getJSONFromUrl(URL, query, "POST"); 

     Log.e("error",json.toString()); 

     try 
      { 
      //gets the JSON array which is patients 
      patients = json.getJSONArray(PATIENTS); 

      //loop through the JSON array 
      for(int x= 0; x < patients.length(); x++) 
      { 
       //gets the key=> value position 
       JSONObject key = patients.getJSONObject(x); 

       //gets the id value 
       int value = key.getInt(ID); 
       String id = Integer.toString(value); 

       //gets the name value 
       String name = key.getString(NAME); 

       //put the key => value in a HashMap 
       map.put(NAME, name); 
       map.put(ID, id); 

       //add the HashMap values to the ArrayList 
       patientsList.add(map); 

      } 
      } 
     catch (JSONException e) 
     { 
      Toast.makeText(getApplicationContext(), "Connection problem", Toast.LENGTH_LONG); 
     } 


     return null; 
    } 


    @Override 
    protected void onPostExecute(String result) 
    { 
     super.onPostExecute(result); 

      //dismiss the dialog after getting all products 
      dialog.dismiss(); 


      adapter = new SimpleAdapter(getApplicationContext(),patientsList,R.layout.list_item, 
         new String[]{NAME}, new int[]{R.id.name}); 

      setListAdapter(adapter);    

     } 


    } 

    } 
+0

http://developer.android.com/guide/topics/search/search-dialog.html - 你應該看看這個 – omi0301

+0

下車然後將向 – ballerz

回答

0

最好的辦法可能是一個TextWatcher添加到的EditText小部件,然後利用Filterable接口,通過SimpleAdapter實現的。儘管這可能是一種痛苦,所以您可能會發現自己更容易處理過濾,通過爲您的適配器創建新列表,並且在用戶鍵入時,將該列表中的值設置爲與搜索查詢匹配的原始列表。每次更新適配器使用的列表時,您都希望使數據無效,因此將重新創建列表。

+0

那麼好吧感謝 – ballerz

+0

所以沒有其他更簡單的方法來做到這一點? – ballerz

+0

哪一部分特別是你有麻煩? – Chris