从onItemClick确定AutoCompleteTextView

时间:2015-09-03 12:36:53

标签: android textview

是否可以从回调onItemClick的参数确定原始AutoCompleteTextView?

我有以下回调函数,如果我单击AutoCompleteTextView的selection-popup-window中的项目,则会正确触发:

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

我知道,我可以implement an own per-textview listener class,但我想确定哪个AutoCompleteTextView仅根据回调函数的参数启动了点击 - 这可能吗?

4 个答案:

答案 0 :(得分:3)

我对这个问题没有肯定的答案(我认为这是不可能的)。但我使用一种解决方法来达到相同的目标(从onItemClick中查找AutoCompleteTextView):

我定义了以下类,可用于修改onItemClick调用以提供AutoCompleteTextView作为第二个参数(而不是原始View参数)。

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

可以通过以下方式使用:

而不是

myTextView.setOnItemClickListener(myListener);

你必须写:

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

现在,只要TextView触发onItemClicked,而不是原始视图值,就可以使用对AutoCompleteTextView的引用。

答案 1 :(得分:2)

如果你想知道autoCompleteTextView1和autoCompleteTextView2之间的currentClickedAutoCompleteTextView,只需检查他们的适配器......

public void onItemClick(AdapterView<?> adaptView, View view, int position, long id) {
    AutoCompleteTextView currentClickedAutoCompleteTextView;
    if( adaptView.getAdapter()==autoCompleteTextView1.getAdapter() )
        currentClickedAutoCompleteTextView = autoCompleteTextView1;
    else
        currentClickedAutoCompleteTextView = autoCompleteTextView2;
}

答案 2 :(得分:0)

基于@talklesscodemore回答,我用这种方式解决了(我有2 AutoCompleteTextView's,如果还有更多,你必须调整以下内容):

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

答案 3 :(得分:0)

我使用yonoyjoy答案的简单版本。它只会将AutoCompleteTextView传递给构造函数。

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

然后在“活动”中,就像在普通的OnClickListener中一样,您只需覆盖onItemClick(),但是使用this.getView()来获取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.

    }
});