fgallego
fgallego

Reputation: 39

Android - OnItemClickListener dont't capture event click in adapterview

I have an activity that has a AdapterView to display a gridview of ImageView

The Activity:

package com.xlck.mislistas

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.xlck.mislistas.adapters.ExpandableHeightGridView;
import com.xlck.mislistas.adapters.ImageGridAdapter;
import com.xlck.mislistas.adapters.ImageGridAdapter.ViewHolder;
import com.xlck.mislistas.adapters.ImageGridBean;

public class AmigosActivity extends SherlockActivity {
...

    private ExpandableHeightGridView gridViewImagenes;
        gridViewImagenes = (ExpandableHeightGridView) findViewById(R.id.grvImagenes);

    // Adapter GridView
    gridViewImagenes.setAdapter(imageGridAdapter);
    gridViewImagenes.setExpanded(true);
    .
    .
    .

    // Listener
    gridViewImagenes.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Do something ...;
        }
    });
}

The Adapter:

package com.xlck.mislistas.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.xlck.mislistas.R;

public class ImageGridAdapter extends BaseAdapter {

private Context mContext;

private List<ImageGridBean> items;

// Constructor
public ImageGridAdapter(Context context, ArrayList<ImageGridBean> items) {
    this.mContext = context;
    this.items = items;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    ImageGridBean item = (ImageGridBean) items.get(position);

    LayoutInflater mInflater = (LayoutInflater) mContext
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.item_grid_imagen, null);
        holder = new ViewHolder();
        holder.txtId = (TextView) convertView.findViewById(R.id.uid);
        holder.txtNombre = (TextView) convertView
                .findViewById(R.id.txtNombre);
        holder.imagen = (ImageView) convertView
                .findViewById(R.id.imgImagen);

        holder.check = (CheckBox) convertView.findViewById(R.id.chkItem);
        holder.txtFondoNombre = (TextView) convertView.findViewById(R.id.txtFondoNombre);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtId.setText(item.getId());
    holder.txtNombre.setText(item.getNombre());
    holder.imagen.setImageBitmap(item.getImagen());
    if (item.getId().equals("0"))
        holder.check.setVisibility(View.INVISIBLE);

    return convertView;
}

@Override
public ImageGridBean getItem(int position) {
    return items.get(position);
}

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


// --------------------------------------------------------< ViewHolder >---
// -------------------------------------------------------------------------

/* private view holder class */
public class ViewHolder {
    public TextView txtId;
    public TextView txtNombre;
    public TextView txtFondoNombre;
    public ImageView imagen;
    public CheckBox check;
}
}

I have this Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants" >

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="92dp"
        android:layout_height="92dp"
        android:layout_alignParentBottom="false"
        android:layout_alignParentLeft="false" >

        <TextView
            .../>

        <ImageView
            .../>

        <TextView
            .../>

        <TextView
            ... />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="38dp"
        android:layout_height="32dp"
        android:layout_alignRight="@+id/relativeLayout2"
        android:layout_alignTop="@+id/relativeLayout2"
        android:layout_marginRight="0dp"
        android:layout_marginTop="4dp" >

        <CheckBox
            android:id="@+id/chkItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

Well, if i click in ImageView, it fire the event click and the listener capture it, but if i click in CheckBox the event click don't fire.

What I doing wrong? What I need to do?

Thanks in advance.

Upvotes: 1

Views: 2496

Answers (4)

badre zouiri
badre zouiri

Reputation: 56

android:focusableInTouchMode="false" android:focusable="false"

Upvotes: 1

fgallego
fgallego

Reputation: 39

I finally decided to put the adapter into the Activity class. In Class Adapter put a listener both ImageView and CheckBox component, since these listener invoking a method of the Activity class.

Thanks to all for your time and response!!!

Upvotes: 0

neo108
neo108

Reputation: 5244

Think you will have to implement a OnCheckedChangelistener to the checkbox, in your case to chkItem.

See CompoundButton.OnCheckedChangeListener

The implementation would be something like...

CheckBox checkBox = (CheckBox) findViewById(R.id.chkItem);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {   
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            // checkbox is checked - doSomething()
        } else {
            // checkbox is unchecked
        }
    }
});

Hope this helps.

Upvotes: 0

Anton Cherkashyn
Anton Cherkashyn

Reputation: 5859

@Sam is right, checkboxes handle clicks on their own and the click event never reaches the AdapterView.

Now, technically speaking, you could set android:clickable="false" for your checkbox in the layout file, and then the click event would get delivered up the view hierarchy, but in this case you won't be able to do anything useful with the checkbox, it will just always stay unchecked.

Here's what would be a better approach. Move the "do something" into a different method and add another onClickListener for the checkbox, to do the same thing:

gridViewImagenes.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        doSomething();
    }
});

myCheckbox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        doSomething();
    }
});

}

private void doSomething() {
    //Do Stuff
}

Upvotes: 0

Related Questions