Android ROOM-如何观察LiveData的更改(每次设置日历时)并将LiveData列表结果发送到适配器?

问题描述 投票:0回答:1

我有一个Custom Calendar

为了创建这个,我有一个CalendarFragment打开CustomCalendarView

(扩展LinearLayout的类。

然后使用MyGridAdapter(扩展ArrayAdapter的类)来构造日历。

单击日历上的单元格时,您将转到一个新活动,可以在其中保存包含某些信息(以及单击的日历中单元格的日期)的日志。(此登录条目已保存到我的数据库中。)>

我想在所有存在日志条目的日历单元上显示圆圈。

为此,我有一个ChangeMonth()方法,其中将日历上所有可见日期的列表传递给ViewModel,然后调用setFilter()方法,该方法检查可见日期列表并返回所有日期该月在我的logEntry数据库中存在。

我如何从我的viewModel中观察结果列表:logEntryDatesFilteredByMonth,然后将其传递给我的适配器,以便我可以执行UI更改?

日历片段

public class CalendarFragment extends Fragment {

    CustomCalendarView customCalendarView;
    List<LogDates> dates = new ArrayList<>();
    LogEntriesViewModel logEntriesViewModel;
    MyGridAdapter myGridAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.calendar_activity_main, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        customCalendarView = (CustomCalendarView) getView().findViewById(R.id.custom_calendar_view);
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

        customCalendarView.SetUpCalendar();
        logEntriesViewModel.getDatesFilteredByMonth().observe(this, logDates -> myGridAdapter.setData(logDates));
        Log.i("PRESENT_DATES", String.valueOf(dates));
    }
}

CustomCalendarView


public class CustomCalendarView extends LinearLayout {

    private LogEntriesViewModel logEntriesViewModel;

    ImageButton NextButton, PreviousButton;
    TextView CurrentDate;
    GridView gridView;
    public static final int MAX_CALENDAR_DAYS = 42;
    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    Context context;
    MyGridAdapter myGridAdapter;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM yyyy", Locale.ENGLISH);
    SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.ENGLISH);
    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", Locale.ENGLISH);
    SimpleDateFormat eventDateFormat = new SimpleDateFormat(("dd-MM-yyyy"), Locale.ENGLISH);

    public static final String MY_PREFS_NAME = "MyPrefsFile";

    List<Date> dates = new ArrayList<>();

    List<LogDates> logsList = new ArrayList<>();

    List<String> datesFormattedList = new ArrayList<>();



    public CustomCalendarView(Context context) {
        super(context);
    }

    public CustomCalendarView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        InitializeLayout();
        SetUpCalendar();

        PreviousButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, -1);
                SetUpCalendar();
            }
        });

        NextButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, 1);
                SetUpCalendar();
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setCancelable(true);

                final String date = eventDateFormat.format(dates.get(position));

                Intent i = new Intent(getContext(), WorkoutButtonsActivity.class);
                i.putExtra(WorkoutButtonsActivity.EXTRA_DATE, date);
                getContext().startActivity(i);
            }
        });
    }

    public CustomCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void InitializeLayout() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.calendar_layout, this);
        NextButton = view.findViewById(R.id.nextBtn);
        PreviousButton = view.findViewById(R.id.previousBtn);
        CurrentDate = view.findViewById(R.id.current_Date);
        gridView = view.findViewById(R.id.gridview);
    }

    void SetUpCalendar() {

        datesFormattedList.clear();

        String currentDate = dateFormat.format(calendar.getTime());
        CurrentDate.setText(currentDate);
        dates.clear();
        Calendar monthCalendar = (Calendar) calendar.clone();
        monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
        int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) - 2;
        monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);

        while (dates.size() < MAX_CALENDAR_DAYS) {
            dates.add(monthCalendar.getTime());
            monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        /*THIS CONVERTS THE LIST OF ALL VISIBLE DATES TO A STRING */
        for (int i = 0; i< MAX_CALENDAR_DAYS; i++) {
            final String dateFormatted = eventDateFormat.format(dates.get(i));
            datesFormattedList.add(dateFormatted);
        }
        Log.i("Dates", String.valueOf(datesFormattedList));
        ChangeMonth();

        myGridAdapter = new MyGridAdapter(context, dates, calendar, logsList);
        gridView.setAdapter(myGridAdapter);
    }

    public void ChangeMonth() {
        logEntriesViewModel = ViewModelProviders.of((FragmentActivity) context).get(LogEntriesViewModel.class);
        logEntriesViewModel.setFilter(datesFormattedList);

    }
}

LogEntries ViewModel

public class LogEntriesViewModel extends AndroidViewModel {

    private LogEntriesRepository repository;
   // private LiveData<List<Log_Entries>> allLogEntries;
    private LiveData<List<Log_Entries>> allWorkoutLogEntries;



    private LiveData<List<LogDates>> logEntryDatesFilteredByMonth;
    private LiveData<List<LogDates>> allLogEntryDates;
    private MutableLiveData<List<String>> filterLogPresentDates = new MutableLiveData <List<String>>();



    public LogEntriesViewModel(@NonNull Application application) {
        super(application);
        repository = new LogEntriesRepository(application);
        allLogEntryDates = repository.getAllLogEntryDates();
        logEntryDatesFilteredByMonth = Transformations.switchMap(filterLogPresentDates, c -> repository.getAllDateLogEntries(c));
    }

    public LiveData<List<LogDates>> getDatesFilteredByMonth() { return logEntryDatesFilteredByMonth; }

    public void setFilter(List<String> currentMonthDates) { filterLogPresentDates.setValue(currentMonthDates); }
    LiveData<List<LogDates>> getAllLogEntryDates() { return allLogEntryDates; }


    public void insert(Log_Entries log_entries){
        repository.insert(log_entries);
    }
    public void update(Log_Entries log_entries){
        repository.update(log_entries);
    }
    public void delete(Log_Entries log_entries ) {
        repository.delete(log_entries);
    }

    public LiveData<List<Log_Entries>> getAllWorkoutLogEntries(int junctionID, String date){
        allWorkoutLogEntries = repository.getAllWorkoutLogEntries(junctionID, date);
        return allWorkoutLogEntries;
    }
}

LogEntries DAO

@Dao
public interface Log_Entries_Dao {

    @Insert
    void insert(Log_Entries logEntry);

    @Update
    void update(Log_Entries logEntry);

    @Delete
    void delete(Log_Entries logEntry);


    @Query("SELECT * FROM log_entries_table")
    LiveData<List<Log_Entries>> getAllFromLogEntries();


    @Query("SELECT * FROM log_entries_table WHERE log_entries_table.junction_id = :junctionID AND log_entries_table.date = :date " )
    LiveData<List<Log_Entries>> getAllFromWorkoutLogEntries(int junctionID, String date);

    @Query("SELECT * FROM log_entries_table WHERE log_entries_table.date = :date " )
    LiveData<List<Log_Entries>> getAllDatesWithLogEntries(String date);

    @Query("SELECT date FROM log_entries_table WHERE log_entries_table.date = :date " )
    LiveData<List<LogDates>> getAllDatesWithLogs(List<String> date);

    @Query("SELECT date FROM log_entries_table " )
    LiveData<List<LogDates>> getAllLogEntryDates();

}

我有一个自定义日历。为此,我有一个CalendarFragment,它可以打开CustomCalendarView(扩展LinearLayout的类)。然后使用MyGridAdapter(扩展了...的类)

java android android-room dao android-livedata
1个回答
1
投票

您应该实现这种观察方法:

© www.soinside.com 2019 - 2024. All rights reserved.