【问题标题】:Android: edittext with TextChangedListener issueAndroid:带有TextChangedListener问题的edittext
【发布时间】:2013-03-05 07:15:34
【问题描述】:

我正在将电话联系人加载到列表中并在edittext上实现TextChangedListener,如下所示

editTxt.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void afterTextChanged(Editable s) {
                final TextView noDataFound = (TextView) findViewById(R.id.norecords);
                inputName = s.toString();
                if(inputName!=null&&!inputName.trim().equals("")){

                Log.d(TAG, "LoadMoreEntries --> Constants.loadEntries : "
                        + Constants.loadEntries);

                    if (Constants.loadEntries != null) {
                        Constants.loadEntries.cancel(true);
                    }

                Constants.loadEntries = new LoadEntries();
                Constants.loadEntries.execute();
            }
                Button closesearch = (Button) findViewById(R.id.closesearch);

                if (inputName != null && !inputName.trim().equals("")) {
                    closesearch.setVisibility(View.VISIBLE);
                } else {
                    closesearch.setVisibility(View.GONE);
                }

                closesearch.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (Constants.loadEntries != null) {
                            Constants.loadEntries.cancel(true);
                Constants.loadEntries = new LoadEntries();
                Constants.loadEntries.execute();
                        }else {


            }
                        return false;
                    }
                }); 

            }
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });

在这里,当用户输入正确的名称时,它会给出名称,而当他输入错误的名称时,它不会显示任何数据。 我的问题是当我输入正确的名称并擦除时,整个列表已加载但当我输入错误的名称并显示时 没有数据,删除名称时,列表没有更新。在输入名称并单击该按钮后,我也有“ x ”按钮 应该拿回我所有的清单。任何帮助表示赞赏

【问题讨论】:

    标签: android android-edittext textchanged


    【解决方案1】:

    使用 Google Places AutoComplete API 而不是实现 Textwatcher。当您开始输入并暂停时,Google Places AutoComplete API 非常有效,然后它会显示下拉列表,并且下拉列表会在每个字符处更新。

    使用它,您可以轻松更新自动完成的下拉列表。

    这是对此的解释。

    editTxt.setAdapter(new PlacesAutoCompleteAdapter(this,R.layout.yourlayout));  
    

    这里是 PlacesAutoCompleteAdapter 类,它是过滤结果并返回过滤结果。

    private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
        private ArrayList<String> resultList;
        private String[] myArray;
    
        public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }
    
        @Override
        public int getCount() {
            return myArray.length;
        }
    
        @Override
        public String getItem(int index) {
            return myArray[index];
        }
    
        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        myArray = autocomplete(constraint.toString());  // here we are calling myAutocomplete method.                    
                        // Assign the data to the FilterResults
                        filterResults.values = myArray;
                        filterResults.count = myArray.length;
                    }
                    return filterResults;
                }
    
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    }
                    else {
                        notifyDataSetInvalidated();
                    }
                }};
            return filter;
        }
    }   
    
    private String[] autocomplete(String dropdownString) {
        ArrayList<String> resultList = null;
        StringBuilder jsonResults = new StringBuilder();
        String term;
    
        try {
            term=URLEncoder.encode(dropdownString, "utf8");
        } catch (Exception e) {
            e.printStackTrace();
            term = dropdownString;
        }
    
        StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE);  
        sb.append("?param="+param1+"); // this is parameter if your getting data from server.        
        sb.append("&term="+term); // this term which you typing in edittext.  
        String url = sb.toString();
    
            // you can do here anything with your list. get it and populate it.   
    
        return myArray;
    }
    

    PLACES_API_BASE:- here is url if you are getting data from Web(in my example www.myurl/myapp).
    TYPE_AUTOCOMPLETE:- file name or exact location from where are you getting data(in my example abc.php).
    如果您有任何疑问问我。不要犹豫。

    【讨论】:

    • @Sandip.. 谢谢你的有用回答,但我想知道如何实现这个以及我在做什么错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多