0

在我的程序中,用户单击按钮,这会弹出一个提示用户说些什么的弹出窗口。在用户说出一个短语或单词后,将显示列出的可能单词。我的问题是,如何选择所选值并将其用作程序中的变量。!

可能值的列表视图

这是我的代码:

public class MainActivity extends Activity {

Button listen; 
ListView lv;
static final int check = 1111;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView)findViewById(R.id.listView1); 
    listen = (Button)findViewById(R.id.button2);


}


public void touched2(View v){ 
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.
    LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak up!");
    startActivityForResult(i, check);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (requestCode == check && resultCode == RESULT_OK){

    ArrayList results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }

    super.onActivityResult(requestCode, resultCode, data); 
    }
      }

提前谢谢你的帮助!

4

1 回答 1

0

尝试像这样访问列表视图事件

      lv.setOnItemClickListener(new ListView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int pos,
                            long id) {

     // put your logic here 
     //pos is the postion of selected item in list view

      //you can call 

     lv.getItemAtPosition(pos).toString();

    }
  });

试试这个链接

于 2013-08-22T08:39:02.883 回答