0
當片段被打開,即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的數據到微調