Android 如何从微调器适配器获取位置?

Android 如何从微调器适配器获取位置?,android,Android,我正在用cat类别类型的通用ArrayList填充微调器 现在,我想从中得到一个特定项目的位置;因此,我尝试了下面的代码,但它总是返回S if (Singleton.getCategory() != null) { cat item =new cat(); String cat= Singleton.getCategory().toString(); int catid= Singleton.getCategoryid(); item.setName(cat)

我正在用cat类别类型的通用ArrayList填充微调器

现在,我想从中得到一个特定项目的位置;因此,我尝试了下面的代码,但它总是返回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);
}
以下是我如何填充微调器:

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");
JSONArray JSONArray=JSONFunction.getjsoncategry();
JSONObject json1=null;
ArrayList listCategory=新的ArrayList();
试一试{
for(int i=0;i
我不确定您在哪里实现了
OnItemSelectedListener()
四个微调器。若你们实施了它,那个么你们可以有一个直线的位置

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
    }

});
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
//你的代码在这里
}
@凌驾
未选择公共无效(AdapterView父视图){
//你的代码在这里
}
});

像这样使用setOnItemSelectedListener

amountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
                int position, long arg3) {

            cat item =(car) view.getTag();



        }
amountSpinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0,视图,
整数位置,长arg3){
cat item=(car)view.getTag();
}

可能对您有所帮助我想根据Id从适配器获取位置类别名称,我想根据Id从适配器获取位置类别名称方法“onItemSelected()”中的自身您正在获取项目和位置,不是吗?我正在获取所选项目的位置,但这是一些场景。我必须将类别类型的obj says category=“Restreent”和id=1与SelCategory adapter匹配,才能获取id为1的Restreent位置,这是逻辑问题。重复适配器计数,如(inti=0;i