ARTICLE AD BOX
I'm wondering how can I make it so that in a recycler view, the input from the editTexts and the input from the checkboxes are displayed into that recycler view.
I mainly would like to see if the checkbox labels from the user chosen checkboxes can be displayed plus the input from the editTexts, but I'm confused on how to really show the labels. What would be a way to make this happen?
I've spent a few days on this same issue and have gotten as far as to try and get the habits to actually show in the recycler view. However, I have noted that the recycler view never displays anything at all even when I've added input from both the editTexts and checkboxes.
I'm also working with two activities: one for the input (Add_Habit_Activity) and one for display (Home_Page_Activity) passing data from one activity to another. The list holds all the necessary items, but I'm still unsure as to why it still does not display those items in the recycler view.
Here is the code for Home_page_Activity.java file:
public class Home_Page_Activity extends AppCompatActivity { public Button Add_Habit_button; public RecyclerView Habit_listRV; public ArrayList<Habit_Data_Model> habit_list; public Habit_Adapter habitAdapter; @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_home_page); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); toolbar = findViewById(R.id.Toolbar_for_Home_page); setSupportActionBar(toolbar); Add_Habit_button = findViewById(R.id.Home_Page_Add_a_Habit_button); Habit_listRV = findViewById(R.id.recyclerview_Home_Page_Habit_list); Add_Habit_button.setOnClickListener(view -> { Intent intent = new Intent(Home_Page_Activity.this, Add_Habit_Activity.class); startActivity(intent); }); habit_list = new ArrayList<>(); // Null exception occurred here earlier so i did this habitAdapter = new Habit_Adapter(habit_list); // Null exception also occurred around here so i also added this line here habit_list = (ArrayList<Habit_Data_Model>) getIntent().getSerializableExtra("List"); Habit_listRV.setLayoutManager(new LinearLayoutManager(Home_Page_Activity.this)); Habit_listRV.setAdapter(habitAdapter); habitAdapter.notifyDataSetChanged(); //Log.d("HOME", "Received habits: " + habit_list.size()); } }Here is the code for the Add_activity_Activity.java file:
public class Add_Habit_Activity extends AppCompatActivity { public EditText Habit_name_edt; public EditText Habit_desc_edt; public EditText Habit_notes_edt; public CheckBox WHabit; public CheckBox DHabit; public CheckBox YHabit; public CheckBox MHabit; public Button Add_Habit_btn; public ArrayList<Habit_Data_Model> habit_list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_add_habit); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); Habit_name_edt = findViewById(R.id.AH_Habit_name); Habit_desc_edt = findViewById(R.id.AH_Habit_description); Habit_notes_edt = findViewById(R.id.AH_Habit_notes); WHabit = findViewById(R.id.AH_Weekly_habit); DHabit = findViewById(R.id.AH_Daily_habit); YHabit = findViewById(R.id.AH_Yearly_habit); MHabit = findViewById(R.id.AH_Monthly_habit); Add_Habit_btn = findViewById(R.id.AH_Add_Habit_Button); habit_list = new ArrayList<>(); Add_Habit_btn.setOnClickListener(view -> { Habit_Data_Model habitDataModel = new Habit_Data_Model(); habitDataModel.setHabit_name(Habit_name_edt.getText().toString()); habitDataModel.setHabit_desc(Habit_desc_edt.getText().toString()); habitDataModel.setHabit_notes(Habit_notes_edt.getText().toString()); habitDataModel.setcheckbox_value_daily(DHabit.isChecked()); habitDataModel.setcheckbox_value_monthly(MHabit.isChecked()); habitDataModel.setcheckbox_value_weekly(WHabit.isChecked()); habitDataModel.setcheckbox_value_yearly(YHabit.isChecked()); habit_list.add(habitDataModel); Log.d("ADDHB", "Habit list size = " + habit_list.size()); Log.d("ADDHB", "List contents:" + habit_list); Intent intent = new Intent(Add_Habit_Activity.this, Home_Page_Activity.class); intent.putExtra("List", (Serializable) habit_list); startActivity(intent); }); } }Here is the code used in the Habit_Adapter.java file:
public class Habit_Adapter extends RecyclerView.Adapter<Habit_Adapter.Habit_ViewHolder> { public List<Habit_Data_Model> habit_list; public Habit_Adapter(List<Habit_Data_Model> habit_list_){ this.habit_list = habit_list_; } @NonNull @Override public Habit_ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.habit_layout, parent, false); return new Habit_ViewHolder(view); } @Override public void onBindViewHolder(@NonNull Habit_ViewHolder holder, int position) { Habit_Data_Model habitDataModel = habit_list.get(position); holder.habit_name.setText(habitDataModel.getHabit_name()); holder.habit_desc.setText(habitDataModel.getHabit_desc()); holder.habit_notes.setText(habitDataModel.getHabit_notes()); holder.Weekly.setChecked(habitDataModel.ischeckbox_value_weekly()); holder.Daily.setChecked(habitDataModel.ischeckbox_value_daily()); holder.Monthly.setChecked(habitDataModel.ischeckbox_value_monthly()); holder.Yearly.setChecked(habitDataModel.ischeckbox_value_yearly()); } @Override public int getItemCount() { return habit_list.size(); } public static class Habit_ViewHolder extends RecyclerView.ViewHolder { TextView habit_name, habit_desc, habit_notes; CheckBox Weekly, Monthly, Daily, Yearly; public Habit_ViewHolder(@NonNull View itemView) { super(itemView); habit_name = itemView.findViewById(R.id.HL_Habit_name); habit_desc = itemView.findViewById(R.id.HL_Habit_desc); habit_notes = itemView.findViewById(R.id.HL_Habit_notes); } } }Here is also the data model class code (Habit_Data_Model.java) file:
public class Habit_Data_Model implements Serializable { public String habit_name; public String habit_desc; public String habit_notes; public boolean checkbox_value_weekly; public boolean checkbox_value_daily; public boolean checkbox_value_monthly; public boolean checkbox_value_yearly; public Habit_Data_Model(boolean checkbox_value_yearly, boolean checkbox_value_monthly, boolean checkbox_value_daily, boolean checkbox_value_weekly, String habit_notes, String habit_desc, String habit_name) { this.checkbox_value_yearly = checkbox_value_yearly; this.checkbox_value_monthly = checkbox_value_monthly; this.checkbox_value_daily = checkbox_value_daily; this.checkbox_value_weekly = checkbox_value_weekly; this.habit_notes = habit_notes; this.habit_desc = habit_desc; this.habit_name = habit_name; } public Habit_Data_Model(){} public String getHabit_name() { return habit_name; } public String getHabit_desc() { return habit_desc; } public String getHabit_notes() { return habit_notes; } public boolean ischeckbox_value_weekly() { return checkbox_value_weekly; } public boolean ischeckbox_value_daily() { return checkbox_value_daily; } public boolean ischeckbox_value_monthly() { return checkbox_value_monthly; } public boolean ischeckbox_value_yearly() { return checkbox_value_yearly; } public void setHabit_name(String habit_name) { this.habit_name = habit_name; } public void setHabit_desc(String habit_desc) { this.habit_desc = habit_desc; } public void setHabit_notes(String habit_notes) { this.habit_notes = habit_notes; } public void setcheckbox_value_weekly(boolean checkbox_value_weekly) { this.checkbox_value_weekly = checkbox_value_weekly; } public void setcheckbox_value_daily(boolean checkbox_value_daily) { this.checkbox_value_daily = checkbox_value_daily; } public void setcheckbox_value_monthly(boolean checkbox_value_monthly) { this.checkbox_value_monthly = checkbox_value_monthly; } public void setcheckbox_value_yearly(boolean checkbox_value_yearly) { this.checkbox_value_yearly = checkbox_value_yearly; } @NonNull @Override public String toString() { return "Habit_Data_Model{" + "habit_name='" + habit_name + '\'' + ", habit_desc='" + habit_desc + '\'' + ", habit_notes='" + habit_notes + '\'' + ", checkbox_value_weekly=" + checkbox_value_weekly + ", checkbox_value_daily=" + checkbox_value_daily + ", checkbox_value_monthly=" + checkbox_value_monthly + ", checkbox_value_yearly=" + checkbox_value_yearly + '}'; }// found out how to add this method so that way I could truly see if there was anything being added in the arraylist. }Below is the code for the activity_home_page.xml file:
<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Home_Page_Activity"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Toolbar_for_Home_page"> </androidx.appcompat.widget.Toolbar> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerview_Home_Page_Habit_list" android:layout_width="match_parent" android:layout_height="200dp"> </androidx.recyclerview.widget.RecyclerView> <Button android:id="@+id/Home_Page_Add_a_Habit_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/AH_Add_a_habit_button_msg" android:layout_marginStart="130dp" android:layout_marginTop="330dp"> </Button> </LinearLayout>Here is the code for the activity_add_habit.xml file:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Add_Habit_Activity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="150dp" android:layout_marginTop="50dp" android:text="@string/AH_Add_Habit_Title"> </TextView> <EditText android:id="@+id/AH_Habit_name" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="145dp" android:hint="@string/AH_hint_msg_Habit_Name" android:inputType="text"> </EditText> <EditText android:id="@+id/AH_Habit_description" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="200dp" android:minLines="10" android:hint="@string/AH_hint_msg_Habit_Description" android:inputType="text"> </EditText> <EditText android:id="@+id/AH_Habit_notes" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="300dp" android:minLines="4" android:hint="@string/AH_hint_msg_Habit_Notes" android:inputType="text"> </EditText> <CheckBox android:id="@+id/AH_Weekly_habit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/AH_Weekly_checkbtn_msg" android:layout_marginStart="130dp" android:layout_marginTop="550dp"> </CheckBox> <CheckBox android:id="@+id/AH_Monthly_habit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/AH_Monthly_checkbtn_msg" android:layout_marginStart="130dp" android:layout_marginTop="600dp"> </CheckBox> <CheckBox android:id="@+id/AH_Yearly_habit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="210dp" android:layout_marginTop="550dp" android:text="@string/AH_Yearly_checkbtn_msg"> </CheckBox> <CheckBox android:id="@+id/AH_Daily_habit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="220dp" android:layout_marginTop="600dp" android:text="@string/AH_Daily_checkbtn_msg"> </CheckBox> <Button android:id="@+id/AH_Add_Habit_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="150dp" android:layout_marginTop="660dp" android:text="@string/AH_Add_a_habit_button_msg"> </Button> </RelativeLayout>Below is the code for the layout (habit_layout.xml) that I'm using for the recycler view:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/HL_Habit_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginEnd="100dp" android:layout_marginStart="40dp"> </TextView> <TextView android:id="@+id/HL_Habit_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:layout_marginEnd="100dp" android:layout_marginStart="40dp"> </TextView> <TextView android:id="@+id/HL_Habit_notes" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="110dp" android:layout_marginEnd="100dp" android:layout_marginStart="40dp"> </TextView> </LinearLayout>