【问题标题】:AutoCompleteTextView force to show all items OR disable filteringAutoCompleteTextView 强制显示所有项目或禁用过滤
【发布时间】:2012-12-04 10:43:36
【问题描述】:

我管理一个 AutoCompleteTextView,根据我输入的 4 个首字母,它应该提供在我的数据库中找到的所有城镇 (ville)。

Async task 运行良好并获得正确的数据。

我的问题是DropDownList 不显示所有项目。 DB 返回的 20 个中通常只有 1、2、3 或 4 个。

所以我想,ACTV 本身应该有一些自动过滤功能! 我在这里查看了很多关于 SO 的主题,以更新我的代码,但我没有成功.... :-(

我不断收到错误,但不知道到底是什么问题! :-(

这是我的代码:

class MyActivity extends Activity implements AdapterView.OnItemClickListener
{
        static class Ville 
        {
            String id;
            String name;

            @Override
            public String toString() { return this.name; }
        };

        ArrayAdapter<Ville>    villeAdapter;
        String                 villeAdapterFilter;
        VilleUpdateTask        villeAdapterUpdateTask;
        AutoCompleteTextView   villeText;
        Ville                  selectedVille;

        final TextWatcher textChecker = new TextWatcher() {

            public void afterTextChanged(Editable s) {}

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

            public void onTextChanged(CharSequence s, int start, int before, int count)
            { 
                  MyActivity.this.setAdapterFilter(s.toString());
            }

        };

        public void onCreate(Bundle bundle)
        {
             super.onCreate(bundle);
             setContentView(R.layout.main);

             this.villeAdapter = new ArrayAdapter<Ville>(this,android.R.layout.simple_dropdown_item_1line, new Ville[0]);
             this.villeText = (AutoCompleteTextView ) findViewById(R.id.villeSelector);
             this.villeText.setAdapter(this.villeAdapter);
             this.villeText.setThreshold(THRESHOLD_DROPDOWN); 
             this.villeText.setOnItemClickListener(this);
             this.villeText.addTextChangedListener(textChecker);
        }

        public void onDestroy() { stopVilleAdapterUpdate(); 


        public void setAdapterFilter(String filter)
        {
             if (filter == null) {
                 // clearing the adapter
                 this.villeAdapterFilter = null;
                 this.villeAdapter.clear();
                 this.villeAdapter.notifyDataSetChanged();
                 Log.d("MyActivity","Clearing ville filter !");
             } else if (filter.length() > THRESHOLD_QUERY) {
                  if (this.villeAdapterFilter == null) {
                      Log.d("MyActivity","Ville Adapter Filter defined to:"+filter);
                      this.villeAdapterFilter = filter;
                      startVilleAdapterUpdate();
                  } else {
                      Log.d("MyActivity","Already filtered with:"+this.villeAdapterFilter);
                  }
             } else {
                  Log.d("MyActivity","Resetting filter (not enough data)");
                  this.villeAdapterFilter = null;
                 this.villeAdapter.clear();
                 this.villeAdapter.notifyDataSetChanged();
             }
        }

        public synchronized void onItemClick(ViewAdapter<?> ad, View v, int position, long id)
        {
             this.selectedVille = this.villeAdapter.getItemAtPosition(position);
             Log.d("MyActivity","Ville selected: "+this.selectedVille);
        }

        public synchronized void startVilleAdapterUpdate()
        {
              stopVilleAdapterUpdate();
              Log.d("MyActivity","Starting Update of Villes with "+this.villeAdapterFilter);
              this.villeAdapterUpdateTask = new VilleUpdateTask();
              this.villeAdapterUpdateTask.execute(this.villeAdapterFilter);
        }

        public synchronized void stopVilleAdapterUpdate()
        {
             if (this.villeAdapterUpdateTask != null) {
                 Log.d("MyActivity","Stopping current update of villes");
                 this.villeAdapterUpdateTask.cancel(true);
                 this.villeAdapterUpdateTask = null;
             }
        }

        public synchronized void onVilleAdapterUpdateResult(Ville[] data)
        {
             this.villeAdapterUpdateTask = null;
             if (data != null) {
                 Log.d("MyActivity","Received "+data.length+" villes from update task");
                 this.villeAdapter.clear();
                 this.villeAdapter.addAll(data);
                 this.villeAdapter.notifyDataSetChanged(); // mise à jour du drop down...
            }
        } 

        class VilleUpdateTask extends AsyncTask<String,Void,Ville[]>
        {
              public Ville[] doInBackground(String ... filters)
              {
                  ArrayList<Ville> values = new ArrayList<Ville>();
                  try  {
                       HttpClient httpclient = new DefaultHttpClient();
                       ....
                       ....
                       for(int i=0;i<json_array.length();i++) {
                           JSONObject json_ligne = json_array.getJSONObject(i);   
                           try {
                               Ville v = new Ville();
                               v.name = json_ligne.getString("NAME_VILLE");
                               v.id = json_ligne.getString("ID_VILLE");
                               values.add(v);
                           } catch (Exception ex) {
                               Log.w("VilleUpdateTask","Invalid value for Ville at index #"+i,ex);
                           }
                       }
                  } catch (Exception ex) {
                       Log.e("VilleUpdateTask","Failed to retrieve list of Ville !",ex);
                  }
                  return values.toArray(new Ville[values.size()]);
             }

             public void onPostExecute(Ville[] data)
             {
                 MyActivity.this.onVilleAdapterUpdateResult(data);
             }
        }

}

编辑 1:是的,抱歉,我的 ACTV 是基本的 TextView,这不是滚动问题,因为在更好的情况下,我可以在列表中看到 10 个项目,最后位置是随机的

编辑 2:您能帮我将现有代码调整为上述 2 个 URL 中的给定解决方案吗?

(1) 根据那个解决方案AutoCompleteTextView - disable filtering

我必须:

  • 创建我的类ClassMyACArrayAdapter,它和给定的一样,只是它的名字改变了

  • 更改我的声明

    ArrayAdapter villeAdapter;

List<ClassMyACArrayAdapter> villeAdapter;
  • 但在 onCreate 中应该用什么替换初始

    this.villeAdapter = 新的 ArrayAdapter
    (this,android.R.layout.simple_dropdown_item_1line, new Ville[0]);

【问题讨论】:

    标签: android autocomplete filtering


    【解决方案1】:

    只要您需要,请致电autoCompleteTextView.showDropDown() .....干杯:)

    【讨论】:

    • 谢谢,真的有助于在几小时后打破僵局。
    【解决方案2】:

    您的 AutoCompleteTextView 是 TextView、LinearLayout 还是 ListView?您的活动中的代码看起来不错,所以我猜测问题可能出在布局中(也许您没有使用滚动,所以您只能看到第一个值)。

    此外,您看到的值始终是返回列表中的第一个值,或者它们位于随机位置?

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多