1

Am having a gridview which sets the views based on the gridview size and the col width, everything fine when we start but when i change the orientation, the views are recreated and the check boxes which are checked are refreshed, which i dont want. I need to maintain the scrolled sate as well the check box state.

Here is my code in getview:

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageItem item = images.get(position);
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            holder.checkbox.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();
                    if (images.get(id).selection) {
                        cb.setChecked(false);
                        images.get(id).selection = false;
                    } else {
                        cb.setChecked(true);
                        images.get(id).selection = true;
                    }
                }
            });
            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = v.getId();
                    ImageItem item = images.get(id);
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    final String[] columns = { MediaStore.Images.Media.DATA };
                    Cursor imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            columns, MediaStore.Images.Media._ID + " = " + item.id, null, MediaStore.Images.Media._ID);
                    if (imagecursor != null && imagecursor.getCount() > 0) {
                        imagecursor.moveToPosition(0);
                        String path = imagecursor.getString(imagecursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                        File file = new File(path);
                        imagecursor.close();
                        intent.setDataAndType(Uri.fromFile(file), "image/*");
                        startActivityForResult(intent, VIEW_IMAGE);
                    }
                }
            });
            // holder.imageview.setImageBitmap(item.img);
            holder.imageview.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (holder.imageview.getLayoutParams().height != mItemHeight) {
                holder.imageview.setLayoutParams(mImageViewLayoutParams);
            }
            bitmapFromId.DisplayImage(item.id, holder.imageview);
            // holder.imageview.setImageBitmap(item.img);
            holder.checkbox.setChecked(item.selection);
            return convertView;
        }

If i set the android:configChanges="orientation" it does not update my views in the Grid

Stefan
  • 5,203
  • 8
  • 27
  • 51
sukarno
  • 597
  • 2
  • 10
  • 31

3 Answers3

0

add the item blew in your activity at androidmanifest.xml

android:configChanges="orientation|screenSize|keyboardHidden"
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
nick.yu
  • 89
  • 3
  • If i set the android:configChanges="orientation" it does not update my views in the Grid. i need to update the views on the orientation change – sukarno Feb 04 '13 at 07:29
  • Everytime I see config change orientation I give a -1 for lazyness – Necronet Oct 29 '13 at 19:14
0

Add this in your manifest

<activity
            android:name=".Your_Activity"
            android:configChanges="keyboardHidden|orientation" >

And make your changes to update view in Java code at Window.onOrientationChange

Harpreet
  • 2,990
  • 3
  • 38
  • 52
0

You can retain the state before config changes and restore it after it got changed. You can do something similar as shown below.

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onRestoreInstanceState(android.os.Bundle)
 */
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int scrollTo = savedInstanceState.getInt("firstItem");
    gridView.setSelection(scrollTo);
    setSelectedItem(savedInstanceState.getInt("selectedItem"));//A method to set the selected Item
}


/* (non-Javadoc)
 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    int scrollTo = gridView.getFirstVisiblePosition();
    outState.putInt("firstItem", scrollTo);
    int selectedItem = getSelectedImageItem();//A method to retrieve the selected item from the adapter
    outState.putInt("selectedItem", selectedItem);
    /*additionally you can pass the entire collection into the bundle and retrieve it as well 
    using android.app.Activity#onRetainNonConfigurationInstance*/
}

Please go through the below link for further information, this will help you in the long run.

Handling Runtime config changes

Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43