2 AutoCompleteTextView's, how to know which vi

2019-08-04 01:40发布

问题:

It may seem like a duplicate, but it ain't.

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.

Initialization of AutoCompleteTextView's

    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);

Notice the R.layout.toautocomplete and R.layout.fromautocomplete

Both are identical as below.

<?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"/>

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) ;
}

Output of log is

07-02 17:24:14.773: E/Taxeeta(12103): -1:-1:0

Frankly, this just sucks, why would id's for parent, view and id return such useless values ?

I am missing something really basic here.

回答1:

I think I figured it out. The answer is 2 fold.

  1. The id value returned by onItemClick 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:1

  2. The layout textview's should have id's. That is returned in the view parameter of onItemClick

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"/>

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"/>