AutoCompleteTextView强制显示所有项目或禁用过滤

时间:2012-12-17 10:33:47

标签: android autocomplete filtering

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

Async task效果很好并获得正确的数据。

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

所以我发现,ACTV本身应该有一些自动过滤功能! 我在这里检查了许多主题,以更新我的代码,但我没有成功......: - (

我一直在弄错,却不知道到底是什么问题! : - (

所以这是我的代码:

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 = new ArrayAdapter
    (这,android.R.layout.simple_dropdown_item_1line,new Ville [0]);

2 个答案:

答案 0 :(得分:3)

只要你需要它就致电autoCompleteTextView.showDropDown() .....欢呼:)

答案 1 :(得分:0)

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

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