MultiAutoCompleteTextView

MultiAutoCompleteTextView

我陷入一个问题,我有一个MultiAutoCompleteTextView,如下图所示:

在这种情况下,我从MultiAutoCompleteTextView的建议中选择城市名称,然后在ListView中添加该项目(如下所示)。我想防止输入其他条目而不是ADAPTER值。



我用这种方式绑定代码,而onClickButton用逗号分隔值:

private void initComp() { //              GET DATA FROM DATABASE

    multiSelectCities = (MultiAutoCompleteTextView) findViewById(R.id.activity_multiselect_city);
    btnGetCity = (Button) findViewById(R.id.activity_btn_slect_city);
    listCity = (ListView) findViewById(R.id.activity_get_city_list);
    btnGetCity.setOnClickListener(this);
    dbHelper = new DatabaseHelper(getApplicationContext());
    dbHelper.open();
    setName();
    // dbHelper.close();
}

public void setName() { // FOR SET THE NAME IN MULTISELECT TEXTVIEW
    final Cursor localcursor = dbHelper.getArea();

    if ((localcursor != null) && (localcursor.getCount() > 0)) {
        arrayOfString = new ArrayList < String > ();
        localcursor.moveToFirst();
        do {
            arrayOfString.add(localcursor.getString(localcursor.getColumnIndex(DatabaseHelper.CITY_NAME)) + "-" + localcursor.getString(localcursor.getColumnIndex(DatabaseHelper.CITY_COUNTRY)));
        } while (localcursor.moveToNext());

        this.multiSelectCities.setThreshold(3);
        adapter = new CityDataWithMultitextAdapter(this, arrayOfString);
        multiSelectCities.setAdapter(adapter);
        multiSelectCities.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        /* localcursor.close(); */

    }


    @Override
    public void onClick(View v) { //SEARCH BUTTON CLICK AND STORE THE DATA INTO LISTVIEW

        if (!Utils.isEmpty(multiSelectCities)) {
            arrayList = multiSelectCities.getText().toString().substring(0, multiSelectCities.length() - 1).split(",");
            for (int i = 0; i < arrayList.length; i++) {
                selectedItems.add(arrayList[i].trim());
            }
        } else {
            Utils.displayToast(this, "Please Enter City/Cities");
        }

        CityListAdapter adapter = new CityListAdapter(this, selectedItems);
        listCity.setAdapter(adapter);
        multiSelectCities.setText("");
        listCity.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView <? > parent, View view, int position, long id) {

                Intent i = new Intent(CityListDataActivity.this, CityListDetail.class);
                i.putExtra("CITY_NAME", selectedItems.get(position).toString());
                startActivity(i);

            }
        });

    }
}


我正在考虑通过SELECT查询与数据库一起检查输入的值,但是我有75000多个记录,因此这很难解决。



有帮助吗?它将不胜感激。

最佳答案

一种方法是消除使用以下命令在AutoCompleteTextView内部编写的可能性:

<AutoCompleteTextView
            android:editable="false"
            android:inputType="none"
    ..../>


以及onTouch呼叫显示适配器,它将显示城市列表。

 private AutoCompleteTextView mAutoText;

 mAutoText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mAutoText.showDropDown();
                mAutoText.setListSelection(mPos);
                return false;
            }


        });
mAutoText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mAdapter.setSelection(position);
            mPos = position;

        }
    });

09-27 17:43