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

3
我管理一个AutoCompleteTextView,应该根据我输入的前四个字母在我的数据库中提供所有的城镇(ville)。 Async task工作正常并获取正确的数据。
我的问题是DropDownList没有显示所有的项。通常只有20个返回的项中的1、2、3或4个。
所以我想到,ACTV本身应该有一些自动过滤!
我查看了这里的许多主题,更新了我的代码,但我没有成功... :-(
我不知道问题出在哪里,一直收到错误提示! :-(
链接如下:
- AutoCompleteTextView 强制显示所有项目 - AutoCompleteTextView - 禁用过滤 以下是我的代码:
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:你能帮我调整现有的代码以适应上述两个链接给出的解决方案吗?

(1)根据该解决方案AutoCompleteTextView - 禁用过滤

  • 创建与给定类相同的类 ClassMyACArrayAdapter ,只是名称不同

  • 将我的声明从

    ArrayAdapter villeAdapter;

改为

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

    这里需要使用以下代码:

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

2个回答

7
只需要在需要的时候调用autoCompleteTextView.showDropDown()即可……干杯 :)

谢谢,真的帮助我打破僵局,经过了几个小时。 - Deven

0
你的AutoCompleteTextView是TextView、LinearLayout还是ListView?你的activity中的代码看起来很好,所以我猜问题可能出在布局上(也许你没有使用滚动条,所以只能看到第一个值)。
此外,你看到的值总是返回列表中的第一个值,还是随机位置的值?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接