2014-05-19 66 views
0

我的問題是,無論何時在下一個活動中向數組列表中添加講座,它都會回調此活動並進行更新。然而,出於某種原因,只要返回到調用活動,我立刻調用onItemSelected,並且一旦我沒有實際回到調用活動,我就立即發送到講座活動(displayLectureIntent立即啓動)從微調選擇任何東西。自動從微調框中選擇的項目?

難道只要我向微調器添加了一些東西,微調器會選擇默認的第一個對象,因此它會「選擇」它?由於

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_class_manager0); 

    Intent receivedIntent = getIntent(); 
    if (receivedIntent.hasExtra("lectureManagerExtra")) { 
     lectureManager = (LectureManager) getIntent().getSerializableExtra("lectureManagerExtra"); 
     update(); 
    } else { 
     lectureManager = new LectureManager(); 
    } 

} 


public void update() { 
    Spinner spinner = (Spinner) findViewById(R.id.lecturespinner); 
    ArrayAdapter<String> lectureadapter = new ArrayAdapter<String> 
     (this, android.R.layout.simple_spinner_dropdown_item, lectureManager.getLectureNames()); 
    lectureadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(lectureadapter); 


    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { 
      Toast.makeText(getApplicationContext(), "HEYYY", Toast.LENGTH_LONG).show(); 

      Intent displayLectureIntent = new Intent(getBaseContext(), LectureActivity.class); 
      displayLectureIntent.putExtra("lectureExtra", 
        lectureManager.returnLecture(adapterView.getItemAtPosition(position).toString())); 
      startActivity(displayLectureIntent); 
     } 

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

} 

回答

0

請加頂部 「中選擇」上lectureManager.getLectureNames字符串()陣列,該陣列的尺寸增加1 和的setSelectedIndex爲0。 和 修改onItemSelected方法如下

@Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) { 
     Toast.makeText(getApplicationContext(), "HEYYY", Toast.LENGTH_LONG).show(); 

     Intent displayLectureIntent = new Intent(getBaseContext(), LectureActivity.class); 
     displayLectureIntent.putExtra("lectureExtra", 
       lectureManager.returnLecture(adapterView.getItemAtPosition(position).toString())); 
     **if(position !=0){ 
     startActivity(displayLectureIntent); 
    } 
    else{ 
    Toast.makeText(getApplicationContext(), " select the lecture.", Toast.LENGTH_LONG).show();** 
    } 
    } 
+0

感謝,但你是什麼意思添加在lectureManager.getLEctures選擇字符串()? – user3155915

+0

如果您的演講清單是 {「math」,「computer」,「science」}然後在索引0 上添加「select」,如下所示{「select」,「math」,「computer」,「science」} – tizbn

相關問題