How can I get a position from a counter adapter?

I populate the counter with a generic type of type ArrayList of category cat.

Now I want to get the position of a certain element from it; therefore, 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); } 

This is how I fill the counter:

 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"); 
+4
source share
2 answers

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(); } 
+3
source

I'm not sure where you deployed your OnItemSelectedListener() four of your Spinner. If you implement it, you can have a direct position.

 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 } }); 
+2
source

All Articles