Android:选择项目时不显示自动建议列表 [英] Android: Don't show autosuggest list when item is selected

查看:49
本文介绍了Android:选择项目时不显示自动建议列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用自动完成的textview做一些事情.用户输入任何内容时都会得到建议.但是,我有一个查询,当用户键入并点击自动完成文本视图上的建议列表时,就会出现更多建议.我想在用户点击列表时限制搜索结果,并且不想在那里显示更多选项.我该如何克服这种情况.

I was doing some stuff using autocomplete textview in my application. I'm getting suggestion when user types anything. But, I have one query, when user types and taps on the suggested list on the autocomplete textview, then more suggestions comes up. I want to limit the search results when user taps on the list and don't want to show more options there. How can I overcome this situation.

这是我正在上的完整课程,

This is my full class, which I'm working on:

public class WikiSuggestActivity extends Activity {
public String data;
public List<String> suggest;
public AutoCompleteTextView autoComplete;
public ArrayAdapter<String> aAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    suggest = new ArrayList<String>();
    autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    autoComplete.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            String newText = s.toString();
            new getJson().execute(newText);
        }

    });

}

class getJson extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... key) {
        String newText = key[0];
        newText = newText.trim();
        newText = newText.replace(" ", "+");
        try {
            HttpClient hClient = new DefaultHttpClient();
            HttpGet hGet = new HttpGet(
                    "http://en.wikipedia.org/w/api.php?action=opensearch&search="
                            + newText + "&limit=8&namespace=0&format=json");
            ResponseHandler<String> rHandler = new BasicResponseHandler();
            data = hClient.execute(hGet, rHandler);
            suggest = new ArrayList<String>();
            JSONArray jArray = new JSONArray(data);
            for (int i = 0; i < jArray.getJSONArray(1).length(); i++) {
                String SuggestKey = jArray.getJSONArray(1).getString(i);
                suggest.add(SuggestKey);
            }

        } catch (Exception e) {
            Log.w("Error", e.getMessage());
        }
        runOnUiThread(new Runnable() {
            public void run() {
                aAdapter = new ArrayAdapter<String>(
                        getApplicationContext(), R.layout.item, suggest);
                autoComplete.setAdapter(aAdapter);
                aAdapter.notifyDataSetChanged();
            }
        });

        return null;
    }

}
}

任何指导将不胜感激.

推荐答案

public class WikiSuggestActivity extends Activity {
    public String data;
    public List<String> suggest;
    public AutoCompleteTextView autoComplete;
    public ArrayAdapter<String> aAdapter;
    public TextWatcher mTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable editable) {
            // TODO Auto-generated method stub
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            String newText = s.toString();
            new getJson().execute(newText);
        }
    };
    public boolean watch = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        suggest = new ArrayList<String>();
        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        autoComplete.addTextChangedListener( mTextWatcher );
        autoComplete.setOnItemClickListener( new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                autoComplete.removeTextChangedListener( mTextWatcher );
                aAdapter.clear();
                watch = false;
            }
        });
    }

    class getJson extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... key) {
            String newText = key[0];
            newText = newText.trim();
            newText = newText.replace(" ", "+");
            try {
                HttpClient hClient = new DefaultHttpClient();
                HttpGet hGet = new HttpGet(
                        "http://en.wikipedia.org/w/api.php?action=opensearch&search="
                                + newText + "&limit=8&namespace=0&format=json");
                ResponseHandler<String> rHandler = new BasicResponseHandler();
                data = hClient.execute(hGet, rHandler);
                suggest = new ArrayList<String>();
                JSONArray jArray = new JSONArray(data);
                for (int i = 0; i < jArray.getJSONArray(1).length(); i++) {
                    String SuggestKey = jArray.getJSONArray(1).getString(i);
                    suggest.add(SuggestKey);
                }

            } catch (Exception e) {
                Log.w("Error", e.getMessage());
            }
            if( watch )
                runOnUiThread(new Runnable() {
                    public void run() {
                        aAdapter = new ArrayAdapter<String>(
                            getApplicationContext(), R.layout.item, suggest);
                        autoComplete.setAdapter(aAdapter);
                        aAdapter.notifyDataSetChanged();
                    }
                });

            return null;
        }

    }
}

这篇关于Android:选择项目时不显示自动建议列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆