2

根据我输入的 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 - 禁用过滤

我必须:

  • 创建我的类ClassMyACArrayAdapter,它与给定的相同,只是它的名称发生了变化

  • 更改我的声明

    ArrayAdapter villeAdapter;

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

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

4

2 回答 2

7

只要你需要它就打电话autoCompleteTextView.showDropDown().....干杯:)

于 2014-02-06T11:40:05.083 回答
0

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

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

于 2012-12-17T10:56:21.173 回答