1

イベントをトリガーしたビューを取得する方法を知っていますか? 例:

    final AutoCompleteTextView edtxInput = (AutoCompleteTextView)layout.findViewById(R.id.edtx_input);  
    edtxInput.setThreshold(2);
    edtxInput.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
            Console.debug(TAG, "view: " + arg1, Console.Liviu);             
            edtxInput.setText(((FormOption)edtxInput.getAdapter().getItem(position)).getDescription());
        }
    });

ここでの問題は、OnItemClickListener の edtxInput からのテキストを最終的なものにせずに更新する方法がわからないことです。

ありがとう

4

3 に答える 3

2

これを確認してくださいhttp://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

パラメーター

親 クリックが発生した AdapterView。

arg1 は AutoCompleteTextView (edtxInput) 自体です。あなたができるように

 AutoCompleteTextView edtxInput = (AutoCompleteTextView)layout.findViewById(R.id.edtx_input);  
    edtxInput.setThreshold(2);
    edtxInput.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
            Console.debug(TAG, "view: " + arg1, Console.Liviu); 
AutoCompleteTextView medtxInput = (AutoCompleteTextView)parent;
                medtxInput.setText(((FormOption)medtxInput.getAdapter().getItem(position)).getDescription());
            }
        });

アップデート

AutoCompleteTextViewAdapterView のサブクラスではないようです。これは、SDK の一部の「障害」です。を取得するAutoCompleteTextViewには、ハックを行うことができます

 AutoCompleteTextView medtxInput = (AutoCompleteTextView)view.getParent();

したがって、クリックされたビューを取得してから、その親であるAutoCompleteTextView

AutoCompleteTextViewしかし、そもそもなぜファイナルになりたくないのでしょうか? 特に理由は?

アダプターのみが必要な場合は、これとこれのみを記述できます

((ChildClass)parent.getItemAtPosition(position)).getDescription();
于 2012-09-19T07:50:52.273 に答える
0

edtxInput をグローバル変数として宣言して、クラス全体でこの変数にアクセスできるようにします。

于 2012-09-19T07:49:19.863 に答える
0

変数を final にするのがなぜそんなに悪いのですか? edtxInput をグローバルにする任意の方法。以下のコードを参照してください

public class myActivity extends Activity implements OnItemClickListener {

    AutoCompleteTextView mEditTextInput;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mEditTextInput = (AutoCompleteTextView)layout.findViewById(R.id.edtx_input);
        mEditTextInput.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
        Console.debug(TAG, "view: " + arg1, Console.Liviu);             
        mEditTextInput.setText(((FormOption)mEditTextInput.getAdapter().getItem(position)).getDescription());
    }
}

それで全部です。

于 2012-09-19T08:28:02.343 に答える