问题
in my application I must use a spinner that should show three items (IT,EN,PR). I must also use a personalized layout. The code is in an another adapter in onCreate method:
holder.spinnerLenguage = (Spinner) convertView
.findViewById(R.id.spinnerLenguage);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("IT");
spinnerArray.add("EN");
spinnerArray.add("PR");
final lenguageSpinnerAdapter lenguageAdapter = new lenguageSpinnerAdapter(
convertView.getContext(), spinnerArray);
holder.spinnerLenguage.setAdapter(lenguageAdapter);
holder.spinnerLenguage
.setOnItemSelectedListener(new OnItemSelectedListener()
...
...
The adapter class is:
private class lenguageSpinnerAdapter extends ArrayAdapter<String> implements
SpinnerAdapter {
private ArrayList<String> lenguages;
public lenguageSpinnerAdapter(Context context,
ArrayList<String> lenguages) {
super(context, R.layout.lenguage_item_layout);
this.lenguages = lenguages;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
// First let's verify the convertView is not null
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lenguage_item_layout,
parent, false);
}
TextView lenguage = (TextView) convertView
.findViewById(R.id.textViewLenguage);
lenguage.setText(lenguages.get(position).toString());
return convertView;
}
}
EDIT: with getCount the error is:
07-11 11:09:20.222: E/AndroidRuntime(32564): FATAL EXCEPTION: main
07-11 11:09:20.222: E/AndroidRuntime(32564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.FlipMainActivity}: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.os.Looper.loop(Looper.java:137)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.app.ActivityThread.main(ActivityThread.java:5328)
07-11 11:09:20.222: E/AndroidRuntime(32564): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 11:09:20.222: E/AndroidRuntime(32564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-11 11:09:20.222: E/AndroidRuntime(32564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-11 11:09:20.222: E/AndroidRuntime(32564): at dalvik.system.NativeStart.main(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-11 11:09:20.222: E/AndroidRuntime(32564):
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.Spinner.onMeasure(Spinner.java:443)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.view.View.measure(View.java:16059)
071 11:09:20.222: E/AndroidRuntime(32564): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.view.View.measure(View.java:16059)
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
...
...
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
07-11 11:09:20.222: E/AndroidRuntime(32564): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
07-11 11:09:20.222: E/AndroidRuntime(32564): ... 29 more
The spinner is draw in the layout but there are no items. Any ideas?
回答1:
Try to implement following code instead of your one.
private class lenguageSpinnerAdapter extends BaseAdapter implements
SpinnerAdapter {
private ArrayList<String> lenguages;
public lenguageSpinnerAdapter(Context context,
ArrayList<String> lenguages) {
this.lenguages = lenguages;
}
public int getCount() {
return lenguages.size();
}
public Object getItem(int position) {
return lenguages.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.get_exercise_activity,
parent, false);
}
TextView lenguage = (TextView) convertView
.findViewById(R.id.btnSave);
lenguage.setText(lenguages.get(position).toString());
return convertView;
}
}
回答2:
see Android Spinner Dropdown Example tutorial this will help you.
来源:https://stackoverflow.com/questions/17589100/spinner-with-custom-layout-doesnt-show-nothing-android