iT邦幫忙

0

如何在其他類別中使用LiveData?

  • 分享至 

  • xImage

希望使用者能在選出日期後,能把日期資料拋到LiveData中,以便其它Fragment使用,創建一個類別 DataPickerFragment。但是我查閱一下liveData使用,似乎僅限用於ViewModel中,以下是我的程式碼~ 煩請各路前輩指點,如何直觀的將資料送到LiveData中保存~

Class for DatePickerFragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    private static String calStrings="AA";

    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(),this, year, month, day);
    }

    public interface DatePickerListener{
        void onDateSet(DatePicker datePicker, int year, int month, int day);
    }

    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        populateSetDate(year, month, dayOfMonth);
    }

    public void populateSetDate(int year, int month, int day){
        TextView textView = getActivity().findViewById(R.id.tvDate);
        String s = String.valueOf(year)+"/"+String.valueOf(month+1)+"/"+String.valueOf(day);
        textView.setText(s);
        this.calStrings = s;
    }

    public String calStr(){
        System.out.println(calStrings);
        return calStrings;
    }
}

and this is my Fragment A

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LiveData liveData;
        liveData = new ViewModelProvider(this, new SavedStateViewModelFactory(requireActivity().getApplication(), this)).get(LiveData.class);

        FragmentABinding binding;
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_a, container, false);
        binding.setData(liveData);
        binding.setLifecycleOwner(requireActivity());

        // Choose Date
        binding.btnDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePickerFragment = new DatePickerFragment();
                datePickerFragment.setCancelable(false);
                FragmentManager fm = getActivity().getSupportFragmentManager();
                datePickerFragment.show(fm,"date Picker");
            }
        });
        return binding.getRoot();
    }

This is LiveData

public class LiveData extends AndroidViewModel {
    SavedStateHandle handle;
    private static final String KEY_CALENDAR= "KEY_CALENDAR"; //calendar
    private static final String SAVE_SHP = "save_sharedper";

    public LiveData(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        if (!handle.contains(KEY_NUM1)){
            SharedPreferences shp = getApplication().getSharedPreferences(SAVE_SHP, Context.MODE_PRIVATE);
            handle.set(KEY_NUM1,shp.getInt(KEY_NUM1, 0));
            handle.set(KEY_CALENDAR,shp.getString(KEY_CALENDAR, null));
        }
        this.handle = handle;
    }
    private MutableLiveData<Integer> number;
    public MutableLiveData<Integer> getNumber(){
        return handle.getLiveData(KEY_NUM1);
    }
    private MutableLiveData<String> calendar;
    public MutableLiveData<String> getCalendar(){
        return handle.getLiveData(KEY_CALENDAR);
    }
    void save(){ // press navigation and then will do this function
        SharedPreferences shp = getApplication().getSharedPreferences(SAVE_SHP, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =shp.edit();
        editor.putInt(KEY_NUM1, getNumber().getValue());
        editor.putString(KEY_CALENDAR, getCalendar().getValue());
        editor.putString(KEY_TIME, getTime().getValue());
        editor.apply();
    }
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-08-01 21:35:04

參考這篇
再學不會你砍我

去砍作者
不是砍我
/images/emoticon/emoticon21.gif

我要發表回答

立即登入回答