Determine AutoCompleteTextView from onItemClick

后端 未结 4 1281
灰色年华
灰色年华 2020-12-20 04:14

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

I have the following callback function, which is co

相关标签:
4条回答
  • 2020-12-20 04:41

    Based on @talklesscodemore answer, I solved it this way (I had 2 AutoCompleteTextView's, you have to adapt the following if there are more):

    AutoCompleteTextView firstDropdown;
    AutoCompleteTextView secondDropdown;
    
    ...
    
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isAdapterViewFromFirstDropdown(adapterView)) {
           // Code specific for 1st ACTV
        } else {
           // Code specific for 2nd ACTV
        }
    }
    
    private boolean isAdapterViewFromFirstDropdown(AdapterView<?> adapterView) {
        return adapterView.getAdapter().equals(firstDropdown.getAdapter());
    }
    
    0 讨论(0)
  • 2020-12-20 04:49

    I use a simpler version of yonoyjoy's answer. It only passes the AutoCompleteTextView to the constructor.

    public class AutoCompleteTextViewClickListener implements AdapterView.OnItemClickListener {
        private AutoCompleteTextView mView;
    
        public AutoCompleteTextView getView() {
            return mView;
        }
    
        public AutoCompleteTextViewClickListener(AutoCompleteTextView view) {
            this.mView = view;
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        }
    }
    

    Then in your Activity, you just override onItemClick() like you would in a normal OnClickListener, but use this.getView() to get your ACTV:

    autoCompleteTextView.setOnItemClickListener(new AutoCompleteTextViewClickListener(v){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            super.onItemClick(parent, view, position, id);
            AutoCompleteTextView v = this.getView();
    
            //Do stuff.
    
        }
    });
    
    0 讨论(0)
  • 2020-12-20 05:00

    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.

    0 讨论(0)
  • 2020-12-20 05:00

    If you want to know which currentClickedAutoCompleteTextView between autoCompleteTextView1 and autoCompleteTextView2, just check their adapter ...

    public void onItemClick(AdapterView<?> adaptView, View view, int position, long id) {
        AutoCompleteTextView currentClickedAutoCompleteTextView;
        if( adaptView.getAdapter()==autoCompleteTextView1.getAdapter() )
            currentClickedAutoCompleteTextView = autoCompleteTextView1;
        else
            currentClickedAutoCompleteTextView = autoCompleteTextView2;
    }
    
    0 讨论(0)
提交回复
热议问题