【问题标题】:How do I get the position from a spinner adapter?如何从微调器适配器获取位置?
【发布时间】:2012-08-24 01:08:04
【问题描述】:

我正在用 cat Category 类型的通用 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");

【问题讨论】:

  • This 可能会帮助你

标签: android


【解决方案1】:

我不确定你在哪里实现了OnItemSelectedListener() 四个你的 Spinner。如果您实施它,那么您可以直接获得位置。

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
    }

});

【讨论】:

  • 我想根据 Id an category name 从适配器获取位置,
【解决方案2】:

像这样使用 setOnItemSelectedListener

amountSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

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



        }

【讨论】:

  • 我想根据 Id 和类别名称从适配器获取位置
  • 在方法“onItemSelected()”中,您正在获取项目和位置不是吗??
  • 我在所选项目上获得了该位置,但是我必须匹配类别类型的 obj 表示 Category = "resturent" 和 id =1 与 SelCategory 适配器以获得具有 id 1 的 Resturent 位置
  • 所以它的逻辑问题.. 迭代适配器计数如 for (int i = 0; i
猜你喜欢
  • 2016-01-22
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
相关资源
最近更新 更多