AliR
AliR

Reputation: 2085

ViewHolder for a gridview -- checkbox checked with onclicklistener not working

I have a ViewHolder that has 3 textboxs, an image, and a checkbox in a gridview. The problem is that I can't seem capture/trigger onclicklistener after I added the checkbox to this viewholder. Only the checkbox seems to set to checked or unchecked. If I click on other area that is on image or textbox I want to trigger onClick event.

Also, if someone has clue if we can setclickable to false for a few checkbox, so it can be viewed but not active to respond.

My code at the moment is in the ImageAdpater:

public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    ImageView imgView = null;

        if (convertView == null) {

            holder = new ViewHolder(); 
            LayoutInflater ltInflate = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 
            convertView = ltInflate.inflate(R.layout.griditem, null);

            holder.textview1 = (TextView) convertView.findViewById(R.id.grid_item_alert_date);
            holder.textview2 = (TextView) convertView.findViewById(R.id.grid_item_alert_time);
            holder.textview3 = (TextView) convertView.findViewById(R.id.grid_item_alert_type);

            holder.imageview    = (ImageView) convertView.findViewById(R.id.grid_item_image);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_ack);
            convertView.setTag(holder);

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

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 // TODO Auto-generated method stub
                  Toast.makeText(context, " checkbox checked", Toast.LENGTH_SHORT).show();

              } });

        holder.textview1.setText("Text 1 ");
        holder.textview2.setText("Text 2 ");
        holder.textview3.setText("Text 3 ");
        holder.checkbox.setChecked(false);
        holder.imageview.setImageBitmap(bitmap);
        holder.id = position;

        return convertView;

}

Code at the Activity from where imageadpater is called:

 GridView gridview = (GridView) findViewById(R.id.mygridview);
        adapter = new ImageAdapter(this);
        gridview.setAdapter(adapter);

        gridview.setOnItemClickListener(new OnItemClickListener()
        {   
            public void onItemClick(AdapterView<?> parent, View v, int position, long id)
            {
                Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
                        Toast.LENGTH_SHORT).show();
            }
        }
        );

So the gridview.setOnItemClickListener in the above is not being called, why? I have tried different setups but nothing seems to work, only checkbox event works and that too if defined in getView as above.

In need of an urgent solution, Cheers

Upvotes: 0

Views: 4179

Answers (1)

AliR
AliR

Reputation: 2085

adding these lines to the checkbox in the layout xml:

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

This does the trick.

Upvotes: 2

Related Questions