2

我在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)

我的项目适配器类是

     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 数据但无法显示它。我称这个项目适配器类用于在两种情况下显示,即显示和更新后。知道我哪里出错了吗?

4

1 回答 1

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有更多...

您应该只使用一个列表来为您的适配器供电。

于 2013-01-02T05:42:26.057 回答