2012-11-16 54 views
0

如果我有2個微調依賴型的ArrayList>微調和onItemSelected

spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { 
        @Override 
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { 
         ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>(); 
         for (HashMap<String, String> map2 : arrList) { 
          String value = map2.get("SectionID"); 
          // Do something 

          Context context = getApplicationContext(); 

          int duration = Toast.LENGTH_SHORT; 

          Toast toast = Toast.makeText(context, value, duration); 
          toast.show(); 
         Log.d("wwwwwwwwwwwwwwwwwwww: ", value); 
         // Do something 
        } 
        } 
        @Override 
        public void onNothingSelected(AdapterView<?> adapter) { 

        } 
       }); 

我非常喜歡這一點,但kothing happend和logcat的沒有錯誤

+0

難道你不能獲得物品ID嗎? –

+0

使用id來區分。 –

+0

我不明白你可以解釋...我的意思是我會得到的位置,然後課程編號 –

回答

0

我不知道如何獲得該項目在用戶單擊微調器中的項目後,在此數組中添加標籤(courseid)。

如果您只想從每行獲取courseid,則不需要自定義的OnItemSelectedListener。簡單使用:

// Let's use the regular listener  vvvvvvvvvvvvvvvvvvvvvv 
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { 
     HashMap<String, String> map = arrList.get(position); 
     String id = map.get("courseid"); 
     // Do something 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> adapter) {} 
}); 

其中arrList是保存SimpleAdapter數據的類變量。


我非常喜歡這一點,但kothing happend和logcat的沒有錯誤

那是因爲你剛剛創建arrList,所以它是

ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>(); 
for (HashMap<String, String> map2 : arrList) { // This arrList has no data! 

你需要使用創建SimpleAdapter時使用的ArrayList。

new SimpleAdapter(this, arrList, ...); 

這是ArrayList的你必須使用,它必須類變量。

+0

和我在類CustomOnItemSelectedListene中寫什麼? –

+0

您並未使用添加任何特殊功能,因此您不需要使用CustomOnItemSelectedListener。您可以使用常規的OnItemSelectedListener。 – Sam

+0

我更新了我的文章,你可以看看它 –

0

你的文章有點難以理解,但我想我有一個想法。你有一個單一的微調器,由一個數組列表組成,每個索引對應於微調器上的每個「梯級」。所以當選擇這個項目時,你想獲得被選擇的單個鍵並且用它做一些事情。那麼我們知道,hashMap沒有真正的索引,所以我們必須使用其他方法來解決它,對嗎?

 ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>(); 

     spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, 
        int position, long id) { 

       // for each key in the hashMap at this position.. 
       for (String key : arrList.get(position).keySet()) { 
        if (key == "Calculus") { 
//      do something 
        } else if (key == "Physics") { 
//      do something else 
        } 
       }          
      } 

這個想法很簡單。你可以將整個hashMap的keySet放在微調器上選定位置的索引處,並根據它所說的內容爲每個key做一些事情。這應該沒有太多的麻煩,假設你只有一個鍵值對。

但是我不得不說,你應該真的重新考慮一下設計。你有多個hashMap容器​​,每個只容納一件事。這實在是浪費資源。很難推薦替代品,因爲我不完全知道如何使用這些數據,但是您應該知道在java中製作對象並不是免費的。

+0

我照你說的做,但我改變它爲(字符串鍵:arrList.get(位置).get(「courseid」)),並且只有一個數組或一個實例java.lang.iterable –

+0

如果你保持原樣 - 也許將「微積分」更改爲「courseid」,它將起作用。你的問題是你試圖獲得一個鍵(在這種情況下是字符串),這是不可迭代的。代碼的作用是獲得*所有*鍵(不管多少)並遍歷它們以獲得特定的鍵。如果關鍵的「courseid」只在arrayList的一個索引中,那麼按照你的方式做並不是最好的,除非你特別想知道它是否在那裏。 – mango

相關問題