我正在使用来自本地主机的 json 数据更新列表视图,它运行良好,显示所有数据都来自服务器。现在我正在尝试在此活动中添加搜索方法。尝试了一些代码,但它给出了一个错误,不知道如何处理它。请帮我应用搜索方法。
/* Background Async Task to Load all person data by making HTTP Request
*/
class LoadPersonData extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url, "GET", params);
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
person = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < person.length(); i++) {
JSONObject c = person.getJSONObject(i);
// Storing each one in variable
String mp_id = c.getString(TAG_PID);
String mp_name = c.getString(TAG_NAME);
String mp_gender = c.getString(TAG_GENDER);
String mp_age = c.getString(TAG_REPAGE);
String repoter_name = c.getString(TAG_REPNAME);
String repoter_contact = c.getString(TAG_REPPHONE);
// creating HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, mp_id);
map.put(TAG_NAME, "Name: "+ mp_name);
map.put(TAG_GENDER, "Gender: "+ mp_gender);
map.put(TAG_REPAGE, "Age: "+ mp_age);
map.put(TAG_REPNAME, "Report Person: "+ repoter_name);
map.put(TAG_REPPHONE, "Reprt Person#: "+ repoter_contact);
// adding HashList to ArrayList
personList.add(map);
}
} else {
// no person from json
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
AddNewPerson.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllFoundPerson.this, productsList,
R.layout.list_item_found, new String[] { TAG_PID,
TAG_NAME, TAG_GENDER, TAG_REPAGE, TAG_REPNAME, TAG_REPPHONE},
new int[] { R.id.fpid, R.id.namefound, R.id.genderfound,R.id.agefound, R.id.repoter_found, R.id.repoterphone_found });
setListAdapter(adapter);
// 我的代码在这里运行良好,但是当我添加以下方法时,应用程序崩溃了
// Applying Search method
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
AllFoundPerson.this.adapter.getFilter().filter(cs); // Error in adapter, adator is not a field
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
});
}
}
}
这是此字段中的错误AllFoundPerson.this.adapter.getFilter().filter(cs);
适配器无法解析或未字段
list_item_found.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/fpid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/namefound"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textColor="#FF00FF"
android:textStyle="bold" />
<TextView
android:id="@+id/genderfound"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/agefound"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/repoter_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/repoterphone_found"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingLeft="6dp"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
all_found_person.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter a name to Search.."
android:inputType="textVisiblePassword"/>
<!-- List View -->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
编辑:
在我将适配器添加为类成员ArrayAdapter<String> adapter;
后,错误消失了,但是当我运行应用程序时,我看到了列表,但是当在搜索框中输入任何键时,应用程序刚刚崩溃,这是我的 logcat 详细信息。
10-06 13:47:30.880: D/AndroidRuntime(4069): Shutting down VM
10-06 13:47:30.880: W/dalvikvm(4069): threadid=1: thread exiting with uncaught exception (group=0xb3fcd4f0)
10-06 13:47:30.880: D/AndroidRuntime(4069): procName from cmdline: com.DRMS.disas_recovery
10-06 13:47:30.880: E/AndroidRuntime(4069): in writeCrashedAppName, pkgName :com.DRMS.disas_recovery
10-06 13:47:30.880: D/AndroidRuntime(4069): file written successfully with content: com.DRMS.disas_recovery StringBuffer : ;com.DRMS.disas_recovery