0

Background:

I am working on a image gridview, where the app will first load in the chosen category (integer) so as to generate the respective arraylist of image url, and then fitting the image to the gridview.

Also, there is a spinner in the actionbar listing out the image category, and I would like to implement in a way that the gridview will refresh upon the chosen category in the spinner.

Code:

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    default_category = Constants.gridview_category_chosen; // integer
    generate_array(default_category); // to generate the array of image urls to load to adapter 
    ....
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
    listView = (GridView) rootView.findViewById(R.id.grid);
    _ImageAdapter = new ImageAdapter();
    ((GridView) listView).setAdapter(_ImageAdapter);
    ....
 }

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) 
{
    Constants.custom_toast(getActivity(), ""+itemPosition+"   "+itemId, "");
    Constants.gridview_category_chosen = itemPosition;
    default_category = Constants.gridview_category_chosen; 
    generate_array(default_category);
    _ImageAdapter.notifyDataSetChanged();       
    listView.setAdapter(new ImageAdapter());
    return false;
}

Logcat:

10-07 22:22:48.711: W/dalvikvm(13464): threadid=1: thread exiting with uncaught exception (group=0x41d17700)
10-07 22:22:48.721: E/AndroidRuntime(13464): FATAL EXCEPTION: main
10-07 22:22:48.721: E/AndroidRuntime(13464): java.lang.ArrayIndexOutOfBoundsException: length=44; index=44
10-07 22:22:48.721: E/AndroidRuntime(13464):    at com.abc.app.fragment.ImageGridFragment.generate_array(ImageGridFragment.java:91)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at com.abc.app.fragment.ImageGridFragment.onNavigationItemSelected(ImageGridFragment.java:165)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:235)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:899)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.widget.AdapterView.access$200(AdapterView.java:50)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:863)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.os.Handler.handleCallback(Handler.java:730)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.os.Looper.loop(Looper.java:137)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at android.app.ActivityThread.main(ActivityThread.java:5493)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at java.lang.reflect.Method.invokeNative(Native Method)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at java.lang.reflect.Method.invoke(Method.java:525)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
10-07 22:22:48.721: E/AndroidRuntime(13464):    at dalvik.system.NativeStart.main(Native Method)

Question:

Beforehand there is nothing inside onNavigationItemSelected loop. After adding the contents in the above code, errors as in the logcat occurs.

Currently there are 44 images as in the image url (i.e. per logcat it seem generate the arraylist for another time making it ArrayIndexOutOfBoundsException?)

I have researched Refresh GridView contents with custom BaseAdapter, clear a custom adapter or similar but still have the same problem. How could such be modified? Thanks in advance!

Community
  • 1
  • 1
pearmak
  • 4,979
  • 15
  • 64
  • 122

1 Answers1

0

java.lang.ArrayIndexOutOfBoundsException: length=44; index=44

Indexing begins from 0 so the maximum you can go is 43. 0 through 43 makes 44 elements in your data set. I am guessing your spinner shows numbers from 1 to 44 ?

I would like to implement in a way that the gridview will refresh upon the chosen category in the spinner.

Items in the ActionBar can not be retrieved using findViewById(). You need to retrieve them in the onCreateOptionsMenu() like so:

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.sort_spinner, menu);
    MenuItem spinnerItem = menu.findItem(R.id.menuid);
    Spinner spinner = (Spinner)spinnerItem.getActionView().findViewById(R.id.spinner);
    //Your rest of code... 
    return super.onCreateOptionsMenu(menu);
}   

Source: Get reference to a Spinner in ActionBar

Once you get your reference to the Spinner, add the onItemSelectedListener to listen for any item clicks on your Spinner.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Thanks a lot for your prompt response. For further details, there are 44 images, each image is assigned a category, and there are only 20 categories. For the ActionBar, i have `implements ActionBar.OnNavigationListener` of which the `onNavigationItemSelected` in the code reponsing to. – pearmak Oct 07 '14 at 14:58
  • All the works perform perfectly without the involvement of the Spinner in the actionBar. – pearmak Oct 07 '14 at 14:59