HomeFragmentでダミーSchedule表示
1 parent 62dc604 commit be9eb1351fa379ce5de4613a91539e40ff4ffb37
h-yamamoto authored on 2 Jul
Showing 2 changed files
View
106
app/src/main/java/com/example/citrusclient/views/HomeFragment.java
 
import com.example.citrusclient.Citrus;
import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;
import com.example.citrusclient.models.Schedule;
import com.example.citrusclient.models.Todo;
import com.example.citrusclient.viewmodels.ScheduleViewModel;
import com.example.citrusclient.viewmodels.TodosViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
 
import java.util.ArrayList;
mParam2 = getArguments().getString(ARG_PARAM2);
}
 
todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class);
scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class);
 
}
 
 
}
 
private List<Todo> todoList;
TodosViewModel todosViewModel;
private List<Schedule> scheduleList;
ScheduleViewModel scheduleViewModel;
 
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
 
String token = citrus.getToken();
String accountId = citrus.getAccountId();
 
todoList = new ArrayList<>();
todoList.add(new Todo("a", true, 2024, 06, 27, 1));
 
RecyclerView recyclerView = view.findViewById(R.id.my_todos_list);
recyclerView.setHasFixedSize(true);
todoList.add(new Todo("a", true, 2024, 7, 2, 1));
scheduleList = new ArrayList<>();
scheduleList.add(new Schedule("a", "1", "1", 1, 1));
 
 
RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list);
todoRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(layoutManager);
todoRecyclerView.setLayoutManager(layoutManager);
MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList);
recyclerView.setAdapter(todoAdapter);
todoRecyclerView.setAdapter(todoAdapter);
 
RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list);
scheduleRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext());
scheduleRecyclerView.setLayoutManager(scheduleLayoutManager);
MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList);
scheduleRecyclerView.setAdapter(scheduleAdapter);
 
todosViewModel.getTodosByDayLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Todo>>() {
@Override
public void onChanged(HashMap<Integer, Todo> idTodoHashMap) {
}
}
});
 
todosViewModel.loadTodosByDay("bird", 1, 2024, 6, 2, "xyz");
scheduleViewModel.getSchedulesByDay().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Schedule>>() {
@Override
public void onChanged(HashMap<Integer, Schedule> idScheduleHashMap) {
if(idScheduleHashMap != null) {
scheduleList = new ArrayList<>(idScheduleHashMap.values());
scheduleAdapter.setSchedules(scheduleList);
}
}
});
scheduleViewModel.updateSchedulesByDay("bird", 2024, 5, 28, "xyz");
todosViewModel.loadTodosByDay("bird", 1, 2024, 7, 2, "xyz");
 
}
}
 
class MyTodoshelfAdapter extends RecyclerView.Adapter<MyTodoshelfAdapter.MyTodoViewHolder> {
 
private List<Todo> todoList;
 
MyTodoshelfAdapter(List<Todo> todo) {
this.todoList = todo;
MyTodoshelfAdapter(List<Todo> todos) {
this.todoList = todos;
}
 
public void setTodos(List<Todo> todos) {
todoList = todos;
}
}
}
 
class MyScheduleshelfAdapter extends RecyclerView.Adapter<MyScheduleshelfAdapter.MyScheduleViewHolder> {
 
private List<Schedule> scheduleList;
 
MyScheduleshelfAdapter(List<Schedule> schedules) {
this.scheduleList = schedules;
}
 
public void setSchedules(List<Schedule> schedules) {
scheduleList = schedules;
notifyDataSetChanged();
}
 
@NonNull
@Override
public MyScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_schedule, parent, false);
return new MyScheduleViewHolder(view);
}
 
@Override
public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) {
Schedule scheduleData = this.scheduleList.get(position);
holder.scheduleButton.setText(scheduleData.getTitle());
}
 
@Override
public int getItemCount() {
return scheduleList.size();
}
 
static class MyScheduleViewHolder extends RecyclerView.ViewHolder {
Button scheduleButton;
public MyScheduleViewHolder(@NonNull View itemView) {
super(itemView);
scheduleButton = itemView.findViewById(R.id.schedule_button);
}
}
}
View
20
app/src/main/res/layout/a_schedule.xml 0 → 100644
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
 
<Button
android:id="@+id/schedule_button"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="#000000"
app:cornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>