2011-11-03 61 views
1

我遇到問題,需要爲微調器選取值。這裏是我的代碼和適配器。Android微調器獲取選定的值OnSelectedItemListener();

微調事件偵聽器

category = (Spinner) findViewById(R.id.Spinner); 
category.setPrompt("Select Category.."); 
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();          
HashMap<String, Object> map = new HashMap<String, Object>(); 
map.put("Category", "Choose a Category"); 
list.add(map); 

map = new HashMap<String, Object>(); 
map.put("Icon", R.drawable.icon1); 
map.put("Category", "Category1"); 
list.add(map); 

map = new HashMap<String, Object>(); 
map.put("Icon", R.drawable.icon2); 
map.put("Category", "Category2");                  
list.add(map); 

map = new HashMap<String,Object>(); 
map.put("Icon", R.drawable.icon3); 
map.put("Category", "Category3");                    
list.add(map); 
CategoryAdapter adapter = new CategoryAdapter(v.getContext(), list,R.layout.list_layout, new String[] { "Category", "Icon" }, new int[] { R.id.category, R.id.icon }); 
category.setAdapter(adapter); 

category.setOnItemSelectedListener(new OnItemSelectedListener(){ 

@Override 
public void onItemSelected(AdapterView<?> arg0,View v, int arg2,long arg3) { 
    // TODO Auto-generated method stub 
    String selected = category.getSelectedItem().toString(); 
    //String selected = category.getTag().toString(); 
    try{ 
     //String selected = category.getSelectedItem().toString(); 
     //Object selected = arg0.getTag(); 
     //Toast.makeText(getApplicationContext(), String.valueOf(selected), Toast.LENGTH_SHORT).show(); 
     if(selected.toString().equals("Category1")){ 
      String msg1 = "Category1 type selected"; 
      Toast.makeText(getApplicationContext(), msg1 + " " + selected, Toast.LENGTH_SHORT).show(); 
     }else if(selected.toString().equals("Category2")){ 
      String msg2 = "Category2 type selected"; 
      Toast.makeText(getApplicationContext(), msg2 + " " + selected, Toast.LENGTH_SHORT).show(); 
     }else if(selected.toString().equals("Category3")){ 
      String msg3 = "Category3 type selected"; 
      Toast.makeText(getApplicationContext(), msg3 + " " + selected, Toast.LENGTH_SHORT).show(); 
     } 
    }catch(Exception e){ 
     Toast.makeText(getApplicationContext(),"Error occured " + e.getName().getClass(), Toast.LENGTH_SHORT).show(); 
    }                        
} 

@Override 
public void onNothingSelected(
     AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), "nothing selected", Toast.LENGTH_SHORT).show(); 
} 

}); 

CategoryAdapter

public class ReminderCategoryAdapter extends SimpleAdapter{ 

    Context c; 

    public ReminderCategoryAdapter(Context context, List<? extends Map<String, ?>> data, 
      int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
     c = context; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = li.inflate(R.layout.list_layout,null); 
     } 

     HashMap<String, Object> data = (HashMap<String, Object>) getItem(position); 

     ((TextView) convertView.findViewById(R.id.category)).setText((String) data.get("Category")); 
     ((TextView) convertView.findViewById(R.id.category)).setTag((Object) data.get("Category")); 

     return convertView; 
    } 
} 

我的微調,在適配器,我吹圖片+文字,我的微調工作發現,特此我說明我的微調

============================

| Icon1 Text1 |

| Icon2 Text2 |

| Icon3 Text3 |

但是,一旦我嘗試使用onitemselectedlistener並使用getSelectedItem()。toString()方法並對其進行烘烤,則敬酒時的值返回值就是圖標和文本的組合,我不希望圖標的值將會返回,但我只想要字符串或文本。我嘗試使用setTag(Object value)方法使用((TextView)convertView.findViewById(R.id.category))。setTag((Object)data.get(「Category」));在適配器中,並且在偵聽器中使用getTag()方法時,拋出nullpointer異常。除了使用getItemPosition(position)或getSelectedItem()方法之外,還有其他解決方案來獲取和操作所選項目的字符串值嗎? 請幫忙。

回答

2

您可以輕鬆使用的一種解決方案是使用Spinner#getSelectedItemPosition()方法。此方法將返回選定項目的位置,並且您可以從傳遞給適配器的列表中檢索項目本身。

所以基本上,替換下面的行中的onItemSelected方法:

String selected = category.getSelectedItem().toString(); 

有:

String selected = list.get(category.getSelectedItemItemPosition()).get("Category"); 
+1

只是爲了解釋這個有點多,'category.getSelectedItem()'返回一個Map實例。這就是爲什麼'toString()'不起作用。如果你做'category.getSelectedItem()。get(「Category」)'',你將在地圖中獲得該鍵的值。 – dmon

+0

謝謝,我的問題解決了。 – Zahary