首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoCompleteTextView getSelectedItemPosition

AutoCompleteTextView getSelectedItemPosition
EN

Stack Overflow用户
提问于 2012-10-29 12:19:00
回答 3查看 2K关注 0票数 1

我需要识别哪一项是由用户从适配器中选择的(当下拉建议出现时)。

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

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

这与AutoCompleteTextView有关吗?

示例:

我的AutoCompleteTextView有标准的AutoCompleteTextView,包括以下内容:

代码语言:javascript
复制
{"one","one2","two","two2", "three", "three2"}

用户类型

thr

他被建议有两种选择:

三,three2

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

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

EN

回答 3

Stack Overflow用户

发布于 2012-10-29 12:44:22

以下是我对这个简单问题所做的工作。

我已经使用适配器来设置自动完成文本视图。

这是我的PlacesAutoCompleteAdapter.java文件

代码语言:javascript
复制
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(约束.toString());,这是我的方法,它将返回我想要显示的数组列表。

最后,这里是我在主java文件中的代码,在这里我已经将我们的AutoCompleteTextview降级了。

内部OnCreate();

代码语言:javascript
复制
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();。

希望它能帮到你。

票数 1
EN

Stack Overflow用户

发布于 2012-10-29 12:34:32

你试过设置OnItemSelectedListener吗?

票数 0
EN

Stack Overflow用户

发布于 2015-02-17 05:08:38

代码语言:javascript
复制
String str_numbers = your_autocompletetext_view.gettext();
int exact_postion;

// here your_List is a list which you have assigned for your_autocompletetext_view
for(int i0; i<your_List.size();i++)
{

    if(str_numbers.equals(your_List.get(i).toString))
     {
       exact_postion=i; // this is your exact position
        break;
  }     
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13121493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档