ViewModel。设定资料

时间:2019-02-18 16:15:52

标签: android viewmodel

我有两个片段。在第一张牌列表中。当您单击卡时,将打开第二个片段,并提供完整说明。我放入了适配器中安装的bundle标签。在第二个片段中,我得到了它,我需要按标签设置数据。

在onChanged方法中,我编写了model.setVerse((List)data);但什么也没发生。

起初我尝试做tvTitle.setText(data.getTitle),但也没有任何反应。

viewModel

 public class DataViewModel extends AndroidViewModel {

LiveData<Data> currentVerse;

public DataViewModel(@NonNull Application application, final int verseId) {
    super(application);
    int verseId1 = verseId;
    DataRepository repository = new DataRepository(application);
    currentVerse = repository.getById(verseId);

}

public LiveData<Data> getById() {
    return currentVerse;
}
public void setVerse(List<Data> data) {
    this.currentVerse= (LiveData<Data>) data;

}


public static class ModelFactory extends ViewModelProvider.NewInstanceFactory {

    @NonNull
    private final Application application;
    private final int verseId;
    private final DataRepository repository;

    public ModelFactory(@NonNull Application application, int id) {
        super();
        this.application = application;
        this.verseId = id;
        repository = new DataRepository(application);
    }


    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass == DataViewModel.class) {
            return (T) new DataViewModel(application, verseId);
        }
        return null;
    }
}

}

片段

 public class DataFragment extends Fragment {
private int verseId;
private static final String KEY_VERSE_ID = "KEY_VERSE_ID";
public TextView tvTitle;

public DataFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootViewRead = inflater.inflate(R.layout.fragment_data, container, 
false);
    Toolbar toolbar = rootViewRead.findViewById(R.id.toolbar);
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    if (activity != null) {
        activity.setSupportActionBar(toolbar);
    }

    setHasOptionsMenu(true);

    tvTitle = (TextView) rootViewRead.findViewById(R.id.text);

    DataViewModel.ModelFactory factory = new DataViewModel.ModelFactory(
            getActivity().getApplication(), 
 getArguments().getInt(KEY_VERSE_ID));

    final DataViewModel model = ViewModelProviders.of(this, factory)
            .get(DataViewModel.class);
    model.getById().observe(this, new Observer<Data>() {
        @Override
        public void onChanged(Data data) {
            model.setVerse((List<Data>) data);


        }
    });

    return rootViewRead;
}

public static DataFragment forData(int verseId) {
    DataFragment fragment = new DataFragment();
    Bundle args = new Bundle();
    args.putInt(KEY_VERSE_ID, verseId);
    fragment.setArguments(args);
    return fragment;
}

如有必要,我将添加dao和存储库类

1 个答案:

答案 0 :(得分:0)

以下是您需要修改以设置LiveData值的部分:

LiveData<<List>Data>> currentVerse;

public void setVerse(List<Data> data) {
    this.currentVerse.setValue(data);

}

有关更多详细信息,click here