我正在使用 simpleAdapter 在 listView 中显示名称。我想添加一个edittext小部件以允许用户过滤他们想要查找的名称。但是,我不知道该怎么做。有人可以解释我怎么能做到这一点。这是我的代码:
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);
}
}
}