androidandroid-fragmentsandroid-listviewandroid-arrayadaptercustom-arrayadapter

Handle button event of ListView of Fragment class?


I have used two Fragment in my code. I have to display a custom ListView in each fragment. But I would like to handle Button's onClickListener event which is in this ListView. I can't find online how to use these Button (ImageButton, actually) components in Fragment class. Here is my code:

My fragment class:

public class tabtodo extends Fragment implements View.OnClickListener {

public tabtodo() {}

private View view, view2;
private ListView lstTask;
private ImageButton btndelete;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_tabtodo, container, false);

    lstTask = (ListView) view.findViewById(R.id.listView);


    AlUpdater alUpdater = new AlUpdater(getContext());
    lstTask.setAdapter(alUpdater.getAdapterTodo());

    view2 = inflater.inflate(R.layout.row, container, false);
    btndelete = (ImageButton) view2.findViewById(R.id.btntest);
    btnTest.setOnClickListener(this);

    return mView;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btntest:
            Log.i("prova", "prova ");
            break;
        default:
            Log.i("prova", "def");
            break;
    }
}

alUpdater.getAdapterTodo() is:

public ArrayAdapter < String > getAdapterTodo() {
    ArrayList < String > taskList = (dG).getListTodo("todo");
    if (adptodo == null) {
        adptodo = new ArrayAdapter < > (context, R.layout.row, R.id.task_title, taskList);
        adptodo.notifyDataSetChanged();
    } else {
        adptodo.clear();
        adptodo.addAll(taskList);
        adptodo.notifyDataSetChanged();
    }
    return adptodo;
}

as you can see, I am using a custom layout for the arrayadapter. R.layout.fragment_tabtodo: is a fragment with just a listview:


    <?xml version="1.0" encoding="utf-8"?>`
    <FrameLayout 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"
        tools:context=".tabtodo"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:background="@color/white">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@color/white"
            android:dividerHeight="0dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true">
        </ListView>
    </FrameLayout>

and finally, the XML of the row of the adapter (**R.layout.row**):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingVertical="8dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/themeselector">

        <TextView
            android:id="@+id/task_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="10dp"
            android:text="TASK"
            android:textColor="@color/white"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btnGoToDone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <ImageButton
            android:id="@+id/btnGoToDone"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:backgroundTint="@android:color/transparent"
            android:onClick="switchTodoDone"
            android:src="@drawable/ic_done_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/btndelete"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@+id/btndelete"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="4dp"
            android:backgroundTint="@android:color/transparent"
            android:src="@drawable/ic_delete_icon"
            android:onClick="deleteTask"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • you need to create a custom adapter for you ListView. and handle the button click there. check this tutorial here

    I would recommend that you use RecyclerView instead of ListView since it enforces the ViewHolder pattern which improves the performance.