1

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.

enter image description here

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
android_guy
  • 236
  • 1
  • 7
  • 18
  • 1
    `onPostExecute` is invoked on the ui thread no need for `runOnUiThread` there – Raghunandan Oct 05 '13 at 10:50
  • @Raghunandan please tell me why i am getting error of adapter on this line `AllFoundPerson.this.adapter.getFilter().filter(cs);` error:adaptor cannot be resolved or not a field. What more i have to do? any tutorial or code for this? – android_guy Oct 06 '13 at 08:30
  • adapter is not a static field. Declare your adapter as a class member – Raghunandan Oct 06 '13 at 08:32
  • @Raghunandan tried this `ArrayAdapter adapter;` i see the list but when i entered a character in the search box the app just closed? and no logcat details in logcat. – android_guy Oct 06 '13 at 08:37
  • check the logcat why. – Raghunandan Oct 06 '13 at 08:39
  • @Raghunandan Logcat details are**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** – android_guy Oct 06 '13 at 08:45
  • edit your question and post the full stacktrace – Raghunandan Oct 06 '13 at 08:47
  • that is not the full stacktrace . do you see caused by part in your logcat. if so post the same – Raghunandan Oct 06 '13 at 08:59
  • @Raghunandan this is the full stacktrace and there is no othere line in my logcat, all lines i have posted here. Look at the addTextChangeListener i think something is wrong there, because the app closes when i enter a key in the search box there but i can't figure it out why it's happing. – android_guy Oct 06 '13 at 10:28
  • with the info provided i don't think i can help further – Raghunandan Oct 06 '13 at 10:30
  • here's a link http://stackoverflow.com/questions/10816243/search-in-listview-with-edittext/15367403#15367403. i posted an answer long time back. You can use that as a reference for cusotm search – Raghunandan Oct 06 '13 at 10:33
  • @Raghunandan i have done it with a little change in my code. Changed the modifier of `adapter` to `final` and instead of this `AllFoundPerson.this.adapter.getFilter().filter(cs);` i used this `((Filterable) adapter).getFilter().filter(cs);`. Thanks a lot you helped me too much. – android_guy Oct 06 '13 at 10:51

1 Answers1

0

After too much search for the solution i have done it with a little change in my code. Changed the modifier of adapter to final final ListAdapter adapter = new SimpleAdapter() and instead of this AllFoundPerson.this.adapter.getFilter().filter(cs); i used this ((Filterable) adapter).getFilter().filter(cs);

The search method in onpostExcute after change

inputSearch.addTextChangedListener(new TextWatcher() {

                            @Override
                            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                                // When user changed the Text
                               ((Filterable) adapter).getFilter().filter(cs);
                            }

                            @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                          
                            }
                        }); 
android_guy
  • 236
  • 1
  • 7
  • 18