我正在处理图像gridview,其中app将首先加载所选类别(整数),以便生成图像url的相应arraylist,然后将图像拟合到gridview。
此外,操作栏中有一个微调器列出了图像类别,我希望以一种方式实现gridview将刷新微调器中所选的类别。
@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;
}
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)
事先onNavigationItemSelected
循环内没有任何内容。在上面的代码中添加内容后,会出现logcat中的错误。
目前图像网址中有44个图像(例如,每个logcat,它似乎再次生成arraylist,使其成为ArrayIndexOutOfBoundsException
?)
我研究过Refresh GridView contents with custom BaseAdapter,clear a custom adapter或类似问题,但仍有同样的问题。 怎么会这样修改?提前谢谢!
答案 0 :(得分:0)
java.lang.ArrayIndexOutOfBoundsException:length = 44; index = 44
索引从0开始,因此您可以获得的最大值为43. 0到43可以在数据集中生成44个元素。我猜你的微调器显示从1到44的数字?
我想以gridview将刷新的方式实现 微调器中选择的类别。
使用ActionBar
无法检索findViewById()
中的项目。您需要在onCreateOptionsMenu()
中检索它们,如下所示:
@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);
}
来源:Get reference to a Spinner in ActionBar
获得Spinner
的引用后,添加onItemSelectedListener
即可收听Spinner上的任何商品点击。