2012-08-08 249 views
1

我只需要顯示Arraylist中的第一個條目。如何從arraylist中獲取第一個數據(在我的代碼中匹配)

我下面的代碼顯示了所有的結果,但我只需要列表中的第一個條目。

wordsListListView名稱:

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

wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches)); 

全部方法代碼:

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.get(0))); 
    } 
} 
+1

「第一數據」是什麼意思?你是指「ArrayList」的第一個元素嗎? – minhaz1 2012-08-08 19:39:52

+0

根據user1543340在第一條語句中提出的問題,這是一個問題。但是,user1543340提到了'ListView',這讓我稍微感到厭倦。 – prolink007 2012-08-08 19:41:25

+0

錯誤是「構造函數ArrayAdapter (VoiceRecognitionDemo,int,String)未定義」 – user1543340 2012-08-08 19:52:24

回答

0

由於您只從ArrayList<String> matches想要的第一個元素,那麼你只需要創建一個新的List<String>,並將第一個元素添加到新的List

然後使用新的List作爲Adapter

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

     if (matches.size() > 0) { 
      List<String> oneMatch = new ArrayList<String>(); 
      oneMatch.add(matches.get(0)); 

      wordsList.setAdapter(
       new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1, oneMatch)); 
     } else { 
      Log.e(TAG, "Matches does not contain any values."); 
     } 
    } 
} 
+0

我嘗試,但它給錯誤。在這裏完整的olf類 – user1543340 2012-08-08 19:43:41

+0

你可以在你原來的文章中發佈錯誤?另外,你能否澄清你想要的?你的問題很難理解。閱讀已發佈的評論。 – prolink007 2012-08-08 19:45:19

+0

我嘗試,但它給error.here全OLF類 「保護無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據) { 如果(requestCode == REQUEST_CODE && resultCode爲== RESULT_OK) {// 填充與字符串的wordsList值識別引擎認爲聽到 ArrayList的匹配= data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); wordsList.setAdapter(new ArrayAdapter (this,android.R.layout.simple_list_item_1, matches.get(0))); }' – user1543340 2012-08-08 19:47:54

0

如果你試圖讓ArrayList的第一個元素,那麼你需要使用ArrayList類的get(int)方法。所以在你的情況下,matches.get(0)會爲你提供matchesArrayList的第一個元素。請澄清你的意思是「第一數據」,所以我可以提供最好的答案。

0

如果你只是想顯示的第一個項目,然後做這樣的事情:

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

wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches.get(0))); 

將其更改爲matches.get(0)使得它僅通過第一String對象列表中的適配器。很難告訴你在做什麼,因爲只有一個適配器的效率似乎很低。

+0

我嘗試,但它給error.here全OLF類 ' 保護無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據) { 如果(requestCode == REQUEST_CODE && resultCode爲== RESULT_OK) {// 填充與字符串的wordsList值識別引擎認爲聽到 ArrayList的匹配= data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); wordsList.setAdapter(new ArrayAdapter (this,android.R.layout.simple_list_item_1, matches.get(0))); }' – user1543340 2012-08-08 19:46:36

+0

請總結我們的錯誤或將它發佈在您的原始文章中,這樣可以更容易閱讀。閱讀「內聯」代碼非常困難。並請澄清你的問題。 – prolink007 2012-08-08 19:48:17

+0

你應該只調用'setAdapter()'一次,通常在'onCreate()'中。如果要將數據更改爲適配器,則直接通過適配器執行,然後確保調用'notifyDataSetChanged()'。 – 2012-08-08 19:48:23

相關問題