安卓:不显示列表自动提示时,选择的项目 [英] Android: Don't show autosuggest list when item is selected

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

问题描述

我在做自动完成使用TextView的在我的应用程序的一些东西。我得到的建议当用户键入任何东西。但是,我有一个查询,当用户类型和水龙头上自动完成TextView的建议列表中,则更多的建议上来。我想,当用户点击该名单上,并不想显示更多选项有限制搜索结果。我怎样才能克服这种情况。

这是我的全类,我的工作:

 公共类WikiSuggestActivity延伸活动{
公共字符串的数据;
公开名单<串GT;建议;
公共AutoCompleteTextView自动完成;
公共ArrayAdapter<串GT; aAdapter;/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    建议=新的ArrayList<串GT;();
    自动完成=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    autoComplete.addTextChangedListener(新TextWatcher(){        公共无效afterTextChanged(编辑编辑){
            // TODO自动生成方法存根        }        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                INT后){
            // TODO自动生成方法存根        }        公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
                诠释计数){
            串newText = s.toString();
            新的getJSON()执行(newText);
        }    });}一流的getJSON扩展的AsyncTask<字符串,字符串,字符串> {    @覆盖
    保护字符串doInBackground(字符串...键){
        字符串newText =键[0];
        newText = newText.trim();
        newText = newText.replace(,+);
        尝试{
            HttpClient的hClient =新DefaultHttpClient();
            HTTPGET hGet =新HTTPGET(
                    http://en.wikipedia.org/w/api.php?action=opensearch&search=
                            + newText +&功放;限= 8安培;命名空间= 0&安培;格式= JSON);
            ResponseHandler所<串GT; rHan​​dler =新BasicResponseHandler();
            数据= hClient.execute(hGet,rHandler);
            建议=新的ArrayList<串GT;();
            JSONArray jArray =新JSONArray(数据);
            的for(int i = 0; I< jArray.getJSONArray(1)。长度();我++){
                串SuggestKey = jArray.getJSONArray(1).getString(ⅰ);
                suggest.add(SuggestKey);
            }        }赶上(例外五){
            Log.w(错误,e.getMessage());
        }
        runOnUiThread(新的Runnable(){
            公共无效的run(){
                aAdapter =新ArrayAdapter<串GT;(
                        getApplicationContext(),R.layout.item,建议);
                autoComplete.setAdapter(aAdapter);
                aAdapter.notifyDataSetChanged();
            }
        });        返回null;
    }}
}

任何指导,将AP preciated。


解决方案

 公共类WikiSuggestActivity延伸活动{
    公共字符串的数据;
    公开名单<串GT;建议;
    公共AutoCompleteTextView自动完成;
    公共ArrayAdapter<串GT; aAdapter;
    公共TextWatcher mTextWatcher =新TextWatcher(){
        公共无效afterTextChanged(编辑编辑){
            // TODO自动生成方法存根
        }        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                INT后){
            // TODO自动生成方法存根
        }        公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
                诠释计数){
            串newText = s.toString();
            新的getJSON()执行(newText);
        }
    };
    公共布尔手表= TRUE;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        建议=新的ArrayList<串GT;();
        自动完成=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        autoComplete.addTextChangedListener(mTextWatcher);
        autoComplete.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                    长ARG3){
                autoComplete.removeTextChangedListener(mTextWatcher);
                aAdapter.clear();
                观看= FALSE;
            }
        });
    }    一流的getJSON扩展的AsyncTask<字符串,字符串,字符串> {        @覆盖
        保护字符串doInBackground(字符串...键){
            字符串newText =键[0];
            newText = newText.trim();
            newText = newText.replace(,+);
            尝试{
                HttpClient的hClient =新DefaultHttpClient();
                HTTPGET hGet =新HTTPGET(
                        http://en.wikipedia.org/w/api.php?action=opensearch&search=
                                + newText +&功放;限= 8安培;命名空间= 0&安培;格式= JSON);
                ResponseHandler所<串GT; rHan​​dler =新BasicResponseHandler();
                数据= hClient.execute(hGet,rHandler);
                建议=新的ArrayList<串GT;();
                JSONArray jArray =新JSONArray(数据);
                的for(int i = 0; I< jArray.getJSONArray(1)。长度();我++){
                    串SuggestKey = jArray.getJSONArray(1).getString(ⅰ);
                    suggest.add(SuggestKey);
                }            }赶上(例外五){
                Log.w(错误,e.getMessage());
            }
            如果(表)
                runOnUiThread(新的Runnable(){
                    公共无效的run(){
                        aAdapter =新ArrayAdapter<串GT;(
                            getApplicationContext(),R.layout.item,建议);
                        autoComplete.setAdapter(aAdapter);
                        aAdapter.notifyDataSetChanged();
                    }
                });            返回null;
        }    }
}

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;
    }

}
}

Any guidance will be appreciated.

解决方案

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;
        }

    }
}

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

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