11

我将项目及其详细信息存储在 sqlite 中,并显示项目的图像以及我正在使用的项目名称

       private void getDataAndPopulate() 
       {
        Cursor cursor = getEvents("bcuk_book");
        while (cursor.moveToNext())
        {
             String temp_id = cursor.getString(0);
             final String temp_name = cursor.getString(1);
             String temp_author=cursor.getString(2);
             String temp_desc=cursor.getString(3);
             byte[] temp_image = cursor.getBlob(4);
             String temp_rating = cursor.getString(5);
             String temp_price = cursor.getString(6);
             String temp_update=cursor.getString(7);
             mapId.add(temp_id);
             mapName.add(temp_name);
             mapAuthor.add(temp_author);
             mapDesc.add(temp_desc);
             mapRating.add(temp_rating);
             mapPrice.add(temp_price);
             map.add(temp_image);
             mapUpdated.add(temp_update);
                Log.e("temp_id from the sqlite", temp_id);
                Log.i(temp_name, temp_name);
                Log.i(temp_desc, temp_desc);
                Log.i(temp_rating, temp_rating);
                Log.e(temp_update,temp_update);
                String[] captionArray = (String[]) mapName.toArray(new String[mapName.size()]);
                itemsAdapter = new ItemsAdapter(
                        Bookicon.this, R.layout.item,captionArray);
                gridView=(GridView)findViewById(R.id.list);
                gridView.setAdapter(itemsAdapter);
                gridView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                    {
                        Intent i = new Intent(getApplicationContext(), DetailsActivity.class);

                        i.putExtra("id", position);

                        startActivity(i);
                    }
                });
        }   
        }

我的 Items 适配器类是::

 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 "+mapName.get(POSITION));
            bookTitle=mapName.get(POSITION);
            System.err.println("title of the book is "+bookTitle);
            cap.setText(mapName.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;
        }
    }

显示时的错误是::

       12-21 10:12:28.792: W/dalvikvm(741): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
       12-21 10:12:28.842: E/AndroidRuntime(741): FATAL EXCEPTION: main
       12-21 10:12:28.842: E/AndroidRuntime(741): java.lang.IndexOutOfBoundsException: Invalid index 56, size is 56
       12-21 10:12:28.842: E/AndroidRuntime(741):   at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at java.util.ArrayList.get(ArrayList.java:304)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.books.bcukbook.Bookicon$ItemsAdapter.getView(Bookicon.java:254)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.AbsListView.obtainView(AbsListView.java:2012)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.makeAndAddView(GridView.java:1323)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.makeRow(GridView.java:328)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.moveSelection(GridView.java:885)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.layoutChildren(GridView.java:1230)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.setSelectionInt(GridView.java:1476)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.arrowScroll(GridView.java:1729)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.commonKey(GridView.java:1543)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.widget.GridView.onKeyDown(GridView.java:1494)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.KeyEvent.dispatch(KeyEvent.java:2551)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.View.dispatchKeyEvent(View.java:5500)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1242)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.os.Handler.dispatchMessage(Handler.java:99)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.os.Looper.loop(Looper.java:137)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at android.app.ActivityThread.main(ActivityThread.java:4424)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at java.lang.reflect.Method.invokeNative(Native Method)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at java.lang.reflect.Method.invoke(Method.java:511)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
       12-21 10:12:28.842: E/AndroidRuntime(741):   at dalvik.system.NativeStart.main(Native Method)

代码的结果显示如下

4

1 回答 1

3

在您的第一个代码块中,您需要while在设置GridView以下代码之前关闭循环:

 private void getDataAndPopulate() 
       {
         //assuing that they are not null at this point..
         mapId.clear();
         mapName.clear();
         mapAuthor.clear();
         mapDesc.clear();
         mapRating.clear();
         mapPrice.clear();
         map.clear();
         mapUpdated.clear();

        Cursor cursor = getEvents("bcuk_book");
        while (cursor.moveToNext())
        {
             String temp_id = cursor.getString(0);
             final String temp_name = cursor.getString(1);
             String temp_author=cursor.getString(2);
             String temp_desc=cursor.getString(3);
             byte[] temp_image = cursor.getBlob(4);
             String temp_rating = cursor.getString(5);
             String temp_price = cursor.getString(6);
             String temp_update=cursor.getString(7);
             mapId.add(temp_id);
             mapName.add(temp_name);
             mapAuthor.add(temp_author);
             mapDesc.add(temp_desc);
             mapRating.add(temp_rating);
             mapPrice.add(temp_price);
             map.add(temp_image);
             mapUpdated.add(temp_update);
                Log.e("temp_id from the sqlite", temp_id);
                Log.i(temp_name, temp_name);
                Log.i(temp_desc, temp_desc);
                Log.i(temp_rating, temp_rating);
                Log.e(temp_update,temp_update);
          }//this would close the while loop

                String[] captionArray = (String[]) mapName.toArray(new String[mapName.size()]);
                itemsAdapter = new ItemsAdapter(
                        Bookicon.this, R.layout.item,captionArray);
                gridView=(GridView)findViewById(R.id.list);
                gridView.setAdapter(itemsAdapter);
                gridView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v, int position, long id)
                    {
                        Intent i = new Intent(getApplicationContext(), DetailsActivity.class);

                        i.putExtra("id", position);

                        startActivity(i);
                    }
                });

        } 

while在进入循环之前还要清除所有列表..

假设您正在使用ArrayList我已经更新代码以在进入 while 循环之前清除列表数据..

于 2012-12-21T07:17:27.427 回答