Основная проблема: Мне интересно, как я могу сделать так, чтобы в recyclerView входные данные из editTexts и входные данные из флажков отображались в этом recyclerView.
(В основном я хотел бы посмотреть, можно ли отображать метки флажков из выбранных пользователем флажков плюс ввод из editTexts, но я не понимаю, как на самом деле отображать метки, какой способ это сделать?)
Я потратил несколько дней на эту же проблему и дошел до того, что попытался заставить привычки действительно отображаться в recyclerView. Однако я заметил, что recyclerView никогда вообще ничего не отображает, даже если я добавил входные данные как из editTexts, так и из флажков.
Я также работаю с двумя действиями: 1 для ввода (Add_Habit_Activity) и 1 для отображения (Home_Page_Activity), передавая данные от одного действия к другому, список содержит все необходимые элементы, но Я до сих пор не понимаю, почему эти элементы до сих пор не отображаются в recyclerView.
Вот код файла Home_page_Activity.java
Код: Выделить всё
`public class Home_Page_Activity extends AppCompatActivity {
public Button Add_Habit_button;
public RecyclerView Habit_listRV;
public ArrayList 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) 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());
}
}`
Код: Выделить всё
`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_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);
});
}
}`
Код: Выделить всё
`public class Habit_Adapter extends RecyclerView.Adapter {
public List habit_list;
public Habit_Adapter(List 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);
}
}
}`
Код: Выделить всё
`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.
}`
Код: Выделить всё
`
`
Код: Выделить всё
`
`
Код: Выделить всё
`
`
Любая помощь будет принята с благодарностью!
Подробнее здесь: https://stackoverflow.com/questions/798 ... els-into-a
Мобильная версия