gpt4 book ai didi

android - 如何从 onItemSelectedListener 获取自定义微调器的选定项目位置?

转载 作者:行者123 更新时间:2023-11-29 17:21:57 25 4
gpt4 key购买 nike

检查下图。我想覆盖方法的 arg2 应该是我当前选择的项目的位置,对吧?但是,它始终为 0。我想要正确的位置来做事。有什么建议吗?

enter image description here

这是我的适配器类。

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

最佳答案

我建议不要创建这样的方法并分配 OnItemSelectedListener()。只需像这里这样在类中实现它就可以做到这一点

public class YourClass implements AdapterView.OnItemSelectedListener {}

并将this分配给Spinner

像这里一样匿名尝试

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

}

});

关于android - 如何从 onItemSelectedListener 获取自定义微调器的选定项目位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com