2012-04-03 79 views
0

我遇到了一個奇怪的問題。我的OnItemSelectedListener似乎只能用一次,我的意思是它在第一次顯示我的測試Toast時單擊相應的項目,但它不顯示測試Toast,當我在第二次打相同的項目。當第二次點擊不同的項目時)有什麼問題? plz幫助我微調控件OnItemSelectedListener雙選問題

部分代碼是在這裏

 //get task object from menu 
    taskListArr = new ArrayList<Task>(); 

    taskListArr = getCurrentTasks(taskListArr); 

    myTask=new TaskListAdapter(this, 0, taskListArr); 
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, TaskModel.sorts); 
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    sortSpinner.setAdapter(aa); 
    sortSpinner.setOnItemSelectedListener(this); 



    @SuppressWarnings("unchecked") 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
    if(arg2 == 0){ 
     Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show(); 
     Collections.sort(taskListArr); 
     taskListView.setAdapter(myTask); 
    } 
    if(arg2 == 1){ 
     Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show(); 
     Collections.sort(taskListArr, new DateComparator()); 
     taskListView.setAdapter(myTask); 
    } 
    if(arg2 == 2){ 
     Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show(); 
     Collections.sort(taskListArr, new PriorityComparator()); 
     taskListView.setAdapter(myTask); 
    } 
    position = arg2; 
} 

public void onNothingSelected(AdapterView<?> arg0) { 

} 
+0

多數民衆贊成在如何工作..! – ngesh 2012-04-03 05:38:28

回答

0

退房飛旋在Android開發者網站http://developer.android.com/reference/android/widget/Spinner.html

A view that displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view. 

它一次選擇一個孩子。所以再次選擇已經選擇的孩子將不會調用onItemSelected函數。

+0

感謝您的解釋,但我仍然需要弄清楚如何多次調用 – 2012-04-03 06:21:39

0

我同意user936414回答他是對的,但如果你仍然想的是你的麪包再來然後添加一個觸摸監聽你的微調,並在ontouch事件由這每一次的添加此

line sortSpinner.setOnItemSelectedListener(this); 

你觸摸你的微調聽衆將再次被調用,你會得到每一次選擇的項目

+0

Thanx,我會嘗試它,但它似乎不工作。也許我需要創建一個自定義的adpter來處理這個問題。 – 2012-04-03 06:29:44

+0

雨zwr你試過這個解決方案嗎? – vipin 2012-04-04 05:44:08

+0

Sry,我試過但失敗了。我使用Menu listener來實現這個問題。謝謝 – 2012-04-13 05:14:49