问题
I am developing an Android app. In my app, I am working with spinner view. But I am using spinner view with custom adapter because my spinner with will be more complicated soon. All the data for spinner come from server through volley. My spinner view with customer adapter is showing the data properly. But the problem is when I click the dropdown to choose item. It throws exception.
This is my customer adapter for spinner view:
public class SpinnerItemAdapter extends ArrayAdapter<NVItem> {
private ArrayList<NVItem> values;
private Context context;
private ArrayList<String> selectedValues;
static class ViewHolder{
public RadioButton radioButton;
public TextView title;
}
public SpinnerItemAdapter(Context contextParam,ArrayList<NVItem> itemsParam,ArrayList<String> selectedParam)
{
super(contextParam,-1,itemsParam);
this.values = itemsParam;
this.context = contextParam;
this.selectedValues = selectedParam;
}
@Override
public View getView(final int position,View convertView,ViewGroup parent)
{
View rowView = convertView;
final ViewHolder viewHolder;
if(rowView==null)
{
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = layoutInflater.inflate(R.layout.spinner_item,null);
viewHolder = new ViewHolder();
viewHolder.radioButton = (RadioButton)rowView.findViewById(R.id.spinner_item_radio);
viewHolder.title = (TextView)rowView.findViewById(R.id.spinner_item_title);
rowView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) rowView.getTag();
}
//rowView.setId(values.get(position).getId());
viewHolder.title.setText(values.get(position).getName());
return rowView;
}
@Override
public int getCount() { return values.size(); }
@Override
public NVItem getItem(int position) { return values.get(position); }
@Override
public long getItemId(int position) { return position; }
}
This is how I open spinner dialog and set adapter on button click in Android:
private void showCategorySpinner()
{
View view = layoutInflater.inflate(R.layout.choose_spinner,null);
Spinner categorySpinner = (Spinner)view.findViewById(R.id.chooser_spinner);
ArrayList<NVItem> items = new ArrayList<NVItem>();
for(int i=0;i<categoryItems.size();i++)
{
NVItem item = new NVItem();
item.setId(categoryItems.get(i).getId());
item.setName(categoryItems.get(i).getName());
items.add(item);
}
ArrayAdapter adapter = new SpinnerItemAdapter(getBaseContext(),items,new ArrayList<String>());
categorySpinner.setAdapter(adapter);
AlertDialog.Builder b = new AlertDialog.Builder(CreateItemActivity.this);
b.setView(view);
b.show();
}
Above function is called when button that open spinner dialog is clicked.
All data binding to spinner and opening spinner dialog works find. As in the screenshot below. But it throws error when the button that is circled in the screenshot is clicked.
This is screenshot:
This is the errors in logcat when that dropdown arrow is clicked:
03-17 22:26:37.925 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion W/EGL_genymotion: eglSurfaceAttrib not implemented
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion W/ResourceType: No known package when getting value for resource number 0xffffffff
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion D/AndroidRuntime: Shutting down VM
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa61c8908)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.content.res.Resources.getValue(Resources.java:1014)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.content.res.Resources.getLayout(Resources.java:853)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:737)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:733)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.Spinner.measureContentWidth(Spinner.java:681)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.Spinner$DropdownPopup.show(Spinner.java:959)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.widget.Spinner.performClick(Spinner.java:609)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 22:29:01.685 32568-32568/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
What is wrong with my code?
Edit
When I change the constructor of Adapter like this
super(contextParam,R.layout.support_simple_spinner_dropdown_item,valuesParam);
List items appear like below screenshot.
回答1:
No need to change your adapter except to implement getDropDownView()
in your adapter -
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View dropDownView = inflater.inflate(R.layout.dropdown, parent, false);
TextView view1 = (TextView)dropDownView.findViewById(<view_id>);
view1.setText(values.get(position).getName());
return dropDownView;
}
where R.layout.dropdown
is the layout xml for the dropdown view.It can be any UI - simple or complex.
来源:https://stackoverflow.com/questions/36072859/spinner-with-customer-adapter-throwing-resourcesnotfoundexception-when-dropdown