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) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.