微调器 setOnItemSelectedListener 的问题
Problem with Spinner setOnItemSelectedListener
当我的片段在 onCreateView 微调器 OnItemSelectedListener 中时 运行!但是选择了 none 个选项
片段:
public class AppDataUsageFragment extends Fragment{
Spinner spinner;
String[] date_array = new String[]{"Day","Week","Month","Year","Custom"};
public AppDataUsageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_data_usage_fragment, container, false);
spinner = view.findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line,date_array));
date_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
GetData(); // ********* THIS RUN WHEN NOTHING SELECT BY USER ***********
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
避免在初始化期间调用spinner.setOnItemSelectedListener()
spinner.setSelection(Adapter.NO_SELECTION, true); //Add this line before setting listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
当我的片段在 onCreateView 微调器 OnItemSelectedListener 中时 运行!但是选择了 none 个选项 片段:
public class AppDataUsageFragment extends Fragment{
Spinner spinner;
String[] date_array = new String[]{"Day","Week","Month","Year","Custom"};
public AppDataUsageFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_data_usage_fragment, container, false);
spinner = view.findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line,date_array));
date_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
GetData(); // ********* THIS RUN WHEN NOTHING SELECT BY USER ***********
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
避免在初始化期间调用spinner.setOnItemSelectedListener()
spinner.setSelection(Adapter.NO_SELECTION, true); //Add this line before setting listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});