2016-03-28 22 views
1

Aşağıdaki resme bakın. Geçersiz kılınmış yöntemin arg2'sinin, şu anda seçmiş olduğum öğenin konumu olması gerekiyor sanırım, değil mi? Ancak, her zaman 0'dır. Malzeme yapabilmek için doğru pozisyonu istiyorum. Herhangi bir öneri?Özelleştirilmiş bir eğiricinin seçili Öğe konumu onItemSelectedListener'dan nasıl alınır?

enter image description here

İşte benim adaptör sınıfı.

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; 
} } 
+0

onItemSelected() yönteminin çağrıldığından emin misiniz? –

+0

cuz, resimde görebildiğim, hata ayıkladığım zamandı. –

+0

Ve bunu yaparsan, iyi çalışıyor mu? –

cevap

2

Bu gibi yöntemler oluşturmamayı ve OnItemSelectedListener()'u atamamayı öneririm. Basitçe zaten bu da

VEYA

buradaki gibi sınıfta

public class YourClass implements AdapterView.OnItemSelectedListener {} 

bu uygulama ve Spinner için this atarım Ayrıca can

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 

     }  

    }); 
1

buradaki gibi anonim yapıyor deneyin bu amaç için getCustomView (.,.,) kullanın

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; 
} 
İlgili konular