Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
我有两个类:fragmentMyListAdapter(数组适配器)。

我必须在哪个类中使用方法onItemSelected

MyListAdapter中不起作用.....

这是我的代码:

阵列适配器:

public  class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener  {
    Spinner spinner;

    /*public MyListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }*/

    public MyListAdapter(Context context) {
        super(context, R.layout.single_listview_item);
    }





        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            System.out.println("ciao");

            spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    getContext(),
                    R.array.country_arrays,
                    android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(this);

/*
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
*/
           /*     @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    //Spinner spinner = null;
                   // String selState = (String) ((Spinner) parent).getSelectedItem();

                    spinner.setSelection(position);
                    String selState = (String) ( parent).getSelectedItem();
                    Toast.makeText(
                            parent.getContext(),
                            "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }


            });*/



            return row;
        }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        spinner.setSelection(position);
        String selState = spinner.getSelectedItem().toString();
        System.out.println(selState);
        Toast.makeText(
                getContext(),
                "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
       /* Toast.makeText(parent.getContext(),
                "OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
                Toast.LENGTH_SHORT).show();*/
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


   /* @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String selState = (String) spinner.getSelectedItem();
        System.out.println(selState);
        Toast.makeText(
                getContext(),
                "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }*/

    }


分段:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment
        //super.onDestroy();

        SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
        listView = (ListView) rootView.findViewById(R.id.listview);
        ListAdapter listAdapter = new MyListAdapter(getContext());
        listView.setAdapter(listAdapter);


java - 使用方法onItemSelected的地方-LMLPHP

最佳答案

将以下代码用于适配器文件。

public  class MyListAdapter extends ArrayAdapter   {
Spinner spinner;

/*public MyListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}*/

public MyListAdapter(Context context) {
    super(context, R.layout.single_listview_item);
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        System.out.println("ciao");

        spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getContext(),
                R.array.country_arrays,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

           @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //Spinner spinner = null;
                String selState = spinner.getSelectedItem().toString();

           //     spinner.setSelection(position);
             //   String selState = (String) ( parent).getSelectedItem();
                Toast.makeText(
                        parent.getContext(),
                        "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }


        });



        return row;
    }

}

07-26 09:42