2个AutoCompleteTextView,如何知道已选择哪个视图 [英] 2 AutoCompleteTextView's, how to know which view has been selected
问题描述
似乎是重复,但不是.
我的布局中有2个AutoCompleteTextView
.从地址,到地址.我需要知道其中哪个人启动了下拉列表.
I have 2 AutoCompleteTextView
's in my layout. From Address, To Address. I need to know which one of them initiated the drop down list.
由于两者都使用相同的商品布局,所以我重复了此尝试并尝试过,仍然没有运气.
Since both were using the same item layout, I duplicated it and tried, still no luck.
AutoCompleteTextView
的初始化
fromAutoComplete = new AutoComplete(this, R.layout.fromautocomplete);
fromAutoComplete.setNotifyOnChange(true);
fromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress);
fromAddress.setAdapter(fromAutoComplete);
fromAddress.setOnItemClickListener(this);
fromAddress.setOnItemSelectedListener(this);
toAutoComplete = new AutoComplete(this, R.layout.toautocomplete);
toAutoComplete.setNotifyOnChange(true);
toAddress = (AutoCompleteTextView) findViewById(R.id.toAddress);
toAddress.setAdapter(toAutoComplete);
toAddress.setOnItemClickListener(this);
toAddress.setOnItemSelectedListener(this);
请注意R.layout.to自动完成和R.layout.from自动完成
两者与下面相同.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:minLines="3"
android:layout_marginBottom="15dp"
android:textSize="15dp"
android:text="sample text"
android:marqueeRepeatLimit="marquee_forever"/>
为了调试这些值,我放置了一些Log语句
To debug the values, I have put some Log statements
@Override
public void onItemSelected(AdapterView<?> parent, View who, int position, long id) {
Log.e("Taxeeta", ">"+parent.getId()+":"+who.getId()+":"+id) ;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("Taxeeta", parent.getId()+":"+view.getId()+":"+id) ;
}
日志输出为
07-02 17:24:14.773:E/Taxeeta(12103):-1:-1:0
07-02 17:24:14.773: E/Taxeeta(12103): -1:-1:0
坦率地说,这很烂,为什么id的parent,view和id返回如此无用的值?
Frankly, this just sucks, why would id's for parent, view and id return such useless values ?
我在这里错过了一些非常基本的东西.
I am missing something really basic here.
推荐答案
我想我明白了.答案是2折.
I think I figured it out. The answer is 2 fold.
-
onItemClick
返回的id
值对于第一次自动完成返回 0 ,对于第二次自动完成返回 1
The
id
value returned byonItemClick
returns 0 for the first autocomplete, and 1 for the second autocomplete
07-02 17:24:14.773:E/Taxeeta(12103):-1:-1:0
07-02 17:24:14.773: E/Taxeeta(12103): -1:-1:0
07-02 17:24:14.773:E/Taxeeta(12103):-1:-1:1
07-02 17:24:14.773: E/Taxeeta(12103): -1:-1:1
布局文本视图的应该具有ID.这是在onItemClick
对于FromLayout
For FromLayout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fromautocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:minLines="3"
android:layout_marginBottom="15dp"
android:textSize="15dp"
android:text="sample text"
android:marqueeRepeatLimit="marquee_forever"/>
对于ToLayout
For ToLayout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toautocomplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:minLines="3"
android:layout_marginBottom="15dp"
android:textSize="15dp"
android:text="sample text"
android:marqueeRepeatLimit="marquee_forever"/>
这篇关于2个AutoCompleteTextView,如何知道已选择哪个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!