如何在listview arrayadapter上填充某个位置<string>?</string>

时间:2012-11-29 06:44:04

标签: android listview toast

     protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                matches));


    }



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

我正在寻找正确的语法来获取这个填充列表的第一个位置到一个字符串,所以我可以把它放在一个吐司或保存到一个文件,我是新的,如果我可以在吐司上显示它我可以照顾好休息,谢谢

Toast.makeText(this,(CharSequence) wordsList.getItemAtPosition(0) , Toast.LENGTH_LONG).show();

2 个答案:

答案 0 :(得分:3)

非常简单:

String firstResult = matches.get(0);
Toast.makeText(getContext(), firstResult,
Toast.LENGTH_SHORT).show();

答案 1 :(得分:1)

wordList.setOnItemClickListener(new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos, long l) {
            try {
               Toast.makeText(this,"Position is===>>"+pos , Toast.LENGTH_LONG).show();
            }
            catch(Exception e) {
                System.out.println("Nay, cannot get the selected index");
            }
        }
    });