Android MultiAutoCompleteTextView:除Adapter以外的值

时间:2015-06-15 17:32:07

标签: android multiautocompletetextview

我遇到一个问题,即我有一个MultiAutoCompleteTextView,如下图所示:

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

enter image description here

我以这种方式绑定代码onClick Button我用逗号分隔值:

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+条记录,因此难以解决此问题。

enter image description here

任何帮助?它会得到赞赏。

1 个答案:

答案 0 :(得分:1)

一种方法是删除使用以下内容在AutoCompleteTextView内写入的可能性:

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

onTouch调用show adapter,它将显示城市列表。

 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;

        }
    });