1

I am trying to implement custom spinner style for the android spinner. There is a problem when I select an item in the spinner it doesnt show on the spinner (the selected item). I have several problems in my code. Here is the log cat

01-06 08:21:38.254: E/AndroidRuntime(733): FATAL EXCEPTION: main 01-06 08:21:38.254: E/AndroidRuntime(733): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 01-06 08:21:38.254: E/AndroidRuntime(733): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 01-06 08:21:38.254: E/AndroidRuntime(733): at java.util.ArrayList.get(ArrayList.java:304) 01-06 08:21:38.254: E/AndroidRuntime(733): at com.xx.x.Page5SubActivity$MyAdapter.getCustomView(Page5SubActivity.java:487) 01-06 08:21:38.254: E/AndroidRuntime(733): at com.xx.x.Page5SubActivity$MyAdapter.getView(Page5SubActivity.java:479) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.Spinner.onMeasure(Spinner.java:285) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.View.measure(View.java:12723) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.os.Handler.dispatchMessage(Handler.java:99) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.os.Looper.loop(Looper.java:137) 01-06 08:21:38.254: E/AndroidRuntime(733): at android.app.ActivityThread.main(ActivityThread.java:4424) 01-06 08:21:38.254: E/AndroidRuntime(733): at java.lang.reflect.Method.invokeNative(Native Method) 01-06 08:21:38.254: E/AndroidRuntime(733): at java.lang.reflect.Method.invoke(Method.java:511) 01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-06 08:21:38.254: E/AndroidRuntime(733): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-06 08:21:38.254: E/AndroidRuntime(733): at dalvik.system.NativeStart.main(Native Method) 

Here is the class I use to customization of the spinner

public class MyAdapter extends ArrayAdapter<String> { private List<String> listString = new ArrayList<String>(); public MyAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); //listString = objects; } @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 convertView, ViewGroup parent) { LayoutInflater inflater=getLayoutInflater(); View row=inflater.inflate(R.layout.spinner, parent, false); TextView label=(TextView)row.findViewById(R.id.textView1); label.setText(listString.get(position)); return row; } } 

above error started when I comment this line //listString = objects; I commented it because it has been initialized with super method. super(context, textViewResourceId, objects); Can anybody explain me this? In OOP we use to super for initializing content with super class values. So why do I need to reinitialize that? Or can't we use super method for 2 parameters and initialize //listString = objects; in the above class? I know I am lack of OOP. can anybody explain me?

Second problem is when I remove comments it starts to work. But it doesn't show up the item that I selected. I have written on item selected events. They works. what else I have to do to work this?

4
  • 2
    I can guarantee you that a private variable in your class has not been initialized with a call to super(). How would a parent class know anything about a private variable declared in a subclass? Commented Jan 6, 2013 at 3:07
  • yes I have declared `private List<String> listString = new ArrayList<String>();' private . Plz give me somtimes till project is built Commented Jan 6, 2013 at 3:12
  • I have the second problem still there. Why doesn't spinner is not showing the selected item on the spinner? Commented Jan 6, 2013 at 3:23
  • @Brian Roach, though I declared sub class variable as private . This works if I remove the comments before //listString = objects; Commented Jan 6, 2013 at 3:33

1 Answer 1

1

listString cannot be initialized by the super class. It is a member of the MyAdapter class and not the super class.

private List<String> listString = new ArrayList<String>(); 

At the time of the get call listString is still empty, and thus the exception.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks and why doesn't it shows the selected item on the spinner?
@MenukaDevinda, is the problem still persisting even after fixing the above code for the exception?
Now it is ok after, I remove the comments spinner works but still there is my second problem
@MenukaDevinda I am not really sure, perhaps it merits a different question?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.