我在tab片段中有微调器,我的代码中需要OnItemSelectedListener。我正在寻找一些例子,但它们主要是在MainActivitys中。所以,我无法弄清楚如何在片段中使用它。请帮忙。
public class MyFragment extends Fragment{
private Button loadButton;
private Spinner turnPointsSpinner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_task, container, false);
loadButton = (Button) myFragmentView.findViewById(R.id.button1);
turnPointsSpinner = (Spinner)myFragmentView.findViewById(R.id.spinner1);
ArrayAdapter<TurnPoints> myAdapter = new ArrayAdapter<TurnPoints>(this.getActivity(), android.R.layout.simple_spinner_item, SingletonTurnPoints.getInstance().mTurnPoints);
myAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
turnPointsSpinner.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
OnItemSelectedListener countrySelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position, long id) {
Log.d("tag", "selected");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
// Setting ItemClick Handler for Spinner Widget
turnPointsSpinner.setOnItemSelectedListener(countrySelectedListener);
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//when button Load is clicked, we start new activity for file browsing
Intent intent = new Intent(getActivity(), BrowseFileActivity.class);
//FragmentTask.this.startActivity(intent);
startActivity(intent);
Log.d("tag", String.valueOf(intent));
}
});
return myFragmentView;
}
答案 0 :(得分:1)
删除OnItemClickListener
,然后保留OnItemSelectedListener
。
Spinners使用OnItemSelectedListener
并且设置两者的可能性很可能会使所有内容混淆。
http://developer.android.com/guide/topics/ui/controls/spinner.html