I am updating list view with json data from localhost, it works well shows all the data comes from server. Now i am trying to add a search method in this activity. Tried some code but it gives an error don't know how to handle it. Please help me applying the search method.
/* 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);
// MY CODE IS WORKING WELL TILL HERE BUT WHEN I ADDED THE BELOW METHOD THE APP JUST CRASHED
// 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
}
});
}
});
}
}
}
Here is error in this field AllFoundPerson.this.adapter.getFilter().filter(cs);
adaptor cannot be resolved or not field
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>
Edited:
After i added the adapter as a class member ArrayAdapter<String> adapter;
the error has gone but when i run the app i see the list but when enter any key in the search box the app just gone crashed here is my logcat details.
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