Search code examples
javaandroidandroid-recyclerviewandroid-adapter

What superclass should I choose for my Adapter Class?


I am making a time table app. In that app, each row is a CardView that has three columns - index, task's name, total hours spent on that task. index is a TextView, task name and total hours spent are EditView. I want to transfer each row's data through an adapter. I am unsure which super class to pick for the Adapter class based on my widgets I have chosen.

This is the main screen that shows all the rows of the time table -

    package com.example.timetable;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class TimeTable extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_table);

        List<cellProperties> cells = new ArrayList<>();


        //adapter should come here

        findViewById(R.id.add).setOnClickListener(view -> {

        });



    }
}

Here is the xml of file of the layout of the main screen -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".TimeTable"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_margin="10dp">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/add"
        android:layout_width="38dp"
        android:layout_height="wrap_content"
        android:text="+"
        android:layout_gravity="center_horizontal"
        >

    </Button>
</LinearLayout>

Here is a sample of each row (I will add this row into my main screen)-

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    app:contentPadding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/index"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_weight="0.125"
            android:text="1."
            android:textAlignment="center">

        </TextView>

        <EditText
            android:id="@+id/taskName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_weight="0.625"
            android:hint="Enter Task's name"
            android:inputType="text">

        </EditText>

        <EditText
            android:id="@+id/totalHours"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:layout_weight="0.25"
            android:hint="Total Time"
            android:inputType="time">

        </EditText>
    </LinearLayout>
</androidx.cardview.widget.CardView>

Solution

  • Your Adapter class should almost always extend ListAdapter. Here's a very basic example.