0

Is it possible to bind onClickListener for listView item buttons in activity? My listView item has name, delete and edit buttons. listView items are stored in List.

exercises_list_view_item.xml

    <?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="40dp"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <Button
        android:id="@+id/deleteExerciseBtn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="@android:drawable/ic_delete" />

    <Button
        android:id="@+id/editExerciseBtn"
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="7dp"
        android:layout_toLeftOf="@id/deleteExerciseBtn"
        android:background="@drawable/edit_btn" />

    <TextView
        android:id="@+id/exerciseName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@id/editExerciseBtn"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="This is a Large text with fill width"
        android:textColor="#000000"
        android:textSize="18sp" />

</RelativeLayout>

code in activity :

@Override
public void onButtonClickListner(int position, String value) {
    Toast.makeText(create_workout.this, "Button click " + value,
            Toast.LENGTH_SHORT).show();

}

List<WorkoutExercise> workoutExercises = workout.getWorkoutExercises();
ListView exercisesList = (ListView) findViewById(R.id.exerciseListView);
WorkoutExerciseListAdapter listAdapter = new WorkoutExerciseListAdapter(this, R.layout.exercises_list_view_item, workoutExercises);
listAdapter.setCustomButtonListener(create_workout.this);
exercisesList.setAdapter(listAdapter);

Since WorkoutExerciseListAdapted has a lot of code I pasted it here : https://pastebin.com/FYNVVQnS

What I am trying to do is : If user clicks edit button I need to pass Exercise object to another activity for editing. If user clicks delete button I need to remove that item from List<> and update listView (with notifyDataSetChanged() ? )

I tried to look for answer through stackoverflow and google, but it either didn't work or my app started crashing. Few of tried examples: http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/ Adding an onclicklistener to listview (android) and others..

I would appreciate any advice. Thank you for your time.

Lukenzo
  • 1,317
  • 1
  • 8
  • 16

2 Answers2

1

1. Update your customButtonListener interface as below:

public interface customButtonListener {
    public void onEditButtonClickListner(int position, String value);
    public void onDeleteButtonClickListner(int position);
}

2. In adapters getView() method set click listener to edit and delete buttons:

@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {

    ............
    ...................

    // Edit
    viewHolder.editBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (customListner != null) {
                customListner.onEditButtonClickListner(position, getItem(position).getExerciseName());
            }
        }
    });

    // Delete
    viewHolder.deleteBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (customListner != null) {
                customListner.onDeleteButtonClickListner(position);
            }
        }
    });

    return convertView;
}

3. In your activity, add CustomButtonListener to your ListView:

A. Get item position from `onEditButtonClickListner()` and get `workoutExercise` object and pass it to another activity 
B. Get item position from `onDeleteButtonClickListner()` and delete item and upadte ListView.

Add below codes in your Activity:

    ..........
    .................

    listAdapter.setCustomButtonListener(create_workout.this);
    exercisesList.setAdapter(listAdapter);

    exercisesList.setCustomButtonListener(new WorkoutExerciseListAdapter.customButtonListener() {

        @Override
        public void onEditButtonClickListner(int position, String value) 
        {
            // Item
            WorkoutExercise workoutExercise = workoutExercises.get(position);

            // Do something with object workoutExercise 
        }

        @Override
        public void onDeleteButtonClickListner(int position) 
        {
            // Delete
            workoutExercises.remove(position);

            // Update ListView
            listAdapter.notifyDataSetChanged();
        }
    });

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • Thank you for your help. I combined both of your examples and everything is working like a charm! – Lukenzo May 27 '17 at 03:48
0

If you wan't to fire any events for every list-item on your list-view, Then you should implement it in your BaseAdapter class. On your WorkoutExerciseListAdapter, Inside the getView() methord define your event listner.

If you need to get the click event on your Activity, Then implement and use an interface, If so then at the WorkoutExerciseListAdapter

MyNotifier listner;

public interface MyNotifier
{
  void OnButtonClicked;
}

public void setMyNotificationListner(MyNotifier listner)
{
  this.listner=listner;
}

Then when the button clicks

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
     Button b=(Button)v.getViewByID(<id>);
     b.setOnClickListner(new OnClickListner...
     {
       listner.OnButtonClicked()
     }
    }

At the MainActivity

WorkoutExerciseListAdapter listAdapter = new WorkoutExerciseListAdapter(this, R.layout.exercises_list_view_item, workoutExercises);
listAdapter.setMyNotificationListner(new MyNotifier {..//You get click calls here..};)
Sangeeth Nandakumar
  • 1,362
  • 1
  • 12
  • 23