Praveen
Praveen

Reputation: 2167

Onclick is being called multiple times

I have a list view with a checkbox and clickable textviews defined as below:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusableInTouchMode="false"
        android:focusable="false">
        <CheckBox
            android:id="@+id/favouritebutton"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_centerHorizontal="true"
            android:text="Like"
            android:focusable="false"
            android:clickable="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:textSize="12sp">
        </CheckBox>
        <TextView
            android:id="@+id/behindPlanItemLabel"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:drawablePadding="-5dp"
            android:focusable="false"
            android:clickable="true"
            android:gravity="center_vertical"
            android:text="Plan"
            android:textColor="#000000"
            style="@style/behindPlanItemLabel"
            android:textSize="12sp" />
        <TextView
            android:id="@+id/behindTimeItemLabel"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:drawablePadding="-5dp"
            android:gravity="center_vertical"
            android:text="Timer"
            android:focusable="false"
            android:clickable="true"
            android:textColor="#000000"
            style="@style/behindTimerItemLabel"
            android:textSize="12sp" />
    </LinearLayout>

I have defined a CustomerAdapter which extends CursorAdapter and implements onClickListener. In the newView method, i am adding the listener on each of the button and text views as

        viewHolder.timerView.setOnClickListener(this);
        viewHolder.likeBox.setOnClickListener(this);
        viewHolder.planView.setOnClickListener(this);

I have overriden the Onclick view to catch the click.
When I click the timer which is the 3rd item in the layout,
the onclick is called once but when I click the 'plan' which is the 2nd item in the layout,
the onCLick is called twice and when i click the 'like' which is the 1st item in the layout onclick is called 3 times.
I need to get only oneClick callback for any click.
Any suggestion on what I am doing wrong here ?

Below is the full onCLick code

      @Override
        public void onClick(View view) {
            int actionType;

            switch (view.getId()) {
                case R.id.favouritebutton:
                    Log.d(LOG_TAG, "favourite button");
                    actionType = Constants.CLICKED_FAV;
                    // Do something
                case R.id.behindPlanItemLabel:
                    Log.d(LOG_TAG, "plan button");
                    actionType = Constants.CLICKED_PLAN;
                    // Do something
                case R.id.behindTimeItemLabel:
                    Log.d(LOG_TAG, "timer button");
                    actionType = Constants.CLICKED_TIMER;
                    // Do something
            }
    }

Upvotes: 2

Views: 2007

Answers (2)

Praveen
Praveen

Reputation: 2167

Thanks for your inputs. My problem is now fixed by adding return in the each switch case statement.

           switch (view.getId()) {
                case R.id.favouritebutton:
                    Log.d(LOG_TAG, "favourite button");
                    actionType = Constants.CLICKED_FAV;
                    // Do something
                 return;
                case R.id.behindPlanItemLabel:
                    Log.d(LOG_TAG, "plan button");
                    actionType = Constants.CLICKED_PLAN;
                    // Do something
                   return;
                case R.id.behindTimeItemLabel:
                    Log.d(LOG_TAG, "timer button");
                    actionType = Constants.CLICKED_TIMER;
                    // Do something
                    return;
            }

Upvotes: 2

AAA
AAA

Reputation: 505

use this in your onclick listener inside adapter if it goes to other activity. intentName.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 0

Related Questions