Reputation: 131
I am filling a spinner with a generic ArrayList of cat Category type.
Now, I want to get the position of a specific item from it; so, I tried the code below but it always returns S.
if (Singleton.getCategory() != null) {
cat item =new cat();
String cat= Singleton.getCategory().toString();
int catid= Singleton.getCategoryid();
item.setName(cat);
item.setcatId(catid);
int spinnerPosition=selcategaryadapter.getPosition(item);
//set the default according to value
selCategary.setSelection(spinnerPosition);
}
Here is how I fill the spinner:
JSONArray jsonarray=JSONFunction.getJSONCategary();
JSONObject json1=null;
ArrayList<eexit> listCategory= new ArrayList<eexit>();
try {
for(int i=0;i< jsonarray.length();i++) {
json1=jsonarray.getJSONObject(i);
arrayCategary[i]=json1.getString("Name")
cat item=new cat();
item.setName(json1.getString("Name"));
item.setcatId(Integer.parseInt(json1.getString("CategoryID")));
listCategory.add(item);
}
} catch (Exception e) {
// TODO: handle exception
}
ArrayAdapter<eexit> selcategaryadapter = new ArrayAdapter<eexit>(Activity.this,R.layout.spinner_layout, listCategory);
selcategaryadapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
selCategary.setAdapter(selcategaryadapter);
selCategary.setPrompt("Select Category");
Upvotes: 4
Views: 2902
Reputation: 4008
Use setOnItemSelectedListener like this
amountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
cat item =(car) view.getTag();
}
Upvotes: 3
Reputation: 128428
I am not sure where you have implemented OnItemSelectedListener()
four your Spinner. If you implement it then you can have position straight way.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Upvotes: 2