AutoCompleteTextView可以返回ID吗?

时间:2015-10-12 09:44:57

标签: android autocompletetextview

AutoCompleteTextView允许用户从有效值列表中选择一个字符串。就像,我想,每个想要使用这个轭的开发人员,我对用户选择的id比它的字符串标签更感兴趣。有没有办法检索所选对象的id属性,或者它在源数组中的索引?

以下C#代码允许用户从SomeObject列表中选择。我在Xamarin工作,但不要让这个让你失望。解决我在java中的问题,我很乐意让它在C#中工作

public class AutoCompleteField : PhysicalField
{
    protected AutoCompleteTextView actv;
    public AutoCompleteField(IList<SomeObject> choices, LogicalField logical, string id)
        : base(logical, id) 
    {
        _choices = choices;
    }
    protected ArrayAdapter<SomeObject> _adapter;

    public override void addToView(LayoutInflater inflater)
    {
        var ctx = App_NotMobility.CurrentActivity;
        actv = new AutoCompleteTextView(ctx);
        actv.Id = _form.generateId();

        // test choices
        var _choices = new List<SomeObject>();
        _choices.Add(new SomeObject(234, "Oranges"));
        _choices.Add(new SomeObject(456, "Apples"));
        _choices.Add(new SomeObject(789, "Bananas"));

        _adapter = new ArrayAdapter<SomeObject>(ctx, Android.Resource.Layout.SimpleDropDownItem1Line, _choices);
        actv.Adapter = _adapter;
        actv.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e)
        {
            // HOW DO I ACCESS THE ID OR THE INDEX OF USER'S SELECTION ?????????????????
        };
       _form.AddView(actv);  
    }
}
public class SomeObject
{
    public int Id { get; set; }
    public string Label { get; set; }
    public SomeObject(int id, string label)
    {
        Id = id;
        Label = label;
    }
    public override string ToString()
    {
        return Label;
    }
}

3 个答案:

答案 0 :(得分:1)

初始化适配器并过度点击项目后,您需要做的就是从您的适配器获取您单击的项目的特定位置的特定对象。 在java中,它有点类似于

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeObject someObject = (SomeObject) adapter.getItem(position);
int id = someObject.getId();
}

这就是你需要的一切。我不确定您在xamarin中的代码,如何获得该职位,因为我没有看到任何位置输入变量的方法,可能您可以将标记添加到您的视图中并且可以获取您的标记点击方法。

var position = ((View)sender).Tag;

但我建议,如果你可以创建一个类扩展基本适配器,那么你将使用方法GetView来覆盖并轻松地做你想要的。你的构造函数可以像这样开始,

List<SomeObjects> items;
Activity context;

public CustomAdapter(Activity context, List<SomeObjects> items)
        : base()
    {
        this.context = context;
        this.items = items;
    } 

答案 1 :(得分:0)

此处searchText是Autocompletetextview ..

searchText.setOnItemClickListener(new OnItemClickListener() 
        {

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

                int position = position;     //Adapter selection position

            }

        });

答案 2 :(得分:0)

Ankush的回答奏效了。我在这里发布了C#代码,因为有一些关于转换和泛型的细微之处......

public class myActv : AutoCompleteTextView, AdapterView.IOnItemClickListener
{
    PhysicalField _physical;
    public myActv(Activity ctx, PhysicalField physical) : base(ctx)
    {
        OnItemClickListener = this;
        _physical = physical;
    }

    public void OnItemClick(AdapterView parent, View view, int position, long id)
    {
         // This is the punchline...
         SomeObject whatIwant = ((ArrayAdapter<SomeObject>)this.Adapter).GetItem(position);
    }
}