无法访问 ViewModel 列表的值<Integer>
Not able to access value of ViewModel's List<Integer>
我有以下设置:
- 单个 MainActivity 持有一个 MainFragment
- MainActivity 有存储整数列表的 ViewModel
- MainActivity 在此 ViewModel
中写入整数值
- 我想在 MainFragment
中打印这些值
- 我可以访问 ViewModel 实例,但无法从该实例获取列表值
主要活动:
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();
}
}
}
主要片段:
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()
方法时出现错误。
mViewModel.getListOfInt().observe(getActivity(), value -> {
formatList(value)
});
希望这对您有所帮助。
你传递的是对象而不是value.so应该是这样的
mViewModel.getListOfInt().observe(getActivity(), new Observer<List<Integer>>() {
@Override
public void onChanged(List<Integer> integers) {
formatList(integers);
}
});
实际上,我的项目正在使用 java 1.7
。 Passing Lambda to function is not supported in java 1.7
,所以当我在 Project Structure -> Module
中更改为 java 1.8
作为源和目标兼容性时,可以编译相同的代码。
注意:如果您使用的是 java 1.7
,@sasikumar 的回答适用于
我有以下设置:
- 单个 MainActivity 持有一个 MainFragment
- MainActivity 有存储整数列表的 ViewModel
- MainActivity 在此 ViewModel 中写入整数值
- 我想在 MainFragment 中打印这些值
- 我可以访问 ViewModel 实例,但无法从该实例获取列表值
主要活动:
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();
}
}
}
主要片段:
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()
方法时出现错误。
mViewModel.getListOfInt().observe(getActivity(), value -> {
formatList(value)
});
希望这对您有所帮助。
你传递的是对象而不是value.so应该是这样的
mViewModel.getListOfInt().observe(getActivity(), new Observer<List<Integer>>() {
@Override
public void onChanged(List<Integer> integers) {
formatList(integers);
}
});
实际上,我的项目正在使用 java 1.7
。 Passing Lambda to function is not supported in java 1.7
,所以当我在 Project Structure -> Module
中更改为 java 1.8
作为源和目标兼容性时,可以编译相同的代码。
注意:如果您使用的是 java 1.7
,@sasikumar 的回答适用于