Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当从第一个片段listview适配器类更新ViewModel列表时,不会在第二个片段中调用ViewModel_Android_Android Fragments_Mvvm - Fatal编程技术网

Android 当从第一个片段listview适配器类更新ViewModel列表时,不会在第二个片段中调用ViewModel

Android 当从第一个片段listview适配器类更新ViewModel列表时,不会在第二个片段中调用ViewModel,android,android-fragments,mvvm,Android,Android Fragments,Mvvm,基本上,我有一个viewModel,应该在第二个片段中使用它来观察列表。 为此,我制作了如下的viewModel public class DetailsViewModel extends ViewModel { MutableLiveData<List<ItemModal>> childList ; private DataSnapshot dataSnapshot; List<ItemModal> tempList; public DetailsView

基本上,我有一个viewModel,应该在第二个片段中使用它来观察列表。 为此,我制作了如下的viewModel

public class DetailsViewModel extends ViewModel {
MutableLiveData<List<ItemModal>> childList ;
private DataSnapshot dataSnapshot;
List<ItemModal> tempList;

public DetailsViewModel() {
    childList = new MutableLiveData<>();
}


public void setList(DataSnapshot dataSnapshot){
    this.dataSnapshot = dataSnapshot;
    convertList();
}

public void convertList(){

    try {
        tempList = new ArrayList<>();
        childList = new MutableLiveData<>();
        for(DataSnapshot dataSnapshot:dataSnapshot.getChildren()){
            ItemModal itemModal = new ItemModal();
            itemModal.setItemType(this.dataSnapshot.getKey());
            itemModal.setItemContent(dataSnapshot.getValue().toString());
            itemModal.setItemName(dataSnapshot.getKey());
            tempList.add(itemModal);
        }
        childList.setValue(tempList);
    }
    catch (Exception c){
        c.printStackTrace();
    }
}


public MutableLiveData<List<ItemModal>> getList(){
    return this.childList;
}

}
因此,问题在于,在与第一个片段相关的项目单击时,从列表适配器成功更新viewModel列表。但是在调用viewModel以使用列表的第二个片段中没有调用viewModel

任何帮助都会得到大力支持。
提前感谢。

这是因为您正在第二个片段中重新实例化viewmodel。它与第一个片段中的实例不同。如果您想让它像这样工作,请使用SharedViewModel。这是因为您正在第二个片段中重新实例化viewmodel。它与第一个片段中的实例不同。如果您想让它像这样工作,请使用SharedViewModel。
 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.details_fragment, container, false);
    recyclerViewDetails = view.findViewById(R.id.recycleDetails);


    mViewModel = ViewModelProviders.of(this).get(DetailsViewModel.class);
    mViewModel.getList().observe(this,  new Observer<List<ItemModal>>() {
        @Override
        public void onChanged(List<ItemModal> itemModals) {

                AdapterDetailsRecycle adapterDetailsRecycle = new AdapterDetailsRecycle(getContext(), itemModals);
                recyclerViewDetails.setAdapter(adapterDetailsRecycle);

        }
    });
    return view;
}
 @Override
public View getView(int position, final View convertView, final ViewGroup parent) {
    view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_main,parent,false);
    TextView textView = view.findViewById(R.id.txtCatName);
    ItemModal itemModal = itemModalList.get(position);
    textView.setText(itemModal.getItemType());
    detailsViewModel = ViewModelProviders.of((FragmentActivity)context).get(DetailsViewModel.class);
    final DataSnapshot dataSnapshot = itemModal.getChilds();
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            detailsViewModel.setList(dataSnapshot);
            ((MainActivity)context).changeFragment(new DetailsFragment());

        }
    });

    return view;
}