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
}
});
In your method of setting
OnItemSelectedListener(this)
within theArrayAdapter
, I believe the issue is that, because every spinner in the list references the same instance ofMyListAdapter
, yourOnItemSelected
method is not distinguishing which spinner within your list was actually selected from-- it just references the spinner object that was last set to theMyListAdapter.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 havelistView.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:
And this in your fragment (no listeners):
On ListViews you use onItemClicked. OnItemSelected is for Spinners. It's one of those Android nuisances...