1

我遇到了一个问题,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 Query 使用数据库检查输入的值,但我有 75000 多条记录,因此很难解决这个问题。

在此处输入图像描述

有什么帮助吗?将不胜感激。

4

1 回答 1

1

一种方法是消除在 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;

        }
    });
于 2015-06-15T18:00:40.607 回答