我可以在不使用适配器或回收器视图的情况下获取数据并将数据插入数据库吗?这是我的代码。
包 com.example.personalapp;
public class PersonalFragment extends Fragment {
private PersonalViewModel personalVM;
private TextView tvFullName, tvIdCard, tvPhoneNumber;
public PersonalFragment(){
// require a empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_personal, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @NonNull Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvFullName = view.findViewById(R.id.tvFullName);
tvIdCard = view.findViewById(R.id.tvIdCard);
tvPhoneNumber = view.findViewById(R.id.tvPhoneNumber);
personalVM = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication())).get(PersonalViewModel.class);
PersonalEntity pe = new PersonalEntity(0, "Testing User", "1234567891011", "08215326211");
personalVM.insertUserData(pe);
personalVM.getUserData().observe(getViewLifecycleOwner(), new Observer<PersonalEntity>() {
@Override
public void onChanged(PersonalEntity personal) {
if(personal != null) {
tvFullName.setText(personal.getFull_name());
tvIdCard.setText(personal.getId_card());
tvPhoneNumber.setText(personal.getPhone_number());
} else {
tvFullName.setText("?");
tvIdCard.setText("?");
tvPhoneNumber.setText("?");
}
}
});
}
}
但是,一旦我运行应用程序,我就无法插入数据并将其显示在我的应用程序上。但在另一页上,我用适配器和回收器视图显示了我的数据库,并且工作正常。