Reputation: 2565
Check the below pic. I guess arg2 of the overrided method is supposed to be the position of the item that I've currently selected, right? But, it's always being 0. I want the correct position to do stuffs. Any suggestion?
Here's my adapter class.
public class PojoSpinnerAdapter extends ArrayAdapter<Pojo> {
private Activity activty;
private ArrayList<Pojo> pojoList;
LayoutInflater inflater;
public PojoSpinnerAdapter(Activity activity, int resource,
ArrayList<Pojo> pojoList) {
super(activity, resource, pojoList);
this.activty = activity;
this.pojoList = pojoList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.taxo_spinner_item, parent, false);
Pojo pojo = pojoList.get(position);
//.... blah blah
return row;
} }
Upvotes: 1
Views: 2912
Reputation: 907
You can also use your getCustomView(.,.,) for this purpose
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.taxo_spinner_item, parent, false);
Pojo pojo = pojoList.get(position);
//.... blah blah
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context," position :"+position,short).show
}
});
return row;
}
Upvotes: 1
Reputation: 8562
I suggest not to create methods like this and assign OnItemSelectedListener()
. Simply do this either implementing this in class like here
public class YourClass implements AdapterView.OnItemSelectedListener {}
and assign this
to Spinner
,
OR
Try doing it anonymously like here
mySpinner = (Spinner) findViewById(R.id.mySpinner);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int selectedPosition = arg2; //Here is your selected position
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Upvotes: 2