[英]Android spinner adapter selecting doesn't work with using custom BaseAdapter
I have a problem with my custom adapter for spinner element. 我的微调器元素自定义适配器有问题。 Spinner shows a list from my adapter but when I try to select item it doesnt work. 微调框显示了适配器的列表,但是当我尝试选择项目时,它不起作用。
Here is my code of adapter: 这是我的适配器代码:
public class CategoryAdapter extends BaseAdapter{
private final ArrayList<String> categories;
private Context context;
LayoutInflater inflater;
public CategoryAdapter(Context context, ArrayList catList){
this.context = context;
inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
categories = catList;
}
@Override
public int getCount() {
return categories.size();
}
@Override
public String getItem(int position) {
return categories.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_spinner_item, null);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setTextColor(Color.RED);
textView.setText(getItem(position));
return convertView;
}
}
What am I doing wrong? 我究竟做错了什么?
if you want any item preselected when spinner appears over screen, you can use 如果您希望在屏幕上出现微调框时预先选择任何项目,则可以使用
spinner.setSelection(position);
and if you want to get position of any item then use below code, 如果要获取任何物品的位置,请使用以下代码,
position = arrayList.indexOf("your item");
menual way, 菜单方式
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
spinner.setSelection(i);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.