Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView Search
    text
    copied!<p>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.</p> <pre><code>/* Background Async Task to Load all person data by making HTTP Request */ class LoadPersonData extends AsyncTask&lt;String, String, String&gt; { protected String doInBackground(String... args) { List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); // 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 &lt; 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&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; 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); </code></pre> <p>// MY CODE IS WORKING WELL TILL HERE BUT WHEN I ADDED THE BELOW METHOD THE APP JUST CRASHED</p> <pre><code>// 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 } }); } }); } } } </code></pre> <p>Here is error in this field <code>AllFoundPerson.this.adapter.getFilter().filter(cs);</code>adaptor cannot be resolved or not field</p> <p>list_item_found.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/fpid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone"/&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>all_found_person.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;!-- Editext for Search --&gt; &lt;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"/&gt; &lt;!-- List View --&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>Edited:</strong></p> <p>After i added the adapter as a class member <code>ArrayAdapter&lt;String&gt; adapter;</code>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.</p> <p><img src="https://i.stack.imgur.com/n5uZ3.png" alt="enter image description here"></p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload