method “onItemSelected” doesn't work in array

2019-03-07 01:01发布

The method onItemSelected doesn't work and I don't know why.

Below is my code.

Array adapter:

public  class MyListAdapter extends ArrayAdapter {
    Spinner spinner;
    ListView listView;

    /*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;

         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);
        System.out.println("ciao1");

      //  spinner.setOnItemSelectedListener(this);

        return row;
    }



   /* @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) {

    }*/

    }

fragment:

 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);
//      listView.setOnItemClickListener(this);
        listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long i)
            {
                listView.setSelection(position);
                String selState = (String) listView.getSelectedItem();
                Toast.makeText(
                        getContext(),
                        "Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

2条回答
相关推荐>>
2楼-- · 2019-03-07 01:30

In your method of setting OnItemSelectedListener(this) within the ArrayAdapter, I believe the issue is that, because every spinner in the list references the same instance of MyListAdapter, your OnItemSelected method is not distinguishing which spinner within your list was actually selected from-- it just references the spinner object that was last set to the MyListAdapter.spinner reference, which should be the last one in the list.

To remedy this, don't use spinner.getSelectedItem(). Instead use ((Spinner) parent).getSelectedItem(), since the parent object will be the actual spinner selected from. And at that point, you should make the spinner variable local to the getView method.

In the method of calling listView.setOnItemSelectedListener(...) in the fragment, I expect you meant to have listView.setOnItemClickListener(...), but that will listen for a click on a row in your list, not a selection from a spinner.

To detect a selection from one of your spinners, make the first modification above. To detect a click on a row in your listView as well, change to listView.setOnItemClickListener. Without more context for your objective or the actual errors occurring, it's hard to tell if both or just the first is desired.

EDIT: From your comments, I think you should have this in your MyListAdapter:

public  class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    Spinner spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            parent.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);

    return row;
}

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

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

}

}

And this in your fragment (no listeners):

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);
查看更多
Root(大扎)
3楼-- · 2019-03-07 01:40

On ListViews you use onItemClicked. OnItemSelected is for Spinners. It's one of those Android nuisances...

查看更多
登录 后发表回答