2013-11-29 57 views
0

您好我有一點問題如何使用onTextChange填充JSON結果。使用onTextChanged填充JSON結果到GridView

我想要做的是每當特定EditText上的文本被更改時更新我的​​GridView的內容。

以下是我迄今所做的:

MainActivity

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<?> 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() > 3; 
     } 
    }; 

    txtSearchPpl.addTextChangedListener(fieldValidator); 

private void doSearch(String keywords){ 
    SearchPerson task = new SearchPerson(); 
    task.execute(keywords); 
} 

雖然我的適配器看起來像這樣

public class SearchGridAdapter extends ArrayAdapter<Person> { 

public SearchGridAdapter(Context context){ 
    super(context, R.layout.grid_item_text); 
    mContext = context; 
    mPeople = null; 
} 

public SearchGridAdapter(Context context, ArrayList<Person> 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; 
} 

}

而onPostExecute我這樣做:

*** REST OF CODE OMITTED *** 
    @Override 
    protected void onPostExecute(final ArrayList<Person> result) {   
     runOnUiThread(new Runnable() { 
      public void run() { 
       if(result != null){ 
        gridView.setAdapter(new SearchGridAdapter(getApplicationContext(), result)); 
       } 
      } 
     }); 
    } 

上面的代碼工作,但它會拋出一些無意的行爲。如果我第一次輸入「Ruby」,它會正確顯示結果,但是當我繼續輸入「Ruby Palmer」時,活動崩潰了(應用程序仍在運行)。

任何人都可以指出我正確的方向嗎?謝謝。

編輯#1:

我刪除了onUiThread,它給我下面的錯誤:

致命異常:#的AsyncTask 4 在xxxSearchPeopleActivity $ SearchPerson.doInBackground(SearchPeopleActivity.java:231)

EDIT#2:

我想出上EDIT#1的錯誤,現在我實現一個簡單的,如果上doInBackground)檢查(

@Override 
protected ArrayList<Person> 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; 
} 

但是,原來的問題仍然存在。當我輸入「Ruby」時,網格顯示包含Ruby的所有名稱。但是,如果我繼續輸入Ruby Pal,則活動會崩潰(應用程序仍在運行)。並沒有任何項目重置GridView。

+0

得到onPostExecute擺脫runOnUiThread的,它已經稱爲UI線程上,並使其立即在UI線程上運行而不是排隊,這可能會導致你的問題。 –

+0

LogCat在崩潰時會說什麼,發佈它。 –

+0

嗨,我確實刪除了runOnUiThread,現在應用程序崩潰了。有關Log Cat Error的信息,請參閱編輯#1。 – Jeremy

回答

0

@svenoaks感謝您的建議。它按我希望的那樣工作。後面的問題是由於不正確的URI生成器。

所以基本上這是我做過什麼(去除onUiThread)

@Override 
    protected void onPostExecute(final ArrayList<Person> result) { 
     if(result != null){ 
      gridView.setAdapter(new SearchGridAdapter(getApplicationContext(), result)); 
     } 
    }