3

我在标签片段中有微调器,我的代码中需要 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;
    }   
4

1 回答 1

1

删除您的OnItemClickListener并保留OnItemSelectedListener.

Spinners 使用OnItemSelectedListener并且设置两者很可能会混淆一切。

http://developer.android.com/guide/topics/ui/controls/spinner.html

于 2013-07-18T06:55:23.530 回答