android - 自动完成 TextView getSelectedItemPosition

标签 android adapter autocompletetextview

我需要确定用户从适配器中选择了哪个项目(当出现下拉建议时)。

如果用户选择项目,我希望能够获得:

  • 位置如果选择项目 - 但我需要关于适配器中所有项目的位置
  • 某种所选项目的唯一标识符
  • 如果用户没有选择我的任何建议并键入他自己的文本,我将只收到 null 或 -1 或其他内容。

这可能与 AutoCompleteTextView 相关吗?

例子:

我的 AutoCompleteTextView 有标准的 ArrayAdapter,包含以下项目:

{"one","one2","two","two2", "three", "three2"}

用户输入

thr

他被建议 2 个选项:

three, three2

然后他选择“three2”。 当 OnItemSelected 被触发时,“位置”参数设置为 1,因为只有 2 个建议。

但是,我想要的是获得 5 的位置,因为我的适配器共有 6 个项目。

最佳答案

这是我为类似问题所做的工作。

我有使用适配器来设置自动完成 TextView 。

这是我的PlacesAutoCompleteAdapter.java 文件

package com.inukshk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.inukshk.CreateInukshk.CreateInukshk;

public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements
        Filterable {
    private ArrayList<String> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public String getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    // Retrieve the autocomplete results.

                    resultList = CreateInukshk.autocomplete(constraint
                            .toString());

                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }
}

这里我使用了resultList = CreateInukshk.autocomplete(constraint .toString()); 这是我的方法,它将返回我想要显示的数组列表。

最后这是我在主 java 文件中初始化我们的 AutoCompleteTextview 的代码。

内部 OnCreate();

autoCompView = (AutoCompleteTextView) findViewById(R.id.editloc);
        autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this,
                R.layout.list_item));
        // autoCompView.setOnItemClickListener(CreateInukshk.this);

        autoCompView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                            // here you can get value of arg2 as your position while selecting value.
                // Place = autoCompView.getText().toString();
                new AsyncGetAutoPlace().execute(autoCompView.getText()
                        .toString().trim());
            }
        });

代替 新的 AsyncGetAutoPlace().execute(autoCompView.getText() .toString().trim());您可以随意添加代码。

希望对你有所帮助。

关于android - 自动完成 TextView getSelectedItemPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121493/

相关文章:

android - 在 AutoCompleteTextView 中设置值

android - 键盘隐藏 AutoCompleteTextView 下拉菜单

缩放按钮的Android自定义形状

javascript - Chrome(适用于 Android)媒体查询做错了?

android - 从非 Activity 类调用 Activity

android - 更新 ListView 内的 ImageView

android - 如何为android添加语言支持

android - 如何在 Firebase 中添加数组值

android - 从 ViewPager 返回 Activity 结果