title: Populating JSON result to GridView using onTextChanged
body: <p>Hi i am having a bit of problem on how to populate JSON result using onTextChange.</p>
  
  <p>What i am trying to do is to update the content of my GridView whenever the text on a specific EditText is changed.</p>
  
  <p>The following is what i have done so far:</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_search_people);
  
      /* INITIALIZES GRID ITEMS */
      gridView.setAdapter(new SearchGridAdapter(this));
      gridView.setOnItemClickListener(new OnItemClickListener(){
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                  long id) {
              // TODO Auto-generated method stub;
          }
      });
  
      TextWatcher fieldValidator = new TextWatcher(){
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before,
                  int count) {
              // TODO Auto-generated method stub
              if(longEnough()){
                  doSearch(txtSearchPpl.getText().toString().trim());
              }
          }
  
          private boolean longEnough() {
              return txtSearchPpl.getText().toString().trim().length() &gt; 3;
          }
      };
  
      txtSearchPpl.addTextChangedListener(fieldValidator);
  
  private void doSearch(String keywords){
      SearchPerson task = new SearchPerson();
      task.execute(keywords);
  }
  </code></pre>
  
  <p><strong>While my Adapter looks like this</strong>:</p>
  
  <pre><code>public class SearchGridAdapter extends ArrayAdapter&lt;Person&gt; {
  
  public SearchGridAdapter(Context context){
      super(context, R.layout.grid_item_text);
      mContext = context;
      mPeople = null;
  }
  
  public SearchGridAdapter(Context context, ArrayList&lt;Person&gt; people){
      super(context, R.layout.grid_item_text);
      mContext = context;
      mPeople = people;
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      if (mPeople == null){
          return 0;
      }else{
          return mPeople.size();
      }
  
  }
  
  @Override
  public Person getItem(int position) {
      // TODO Auto-generated method stub
      return mPeople.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
  
      LayoutInflater inflater = (LayoutInflater) mContext
              .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      View view = inflater.inflate(R.layout.grid_item_text,
              parent, false);
  
      TextView personName = (TextView) view.findViewById(R.id.textItem);
      ImageView personThumb = (ImageView) view.findViewById(R.id.iconItem);
  
      personName.setText(mPeople.get(position).fullName);
      personThumb.setImageResource(mThumbs);
      return view;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>And onPostExecute i did this:</strong></p>
  
  <pre><code>*** REST OF CODE OMITTED ***
      @Override
      protected void onPostExecute(final ArrayList&lt;Person&gt; result) {         
          runOnUiThread(new Runnable() {
              public void run() {
                  if(result != null){
                      gridView.setAdapter(new SearchGridAdapter(getApplicationContext(), result));
                  }
              }
          });
      }
  </code></pre>
  
  <p>The code above works, but it throws some unintentional behaviour. If i type "Ruby" the first time it displays the result correctly, however when i continue typing "Ruby Palmer" the activity crashed (the app is still running).</p>
  
  <p>Can anyone point me to the right direction here? Thx.</p>
  
  <p>EDIT #1:</p>
  
  <p>I removed the onUiThread and it gives me the following error:</p>
  
  <p>FATAL EXCEPTION: AsyncTask #4
  at x.x.x.SearchPeopleActivity$SearchPerson.doInBackground(SearchPeopleActivity.java:231)</p>
  
  <p>EDIT #2:</p>
  
  <p>I figured out the error on EDIT #1, now i implement a simple if checks on doInBackground()</p>
  
  <pre><code>@Override
  protected ArrayList&lt;Person&gt; doInBackground(String... params) {
      // TODO Auto-generated method stub
      String keyword = params[0];
      String resultData = ((new HttpRetriever())).searchPerson(myAccount.email, myAccount.passwd, keyword);
      Log.i("THUMBQOO", resultData);
      try {
          if(resultData != null){
              mSearchResult = (new ContactHandlers()).personResult(resultData);
          }else{
              mSearchResult = null;
          }
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          Log.e("THUMBQOO - REG PHONE", "JSON Exception");
      }
      return mSearchResult;
  }
  </code></pre>
  
  <p>However, the original problem persists. When i type "Ruby" the grid displays all name contains Ruby. However, if i continue typing Ruby Pal the activity crashed (the app is still running). And reset the GridView with no items.</p>