我正在gridview中显示sqlite中的项目..第一次它正好显示项目。将新项目更新为sqlite后,新项目无法显示..
我的错误日志显示为
01-02 10:57:45.497: E/AndroidRuntime(1274): java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
01-02 10:57:45.497: E/AndroidRuntime(1274): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-02 10:57:45.497: E/AndroidRuntime(1274): at java.util.ArrayList.get(ArrayList.java:304)
01-02 10:57:45.497: E/AndroidRuntime(1274): at com.books.bcukbook.Bookicon$ItemsAdapter.getView(Bookicon.java:262)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.GridView.makeAndAddView(GridView.java:1323)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.GridView.makeRow(GridView.java:328)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.GridView.fillDown(GridView.java:281)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.GridView.fillFromTop(GridView.java:403)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.GridView.layoutChildren(GridView.java:1215)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.View.layout(View.java:11278)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.View.layout(View.java:11278)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.View.layout(View.java:11278)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.View.layout(View.java:11278)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.View.layout(View.java:11278)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.os.Looper.loop(Looper.java:137)
01-02 10:57:45.497: E/AndroidRuntime(1274): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-02 10:57:45.497: E/AndroidRuntime(1274): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 10:57:45.497: E/AndroidRuntime(1274): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 10:57:45.497: E/AndroidRuntime(1274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-02 10:57:45.497: E/AndroidRuntime(1274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-02 10:57:45.497: E/AndroidRuntime(1274): at dalvik.system.NativeStart.main(Native Method)
我的itemsadapter类是
private class ItemsAdapter extends BaseAdapter implements ListAdapter
{
String[] items;
public ItemsAdapter(Context context,int textViewResourceId,String[] items)
{
this.items = items;
}
public View getView( int POSITION, View convertView,ViewGroup parent)
{
TextView cap = null ;
View view = convertView;
ImageView img = null;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.item, null);
}
img = (ImageView) view.findViewById(R.id.image);
Log.i("iiiiii","iiiii");
cap = (TextView) view.findViewById(R.id.caption);
System.err.println("Position of the book is "+mapNames.get(POSITION));
bookTitle=mapNames.get(POSITION);
System.err.println("title of the book is "+bookTitle);
cap.setText(mapNames.get(POSITION));
img.setImageBitmap(BitmapFactory.decodeByteArray(map.get(POSITION), 0, map.get(POSITION).length));
return view;
}
public int getCount()
{
return items.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
}
我正在追踪值,但我得到sqlite数据,但无法显示。 我正在调用这个itemsadapter类来显示两种情况,即显示和更新后。我知道哪里出错了?
答案 0 :(得分:1)
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
...
at com.books.bcukbook.Bookicon$ItemsAdapter.getView(Bookicon.java:262)
您正在使用两种不同的索引。 getCount()
基于items
:
public int getCount()
{
return items.length;
}
但是在getView()
中您正在使用mapNames.get(POSITION)
。显然mapNames
只有7个元素而items
有更多...
您应该只使用一个列表来为适配器供电。