我如何从一个微调适配器的位置? [英] How do I get the position from a spinner adapter?

查看:91
本文介绍了我如何从一个微调适配器的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我灌了微调与猫类类型的一个通用的ArrayList

现在,我想从它那里得到一个具体项目的位置;所以,我想下面的code,但它总是返回秒。

 如果(Singleton.getCategory()!= NULL){
    猫项=新猫();
    串猫= Singleton.getCategory()的toString()。
    INT CATID = Singleton.getCategoryid();    item.setName(猫);
    item.setcatId(CATID);    INT spinnerPosition = selcategaryadapter.getPosition(项目);    //根据值设置默认
    selCategary.setSelection(spinnerPosition);
}

下面是我如何填写微调:

  JSONArray jsonarray = JSONFunction.getJSONCategary();
JSONObject的json1 = NULL;ArrayList的< eexit> listCategory =新的ArrayList< eexit>();尝试{
    的for(int i = 0; I< jsonarray.length();我++){
        json1 = jsonarray.getJSONObject(ⅰ);
        arrayCategary [I] = json1.getString(姓名)        猫项=新猫();
        item.setName(json1.getString(姓名));
        item.setcatId(的Integer.parseInt(json1.getString(类别ID)));
        listCategory.add(项目);    }}赶上(例外五){
    // TODO:处理异常
}ArrayAdapter< eexit> selcategaryadapter =新ArrayAdapter< eexit>(Activity.this,R.layout.spinner_layout,listCategory);
selcategaryadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selCategary.setAdapter(selcategaryadapter);
selCategary.setPrompt(选择类别);


解决方案

使用 setOnItemSelectedListener 像这样

  amountSpinner.setOnItemSelectedListener(新OnItemSelectedListener(){        @覆盖
        公共无效onItemSelected(适配器视图<>为arg0,观景,
                INT位置,长ARG3){            猫项=(车)view.getTag();        }

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

解决方案

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



        }

这篇关于我如何从一个微调适配器的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆