Solution 1 :
Your Adapter class should almost always extend ListAdapter
. Here’s a very basic example.
Problem :
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>
Comments
Comment posted by Community
Please provide enough code so others can better understand or reproduce the problem.
Comment posted by Jezues
Hey! sorry for a late response, but here is the code as needed. Edit – I want to know which adapter class I should use.