user3526598
user3526598

Reputation: 71

Clicking on an item in grid does not evoke listener on checkbox present in grid element

There is gridview and I am adding adapter to it.

 GridView numgrid = (GridView) findViewById(R.id.selectNumberGrid);
 numberAdapter = new NumberAdapter(this);
 numgrid.setAdapter(numberAdapter);

The NumberAdapter code is like this

public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if(convertView==null) {
        /****** Inflate xml file for each row ( Defined below ) *******/
        holder = new ViewHolder();
        convertView = inflater.inflate(
                R.layout.select_number_item, null);
        holder.textview = (TextView) convertView.findViewById(R.id.thumbText);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    holder.checkbox.setId(position);
    holder.textview.setId(position*100);

    //set values
    holder.textview.setText(data[position]);
    holder.id = position;
    if(((MyApplication) activity.getApplication()).numbers.contains(Integer.parseInt(data[position])))
        holder.checkbox.setChecked(true);
    else
        holder.checkbox.setChecked(false);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb  = (CheckBox)((ViewGroup)v).getChildAt(1);
            TextView tv = (TextView) ((ViewGroup)v).getChildAt(0);
            if(cb.isChecked()) {
                if(((MyApplication) activity.getApplication()).numbers.size()==1)
                    Toast.makeText(activity,"Select one or more numbers",Toast.LENGTH_SHORT).show();
                else {
                    cb.setChecked(false);
                    ((MyApplication) activity.getApplication()).changeNumState(false, Integer.parseInt(tv.getText().toString()));
                }
            }
            else {
                cb.setChecked(true);
                ((MyApplication) activity.getApplication()).changeNumState(true, Integer.parseInt(tv.getText().toString()));
            }
            Log.i("TAG","clicked");
        }
    });



    return convertView;
}

The xml files are like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"
 android:layout_height="match_parent"       android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.thakoor.timestables.SelectNumbers"
android:background="#FFFFFF">

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:entries="@array/options"
    android:prompt="@string/options_prompt"
    android:spinnerMode="dialog"
    />

<GridView android:id="@+id/selectNumberGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:verticalSpacing="2dp"
    android:horizontalSpacing="2dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_below="@id/spinner"
    android:background="#e1e1e1"
    android:fadeScrollbars="false"
    android:scrollbarStyle="outsideInset"
    />


 </RelativeLayout>

and the element in grid - xml file is like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<TextView
    android:id="@+id/thumbText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:textSize="20sp"/>

<CheckBox
    android:id="@+id/itemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:scaleX="0.90"
    android:scaleY="0.90"
    />
</RelativeLayout>

Whenever I click on item in gridview 1. When I click on textview onclick event is called 2. But When I click on the checkbox, onclick event is not called

I want to call onclick event on the checkbox as well.. What should I do

Upvotes: 0

Views: 69

Answers (1)

Prathmesh Swaroop
Prathmesh Swaroop

Reputation: 621

use setOnCheckChangeListener() or onClick on holder.checkbox instead of on convertView. If you add onClick on convertView click event will be consumed by the parent and same event is passed down to all of its child views.

To Acheive the check/uncheck behaviour on click on textView you can have some like this:

 CheckBox cb  = (CheckBox)((ViewGroup)v).getChildAt(1);
 TextView tv = (TextView) ((ViewGroup)v).getChildAt(0);
 cb.setOnCheckChangeListener(new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)    {
              //your check/uncheck logic
          }
 });

 tv.setOnClickListener(new OnClickListener(){
  @Override
  public void OnClick(View v){
     if (holder.checkbox.isChecked()) {
         holder.checkbox.setChecked(false);
    } else {
         holder.checkbox.setChecked(true);
    }
  } 
 });

Upvotes: 1

Related Questions