user1985943
user1985943

Reputation: 83

Android grid view issue

In grid view I’ve selected two images(Checkbox) and I’ve scrolled down the view to the bottom and somehow these selections are displayed on completely different images.

This is my Code.Please help me on this

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GridViewImageAdapter extends BaseAdapter {

    private static final float ASPECT_RATIO = 10/16f; //To show grid view items as rectangles instead of squares
    private Activity mActivity;
    private ArrayList<File> mFiles = new ArrayList<File>();
    private ArrayList<File> mSelectedFiles = new ArrayList<File>();
    private int mImageWidth;
    private File mCurrentFolder;
    private PresentationUtils mUtils;

    public GridViewImageAdapter gridViewAdpter = this;

    Resources resources;
    public GridViewImageAdapter(Activity activity, File folderToScan,
            int imageWidth) {
        this.mActivity = activity;
        this.mCurrentFolder = folderToScan;
        this.mImageWidth = imageWidth;
        this.mFiles = new PresentationUtils(mActivity).getFiles(folderToScan.getAbsolutePath(), true);
        mUtils = new PresentationUtils(mActivity);
    }

    @Override
    public int getCount() {
        return this.mFiles.size();
    }

    @Override
    public Object getItem(int position) {
        return this.mFiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView; 
        final CheckBox gViewItemSelBt;
        TextView label;
        View v;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.grid_view_item, null);

        } else {
            v = convertView;
        }

        imageView = (ImageView) v.findViewById(R.id.image);
        label = (TextView) v.findViewById(R.id.label);
        gViewItemSelBt = (CheckBox) v.findViewById(R.id.imageSelector);

        // get screen dimensions
        Bitmap image = null;
        if(!mFiles.get(position).isDirectory()){
                image = decodeFile(mFiles.get(position).getAbsolutePath(), mImageWidth,
                mImageWidth);
                final int pos = position;
                gViewItemSelBt.setVisibility(View.VISIBLE);             
                gViewItemSelBt.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(gViewItemSelBt.isChecked()){
                        mSelectedFiles.add(mFiles.get(pos));
                    }else{
                        mSelectedFiles.remove(mFiles.get(pos));
                    };
                    }
                });

        } else {
            image = BitmapFactory.decodeResource(mActivity.getResources(),
                    R.drawable.ic_folder);
        /*File latestFile =     mFiles.get(mFiles.size() -1);

            image = BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.ic_folder );
            image = decodeFile(latestFile.getAbsolutePath(), mImageWidth,
                    mImageWidth);*/
            gViewItemSelBt.setVisibility(View.GONE);
        }
        int height = (int) (mImageWidth*ASPECT_RATIO);
        imageView.setImageBitmap(image);
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(mImageWidth, height));
        imageView.setOnClickListener(new OnImageClickListener(position));   

        if(mUtils.getSharedValue(mActivity, mFiles.get(position).getName()).length() > 0 ){
            mActivity.getActionBar().setTitle(mCurrentFolder.getName());
            label.setText(mUtils.getSharedValue(mActivity, mFiles.get(position).getName()));
        }
        else{
        label.setText(mFiles.get(position).getName());
        mActivity.getActionBar().setTitle("Albums");
        }
        return v;
    }

    class OnImageClickListener implements OnClickListener {

        int mPostion;
        // constructor
        public OnImageClickListener(int position) {
            this.mPostion = position;
        }

        @Override
        public void onClick(View v) {
            // on selecting grid view image
            // launch full screen activity

            if(mFiles.get(mPostion).isDirectory()){     
                //We just need to go one level deeper and load images from there
                mCurrentFolder = new File(mFiles.get(mPostion).getAbsolutePath());
                loadFiles();
                mActivity.invalidateOptionsMenu();
            }else{
                //An image has been clicked, open in full screen view
                Intent i = new Intent(mActivity, FullScreenViewActivity.class);
                i.putExtra(AppConstant.INTENT_DATA_POSITION, mPostion);
                i.putExtra(AppConstant.INTENT_DATA_FOLDER, mCurrentFolder.getAbsolutePath());           
                i.putExtra(AppConstant.INTENT_DATA_IMAGENAME, mFiles.get(mPostion).getName());
                mActivity.startActivity(i);

            }
        }
    }

    /*
     * Resizing image size
     */
    public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
        try {

            File f = new File(filePath);

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            if(!f.isDirectory())
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            final int REQUIRED_WIDTH = WIDTH;
            final int REQUIRED_HIGHT = HIGHT;
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                    && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            if(!f.isDirectory()){
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            }else{
                return null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void goBackOneLevel() {
        mCurrentFolder = mCurrentFolder.getParentFile();
        mActivity.getActionBar().setTitle("Albums");
        loadFiles();
    }

    public boolean isAtRootFolder() {
        File rootImageFolder = new File(mActivity.getFilesDir().getPath()
                + File.separator + AppConstant.PHOTO_GALLERY_ROOT_FOLDER);
        if(mCurrentFolder.compareTo(rootImageFolder) == 0){
            return true;
        }
        return false;
    }

    public void loadFiles() {
        mFiles = mUtils.getFiles(mCurrentFolder.getAbsolutePath(), true);
        notifyDataSetChanged();
    }

    public void createAlbum(String albumName){

        mUtils.moveFiles(this.mSelectedFiles, albumName);
    }

    public void deleteFile(){
        mUtils.deleteFile(this.mSelectedFiles);
    }

    public boolean isFileSelected(){
        notifyDataSetChanged();
        if(this.mSelectedFiles.isEmpty()){
            return false;
        }else{
            return true;
        }
    }

}

Upvotes: 0

Views: 68

Answers (1)

Related Questions