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 に答える