【问题标题】:Android Progressbar next to Autocompletetextview not able to hide appropriatelyAutocompletetextview 旁边的 Android 进度条无法正确隐藏
【发布时间】:2023-03-05 08:19:01
【问题描述】:

我有一个AutoCompleteTextViewProgressBar 在它的右边,ProgressBar 的可见性在开始时是不可见的。

我正在使用自定义类 PlacesAutoCompleteAdapter 和包含 TextView 的 xml 文件自动完成在我的活动的 onCreate 中将适配器设置为 AutocompleteTextView,

actvFrom.setAdapter(new PlacesAutoCompleteAdapter(this,
            R.layout.autocomplete));

PlacesAutocompleteAdapter 类中,我正在实现Filterable 接口。

private class PlacesAutoCompleteAdapter extends ArrayAdapter<String>
        implements Filterable {

    private ArrayList<String> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);

    }

    @Override
    public int getCount() {

        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public android.widget.Filter getFilter() {

        android.widget.Filter filter = new android.widget.Filter() {

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {

                // TODO Auto-generated method stub
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }

            }



            @Override
            protected FilterResults performFiltering(
                    final CharSequence constraint) {

                FilterResults filterResults = new FilterResults();

                if (constraint != null) {
                    // Retrieve the autocomplete results.
                    // TODO Auto-generated method stub
                    resultList = autocomplete_fill_data(constraint
                            .toString());


                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }

                return filterResults;
            }
        };

        return filter;
    }
}

在 autocomplete_fill_data 函数中,我正在调用服务器并将其添加到 ArrayList myCollection 并返回它。

public ArrayList<String> autocomplete_fill_data(String value) {

    myCollection = new ArrayList<String>();

  // call to server
    ...

     myCollection.add(result);
   return myCollection;

 }

AutoComplete 工作正常,但问题是我无法在调用服务器时显示ProgressBar

我都试过了,

  • 使用 addTextChangedListener

    autocomplete.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                if (s.length() >= 1) {
                    System.out.println("Count ="+count);
                    progressBar.setVisibility(View.VISIBLE);
    
    
                } else {
                     progressBar.setVisibility(View.INVISIBLE);
                }
    
            }
    

但问题在于,即使您停止输入文本,progressBar 仍会旋转。我想在用户停止编写文本后立即隐藏 ProgressBar。

  • 在 performFiltering 中,我尝试隐藏 progressBar,但在您键入时自动完成框卡住了。

有没有其他方法可以在用户停止输入文本时尝试隐藏进度条。

【问题讨论】:

    标签: android progress-bar autocompletetextview android-filterable


    【解决方案1】:

    你应该尝试使用 public void afterTextChanged(Editable s)

     autocomplete.addTextChangedListener(new TextWatcher() 
     {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
              progressBar.setVisibility(View.VISIBLE);
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
             progressBar.setVisibility(View.GONE);
        }
    

    【讨论】:

    • 我已经尝试过了,但是一旦你输入文本,它就会变得不可见。所以progressBar 根本不可见。
    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 2016-08-29
    • 2015-07-10
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多