Likith Ts Likith Ts - 4 months ago 59
Android Question

Implementing Multi Select in GridView Android

I need to select multiple images inside the

GridView
and add share and delete button to the
Gridview
.

My
ImageAdapter
class:

private class ImageAdapter extends BaseAdapter {

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
CheckableLayout checkableLayout;

if (convertView == null) {
imageView = new ImageView(activity);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
checkableLayout = new CheckableLayout(activity);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth));
} else {
checkableLayout = (CheckableLayout) convertView;
imageView = (ImageView) checkableLayout.getChildAt(0);
}

bitmap = decodeFile(getItem(position), imageWidth, imageWidth);
imageView.setImageBitmap(bitmap);
imageView.setOnClickListener(new OnImageClickListener(position));
return imageView;

}

class OnImageClickListener implements View.OnClickListener {

int position;

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

@Override
public void onClick(View v) {
Intent intent = new Intent(activity, ImageDisplayActivity.class);
intent.putExtra("id", getPosition(position));
activity.startActivity(intent);
}
}

public class CheckableLayout extends FrameLayout implements Checkable {

private boolean mChecked;

public CheckableLayout(Context context) {
super(context);
}

@Override
public void setChecked(boolean checked) {
mChecked = checked;
setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.blue) : null);
}

@Override
public boolean isChecked() {
return mChecked;
}

@Override
public void toggle() {
setChecked(!mChecked);
}
}

public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {

@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
int selectCount = gridView.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount + " items selected");
break;
}
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}
}
}


But it gives me an error when I try to open Gridview.

My Logcat details


02-10 21:10:46.965 21658-21658/com.android.example E/AndroidRuntime﹕
FATAL EXCEPTION: main
Process: com.android.example, PID: 21658
java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.android.example.GridViewActivity$CheckableLayout
at com.android.example.GridViewActivity$ImageAdapter.getView(GridViewActivity.java:224)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.GridView.onMeasure(GridView.java:1044)
at android.view.View.measure(View.java:16540)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16540)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16540)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16540)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

Answer

ClassCastException: android.widget.ImageView cannot be cast to com.android.example.GridViewActivity$CheckableLayout

Because you are returning ImageView from getView but trying to convert it to CheckableLayout in this line:

checkableLayout = (CheckableLayout) convertView;

return checkableLayout from getView() method :

        @Override
        public View getView(final int position, 
                            View convertView, ViewGroup parent) {
            final ImageView imageView;
            CheckableLayout checkableLayout;

            if (convertView == null) {
                imageView = new ImageView(activity); 
                // your code here ... 
                // add ImageView to checkableLayout
                checkableLayout.addView(imageView); 

            } else 
               checkableLayout = (CheckableLayout) convertView;
               imageView = (ImageView) checkableLayout.getChildAt(0);
               // your code here ... 
              return checkableLayout;

   }