使用onTextChanged将JSON结果填充到GridView

时间:2013-11-29 05:14:19

标签: android json gridview

您好我在如何使用onTextChange填充JSON结果方面遇到了一些问题。

我想要做的是每当更改特定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”时,活动崩溃了(应用程序仍在运行)。

有人能指出我正确的方向吗? THX。

编辑#1:

我删除了onUiThread,它给了我以下错误:

致命异常:AsyncTask#4 在x.x.x.SearchPeopleActivity $ SearchPerson.doInBackground(SearchPeopleActivity.java:231)

编辑#2:

我在EDIT#1上找到了错误,现在我实现了一个简单的if onDoBackground()

@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没有任何项目。

1 个答案:

答案 0 :(得分:0)

@svenoaks感谢您的建议。它的工作方式与我想要的一样。后一个问题是由于URI构建器不正确。

所以基本上这就是我所做的(删除onUiThread)

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