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!