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.