android Listview onItemClick填充EditText

时间:2015-12-02 02:31:51

标签: java android android-listview onclicklistener

我正在开发一个文本语音识别的小程序,问题是语音识别正在填充listView,这很好,现在我想用ClickView项目的内容填充EditText: 我的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
        if(resultCode == RESULT_OK) {

            ArrayList<String> textMatchList = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            if (!textMatchList.isEmpty()) {
                mlvTextMatches.setAdapter(new ArrayAdapter<String>(this,
                                    android.R.layout.simple_list_item_1,
                                    textMatchList));


            }
        }

    mlvTextMatches.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            //Not Sure what to do here!
            EditText InputText = (EditText) findViewById(R.id.inptWord);
        }
    });
    super.onActivityResult(requestCode, resultCode, data);
}`

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使textMatchList成为成员变量:

ArrayList<String> textMatchList = new ArrayList<String>();

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
        if(resultCode == RESULT_OK) {
            textMatchList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    ...
    ...
    mlvTextMatches.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            EditText InputText = (EditText) findViewById(R.id.inptWord);
            InputText.setText(textMatchList.get(position));
        }
    });
}

position方法的onItemClick参数表示所点击列表项的位置。