4

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");
APerson
  • 8,140
  • 8
  • 35
  • 49
sara
  • 131
  • 4
  • 14
  • 1
    [This](http://stackoverflow.com/questions/2390102/how-to-set-selected-item-of-spinner-by-value-not-by-position) might help you – Manoj Kumar Aug 29 '12 at 12:22

2 Answers2

3

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();



        }
NullPointerException
  • 3,978
  • 4
  • 34
  • 52
  • i want to get position from adapter based on Id an category name – sara Aug 29 '12 at 12:29
  • Here itself in method "onItemSelected()" you are getting the item and position isnt it?? – NullPointerException Aug 29 '12 at 12:36
  • i am getting that position on selected item but is some Scenerio i have to match obj of category type says Category = "resturent" and id =1 with SelCategory adapter to get Position of Resturent with id 1 – sara Aug 29 '12 at 12:39
  • 1
    So its the matter of logic.. Iterate over the adapter count like for (int i = 0; i< adapter.getCount() ; i++) { cat item = adapter.getItem(i); if(item.getId == 1){ spinner.selection(i); } } – NullPointerException Aug 29 '12 at 13:00
2

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
    }

});
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295