0

I have a gridView that has a custom adapter here:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class GridViewAdapter extends BaseAdapter {
private Context Context;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

CheckBox mCheckBox=null;

// Constructor
public GridViewAdapter(Context c){
    Context = c;
    Log.d("GridViewAdapter", "Constructor is set");

    drawables.add(R.drawable.add_button);
    Log.d("GridViewAdapter", "add_button added");

    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");
}

public void setCheckBox(CheckBox checkbox){
    mCheckBox=checkbox;
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
        view.setId(R.id.iconImageView_id);
    }
    if(checked == true){
        isSdReadable();
        Log.i("GridViewAdapter", "checkbox is checked");
        /*FileInputStream in = null;
        try {
            in = Context.openFileInput("BitmapImage");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Load back the image file to confirm it works
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        view.setImageBitmap(bitmap);
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    } else {
        Log.i("GridView", "Icons not for use/checkbox not checked");
    }

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            ImageView folder = new ImageView(Context);
            folder.setImageResource(R.drawable.ic_launcher);
        }

    });

    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}

But whenever I run this code, the listView items are unclickable and I can't figure out why. I am trying to make it so that when an item in the gridView is clicked, it creates a new imageView called "folder" that displays the ic_launcher. Please help!

user2909006
  • 253
  • 6
  • 22
  • have u set any listeners for gridview, and also u should set up the click listener in this block if (view == null) { view = new ImageView(Context); Log.d("GridViewAdapter", "new imageView added"); view.setId(R.id.iconImageView_id); } – Pulkit Sethi Feb 02 '14 at 00:16

1 Answers1

0

You appear to have added a clickable view inside your list items. This will disable the default behavior of list item click events.

You could circumvent this for tapping by setting android:clickable="true" on the desired list itemview, but bear in mind that this doesn't provide a solution for D-pad devices such as TVs as the view will also need to be able to receive focus.

A more complete solution would be to ensure that the list item can also receive focus. Again, as the default behavior for this is disabled, you will need to provide a focus and touch state for your list item's background.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
  • Ok. Is there a way that I can possibly achieve what I am trying to do without disabling the default dehavior of the item click events? – user2909006 Feb 02 '14 at 03:46
  • Also, in the near future I'm planning on making these items drag and droppable and already have the coding for this all set up (I'm just not using it at the moment). If I am understanding right, would this help to cover "providing a focus and touch state for you list item's background", even though the touch events might be different (long touch vs a normal tap)? – user2909006 Feb 02 '14 at 03:49
  • I'm afraid I can't help you with that. There's [a similar problem description in this question](http://stackoverflow.com/questions/7386340/android-grid-view-with-clickable-grid-items-and-nested-views-buttons-checkbox), perhaps you want to experiment with that a little. If you're implementing drag & drop, it seems like your solution won't work for D-pad devices anyway, so it may not be pertinent to provide navigation for those devices. – Paul Lammertsma Feb 02 '14 at 16:51