Android 使用SimpleAdapter筛选listView数据
我正在使用simpleAdapter在列表视图中显示名称。我想添加一个edittext小部件,允许用户筛选他们想要查找的名称。然而,我不知道如何做到这一点。有人能解释一下我是怎么做到的吗。这是我的密码:Android 使用SimpleAdapter筛选listView数据,android,Android,我正在使用simpleAdapter在列表视图中显示名称。我想添加一个edittext小部件,允许用户筛选他们想要查找的名称。然而,我不知道如何做到这一点。有人能解释一下我是怎么做到的吗。这是我的密码: import java.util.ArrayList; import java.util.HashMap; import java.util.jar.Attributes.Name; import org.apache.http.NameValuePair; im
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);
}
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.jar.Attributes.Name;
导入org.apache.http.NameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Dialog;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.EditText;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.SimpleCursorAdapter;
导入android.widget.TextView;
导入android.widget.Toast;
公共类活动{
私有静态最终字符串ID=“ID”;
私有静态最终字符串NAME=“NAME”;
私有静态最终字符串PATIENTS=“PATIENTS”;
私人对话;
私人编辑文本搜索;
//获取患者信息的URL
私有静态最终字符串URL=”http://10.0.2.2:8080/HMS2/patients.php";
列表适配器;
//用于获取解析数据
JSONParser jParser=新的JSONParser();
//JSON数组
JSONArray患者=null;
ArrayList患者列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局,所有患者);
search=(EditText)findViewById(R.id.inputSearch);
新建LoadPatients().execute();
//选择单个列表视图
ListView list=getListView();
list.isTextFilterEnabled();
list.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(适配器视图arg0、视图视图、内部位置、,
长id){
字符串名称=((TextView)findViewById(R.id.name)).getText().toString();
}
});
}
类LoadPatients扩展异步任务
{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=新建进度对话框(Patients.this);
setMessage(“加载患者信息…”);
dialog.setUndeterminate(false);
对话框。可设置可取消(false);
dialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//ListView的HashMap
patientsList=新建ArrayList();
HashMap=newHashMap();
ArrayList query=新建ArrayList();
//获取JSON对象
JSONObject json=jParser.getJSONFromUrl(URL,查询,“POST”);
Log.e(“error”,json.toString());
尝试
{
//获取要删除的JSON数组
patients=json.getJSONArray(patients);
//循环遍历JSON数组
对于(int x=0;x值位置
JSONObject key=patients.getJSONObject(x);
//获取id值
int value=key.getInt(ID);
字符串id=Integer.toString(值);
//获取名称值
字符串名称=key.getString(名称);
//将key=>值放入HashMap中
地图。放置(名称、名称);
地图放置(ID,ID);
//将HashMap值添加到ArrayList
patientsList.add(地图);
}
}
捕获(JSONException e)
{
Toast.makeText(getApplicationContext(),“连接问题”,Toast.LENGTH\u LONG);
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
//获取所有产品后关闭对话框
dialog.dismise();
adapter=new simpledapter(getApplicationContext(),patientsList,R.layout.list_项,
新字符串[]{NAME},新int[]{R.id.NAME});
setListAdapter(适配器);
}
}
}
最好的方法可能是在EditText小部件中添加一个,然后使用SimpleAdapter实现的接口。不过,这可能有点困难,因此您可能会发现,通过为适配器创建一个新列表,并在用户键入时,将该列表中的值设置为与搜索查询匹配的原始列表中的值,您自己就可以更轻松地处理筛选。每次更新适配器使用的列表时,您都希望使数据无效,因此将重新创建该列表 -你应该看看这一点,这样就没有其他更简单的方法了吗?你对哪一部分特别有问题?@Chris:simpledapter
已经实现了Filterable
,因此,这样一个适配器的扩展可以覆盖相关的方法。你能给我一个这样做的例子吗?我是android新手development@MH:接得好,我以前没注意到。我的答案已经更新以反映这一点。谢谢