How to create Spinner-list using CustomAdapter in android
Inside your adapter constructor, send the id of textview also
public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
flater = context.getLayoutInflater();
}
call it by
CustomAdapter adapter = new CustomAdapter(MainActivity.this,
R.layout.listitems_layout, R.id.title, rowItems);
EDIT Your images are not showing because you didn't override the getDropdownView() method. This method decides the layout of child when dropdown is visible. So add this method to your adapter
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = flater.inflate(R.layout.list_itemslayout,parent, false);
}
RowItem rowItem = getItem(position);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem.getTitle());
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
imageView.setImageResource(rowItem.getImageId());
return convertView;
}
Suggestion in your getView() put a check for (convertview == null). It might not have any impact on this small adapter but in case your adapter is having more items, it will impact the performance.
EDIT To put your spinner dropdown below the anchor, use
android:overlapAnchor="false"
inside your spinner
Change your adapter like this
public class CustomAdapter extends ArrayAdapter<RowItem> {
LayoutInflater flater;
public CustomAdapter(Activity context, int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
// flater = context.getLayoutInflater();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return rowview(convertView,position);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return rowview(convertView,position);
}
private View rowview(View convertView , int position){
RowItem rowItem = getItem(position);
viewHolder holder ;
View rowview = convertView;
if (rowview==null) {
holder = new viewHolder();
flater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowview = flater.inflate(R.layout.listitems_layout, null, false);
holder.txtTitle = (TextView) rowview.findViewById(R.id.title);
holder.imageView = (ImageView) rowview.findViewById(R.id.icon);
rowview.setTag(holder);
}else{
holder = (viewHolder) rowview.getTag();
}
holder.imageView.setImageResource(rowItem.getImageId());
holder.txtTitle.setText(rowItem.getTitle());
return rowview;
}
private class viewHolder{
TextView txtTitle;
ImageView imageView;
}
}