Reputation: 1012
I try to create list dialog for spinner but I have error "ArrayAdapter requires the resource ID to be a TextView" I created my custum adapter for it
public class OrderTypeAdapter extends ArrayAdapter<String> {
private final Context context;
private final List<String> values;
private class ResourseHolder{
protected TextView text;
}
public OrderTypeAdapter(Context context, List<String> values) {
super(context, R.layout.item_list_dialog,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.item_list_dialog,parent,false);
ResourseHolder holder = new ResourseHolder();
holder.text = (TextView) rowView.findViewById(R.id.ild_main_text_view);
rowView.setTag(holder);
} else {
rowView = convertView;
}
ResourseHolder holder = (ResourseHolder) rowView.getTag();
holder.text.setText(values.get(position));
return rowView;
}
}
xml code of item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/ild_main_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I set Addapter to spinner by standart way
OrderTypeAdapter adapter =new OrderTypeAdapter(getApplicationContext(),
Convert.toList(searchParam.getOrderTypes()));
orderTypeSpinner.setAdapter(adapter);
When I click at spinner for show dialog my application crashed, but when I try with ArrayAdapter all was ok. What is the problem here ?
Upvotes: 0
Views: 572
Reputation: 1343
you didn't defined getDropDownView in your customized Arrayadapter.spinners need a getdropdown method in array adapter. my recommendation is define a getCustomView method in Arrayadapter. a simple code for example must be like it :
public class Adapter1 extends ArrayAdapter<String> {
private final Activity context;
private final String[] string1;
private final String[] stringpic;
public Adapter1(Activity context, String[] name, String[] pic) {
super(context, R.layout.row, name);
this.context = context;
this.string1 = name;
this.stringpic = mpic;
}
@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 view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView txtname = (TextView) rowView.findViewById(R.id.textView);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
// write your statements in here for example
if (string1[position] != null) {
txtname.setText(string1[position]);
}
return rowView;
}
}
Upvotes: 0
Reputation: 619
in your else condition do
holder = (ResourseHolder) rowview.getTag()
and initialize rowview with convert view at start
Upvotes: 0
Reputation: 157457
you are looking for
rowView.findViewById(R.id.ild_main_text_view);
but inside your layout you have
@+id/item_text_view
So you should change it accordingly:
rowView.findViewById(R.id.item_text_view);
Upvotes: 1