我的spinner元素的自定义适配器有问题。 Spinner显示来自我的适配器的列表,但是当我尝试选择项目时它不起作用。
这是我的适配器代码:
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;
}
}
我做错了什么?
答案 0 :(得分:0)
如果您希望在微调器出现在屏幕上时预先选择任何项目,则可以使用
spinner.setSelection(position);
如果您想获得任何项目的位置,请使用下面的代码,
position = arrayList.indexOf("your item");
手段,
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) {
}
});