method “onItemSelected” doesn't work in array adapter and also in fragment

折月煮酒 提交于 2019-12-02 14:26:14

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

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!