无法访问ViewModel的List <Integer>的值

时间:2019-11-18 07:14:17

标签: android viewmodel android-jetpack android-viewmodel

我具有以下设置:

  • 持有 MainFragment
  • 的单个 MainActivity
  • MainActivity 具有用于存储整数列表的ViewModel
  • MainActivity 在此ViewModel中写入整数值
  • 我想在 MainFragment
  • 中打印这些值
  • 我能够访问ViewModel实例,但无法从该实例获取List值

MainActivity

public class MainActivity extends AppCompatActivity {

private MainViewModel mViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

    // Adding integer values in ViewModel
    mViewModel.selectInteger(1);
    mViewModel.selectInteger(10);
    mViewModel.selectInteger(111);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, MainFragment.newInstance())
            .commitNow();
    }
}

}

MainFragment

public class MainFragment extends Fragment {

private MainViewModel mViewModel;

private TextView tv;

public static MainFragment newInstance() {
    return new MainFragment();
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {

    View rootView =  inflater.inflate(R.layout.main_fragment, container, false);
    tv = rootView.findViewById(R.id.myTextView);
    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mViewModel = ViewModelProviders.of(getActivity()).get(MainViewModel.class);

    // I am getting error in below line, Not able to pass List of integer to formatList function
    mViewModel.getListOfInt().observe(getActivity(), {value -> {formatList(value)}});
}

private String formatList(List<Integer> list) {
    String returnValue = "";
    for(Integer a : list) {
        returnValue = returnValue + a;
    }
    return returnValue;
}

}

MainViewModel

public class MainViewModel extends ViewModel {
// TODO: Implement the ViewModel
private final MutableLiveData<List<Integer>> listOfInt = new MutableLiveData<List<Integer>>();


public MutableLiveData<List<Integer>> getListOfInt() {
       return listOfInt;
   }

   public void selectInteger(Integer a) {
       List<Integer> current = listOfInt.getValue();
       current.add(a);
       listOfInt.setValue(current);
   }
}

将整数列表传递给formatList()方法时出现错误。

enter image description here

3 个答案:

答案 0 :(得分:2)

您传递的是对象而不是值。所以应该像这样

mViewModel.getListOfInt().observe(getActivity(), new Observer<List<Integer>>() {

        @Override
        public void onChanged(List<Integer> integers) {
            formatList(integers);
        }
    });

答案 1 :(得分:1)

 mViewModel.getListOfInt().observe(getActivity(), value -> {

     formatList(value)
 });

希望这会有所帮助。

答案 2 :(得分:0)

实际上,我的项目正在使用java 1.7Passing Lambda to function is not supported in java 1.7,所以当我在java 1.8中将Project Structure -> Module更改为Source和Target时,可以编译相同的代码。

注意:如果您使用的是java 1.7,则@sasikumar的答案适用于此