当打开片段时,即调用onCreateView
方法,即使尚未选择任何项目,也会调用spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener())
。
为什么这样以及如何更改代码以使其仅在选择项目时调用该方法?
这是代码:
package com.petroc.nationaldiploma;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.actionbarsherlock.app.SherlockFragment;
public class AddGradesFragment extends SherlockFragment {
private GradesDataSource datasource;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Spinner spinner;
View view = inflater.inflate(R.layout.fragment_add_grades, container,
false);
spinner = (Spinner) view.findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("Pass");
list.add("Merit");
list.add("Distinction");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner = (Spinner) view.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// EditText editText = (EditText)
// view.findViewById(R.id.editModuleText1);
return view;
}
}
感谢任何帮助
答案 0 :(得分:0)
当您使用列表对象和适配器将数据设置到微调器时,它会在设置时选择默认值为0,这就是您选择的原因