有时Room数据库LiveData返回空值

时间:2019-09-03 06:01:39

标签: java android

当我从会议室数据库获取数据时,有时在编辑表单期间从会议室数据库获取空数据

我正在尝试确定数据库中是否存在具有某些Items的项目,但是即使我知道它在数据库中,我也一直为空。 我一直在查看“房间”查询示例,但无法解决该问题。当我尝试打印查询日志时,除了IndexOutOfBounds异常外,我在运行代码时没有看到任何错误。

存储库类

public class SectorRepository

 {
private String DB_NAME = "db_foodgospel";

 private SectorDatabase sectorDatabase;

    public SectorRepository(Context context) 
{

        sectorDatabase = Room.databaseBuilder(context, SectorDatabase.class, DB_NAME)
                .fallbackToDestructiveMigration().allowMainThreadQueries().build();

    } 

public LiveData<List<State>> getAllState()
 {
        return sectorDatabase.daoAccess().fetchAllStateData();
    }

  public LiveData<List<District>> getAllDistrict(int state_id) {
 return sectorDatabase.daoAccess().fetchDistrictDataByStateId(state_id);
    }

 public LiveData<List<City>> getAllCityData(int state_id)
 {
     return sectorDatabase.daoAccess().fetchAllCityData(state_id);
    }

片段类

public class AddFarmerFragment extends Fragment {

    private SectorRepository objSectorRepository;
    private ArrayList<State> arlStateList = new ArrayList<>();
    private ArrayList<District> arlDistrictList = new ArrayList<>();
    private ArrayList<City> arlCityList = new ArrayList<>();
    private UserFarmer objFarmer = null;

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
 objSectorRepository = new SectorRepository(getActivity());
 objSectorRepository.getAllState().observe(this, new Observer<List<State>>() {
                @Override
public void onChanged(@Nullable List<State> states) {
arlStateList.addAll(states);
 }
            });
stateIdForDistrict = objFarmer.getState().getId();
objSectorRepository.getAllDistrict(stateIdForDistrict).observe(this, new Observer<List<District>>() {
                @Override
                public void onChanged(@Nullable List<District> districts) {
                    arlDistrictList.addAll(districts)
                }
            });
stateIdForCity = objFarmer.getState().getId();
            objSectorRepository.getAllCityData(stateIdForCity).observe(this, new Observer<List<City>>() {
                @Override
                public void onChanged(@Nullable List<City> cities) {
                    arlCityList.addAll(cities);
                }
            });
 btnNext = view.findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

  final AddFarmerStepThreeFragment objAddFarmerStepThreeFragment = new AddFarmerStepThreeFragment();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("FarmerEditStepTwo", objFarmer);
                    bundle.putSerializable("StateList", arlStateList);
                    bundle.putSerializable("CityList", arlCityList);
                    bundle.putSerializable("DistrictList", arlDistrictList);
                    objAddFarmerStepThreeFragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.frameLayout, objAddFarmerStepThreeFragment).commit();
                    fragmentTransaction.addToBackStack(null);
                }
}
        });

        return view;
    }

片段类

public class AddFarmerStepThreeFragment extends Fragment{
 Spinner spCity, spState, spDistrict;
 ArrayList<String> arlStateName = new ArrayList<>();
    private ArrayList<District> arlDistrictList = new ArrayList<>();
 private ArrayList<City> arlCityList = new ArrayList<>();
    private ArrayList<State> arlStateList = new ArrayList<>();
 int stateId, districtstateId, selectedStateId, selectedCityId, selectedDistrictId;
    Button btnNext;
    private SectorRepository objSectorRepository;
private UserFarmer objFarmer = null;
    Boolean isEditStepTwo;
    StateArrayAdapter adpState;
    DistrictArrayAdapter adpDistrict;
    CityArrayAdapter adpCity;

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_add_farmer_step_three, container, false);

    activity = (FarmerActivity) getActivity();

        objSectorRepository = new SectorRepository(getActivity());

  final AddFarmerAllData objAllFarmerData = (AddFarmerAllData) getArguments().getSerializable("FarmerStepTwo");


        isEditStepTwo = getArguments().getBoolean("isEditStepTwo", false);

        spCity = view.findViewById(R.id.spCity);
        spState = view.findViewById(R.id.spState);
        spDistrict = view.findViewById(R.id.spDistrict);
        etAddress = view.findViewById(R.id.etAddress);


        objSectorRepository.getAllState().observe(this, new Observer<List<State>>() {
            @Override
            public void onChanged(@Nullable List<State> states) {
                if (states.size() > 0) {
                    arlStateList.clear();
                    arlStateList.addAll(states);


                        adpState = new StateArrayAdapter(getActivity(), R.layout.item_spinner, arlStateList);
                        spState.setAdapter(adpState);

                    if (isEditStepTwo) {
                        int getPosition = adpState.getPosition(new State(objFarmer.getState().getId(), objFarmer.getState().getName()));
                        spState.setSelection(getPosition);
                    }
                }
            }
        });

 spState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            int selectedId = 0;

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                selectedId = arlStateList.get(position).getId();

                arlDistrictList.clear();
                arlCityList.clear();

                selectedStateId = arlStateList.get(position).getId();


                objSectorRepository = new SectorRepository(getActivity());
                objSectorRepository.getAllDistrict(selectedId).observe(AddFarmerStepThreeFragment.this, new Observer<List<District>>() {

                    @Override
                    public void onChanged(@Nullable List<District> districts) {

                arlDistrictList.addAll(districts);

                spDistrict.setAdapter(new DistrictArrayAdapter(getActivity(), R.layout.item_spinner, arlDistrictList));


                    }
                });

                objSectorRepository = new SectorRepository(getActivity());
                objSectorRepository.getAllCityData(selectedId).observe(AddFarmerStepThreeFragment.this, new Observer<List<City>>() {

                    @Override
                    public void onChanged(@Nullable List<City> cities) {
                        arlCityList.addAll(cities);

                        spCity.setAdapter(new CityArrayAdapter(getActivity(), R.layout.item_spinner, arlCityList));

                    }
                });
                           }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });

        spDistrict.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                selectedDistrictId = arlDistrictList.get(i).getId();
                Log.d("districtid", String.valueOf(selectedDistrictId));

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        spCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

              selectedCityId = arlCityList.get(i).getId();

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

  if (isEditStepTwo) {

 objFarmer = (UserFarmer) getArguments().getSerializable("FarmerEditStepTwo");
            arlStateList.clear();
            arlStateList.addAll((ArrayList<State>) getArguments().getSerializable("StateList"));

            if (objFarmer.getState() == null) {
                spState.setSelection(0);

            } else {

        adpState = new StateArrayAdapter(getActivity(), R.layout.item_spinner, arlStateList);

                if (arlStateList != null ||  arlStateList.size() != 0) {
                    spState.setAdapter(adpState);
                }
                int getPosition = adpState.getPosition(new State(objFarmer.getState().getId(), objFarmer.getState().getName()));
                spState.setSelection(getPosition);


            }

            if (objFarmer.getDistrict() == null) {

                spDistrict.setSelection(0);   
            }
            else
            {

              spDistrict.setSelection(objFarmer.getDistrict().getId());
            }
            if (objFarmer.getCity() == null) {
                spCity.setSelection(0);

            }
            else {


                spCity.setSelection(objFarmer.getCity().getId());
            }


        }

我希望在编辑时选择州ID的地区数据基础,但是从数据库中获取空数据。

0 个答案:

没有答案