Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

android - Determine AutoCompleteTextView from onItemClick

Is it possible to determine the originating AutoCompleteTextView from the parameters of the callback onItemClick?

I have the following callback function, which is correctly triggered if I click on an item of the selection-popup-window of the AutoCompleteTextView:

public void onItemClick(AdapterView<?> adaptView, View view, int position, 
        long id) {
}

I know, that I can implement an own per-textview listener class, but I would like to determine which AutoCompleteTextView initiated the click solely from the parameters of the callback function - is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I do not have a positive answer to the question (I think it is not possible). But I use a workaround to reach the same goal (find AutoCompleteTextView from within onItemClick):

I defined the following class, which can be used to modify the onItemClick call to provide the AutoCompleteTextView as second parameter (instead of original View param).

public class AutoCompleteTextViewClickListener implements OnItemClickListener {
       AutoCompleteTextView mAutoComplete;
       OnItemClickListener mOriginalListener;

       public AutoCompleteTextViewClickListener(AutoCompleteTextView acTextView, 
               OnItemClickListener originalListener) {
           mAutoComplete = acTextView;
           mOriginalListener = originalListener;
       }

       public void onItemClick(AdapterView<?> adView, View view, int position,
               long id) {
           mOriginalListener.onItemClick(adView, mAutoComplete, position, id);
       }       
}

This can be used in the following way:

Instead of

myTextView.setOnItemClickListener(myListener);

you'll have to write:

myTextView.setOnItemClickListener(
        new AutoCompleteTextViewClickListener(myTextView, myListener));

Now, whenever onItemClicked is triggered by the TextView, instead of the original view value the reference to the AutoCompleteTextView is available.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...