从微调器自动选择的项目?

时间:2014-05-19 04:58:14

标签: java android android-spinner

我的问题是,每当我在下一个活动中向数组列表添加讲座时,它都会回调此活动并对其进行更新。但是,出于某种原因,只要我回到调用活动就立即调用onItemSelected,并且一旦我回到调用活动,我就立即被发送到Lecture活动(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) {}
    });

}

1 个答案:

答案 0 :(得分:0)

请添加&#34;选择&#34;在顶部的讲话管理.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();**
    }
    }