我有一个用于Android应用程序的SQLite数据库。数据库在每个属性中都有一个用于[[properties的table和另一个用于repairs的表。
表维修
:@Entity(tableName = "repairs",
indices = {@Index(value = "repairID", unique = true), @Index("repairPropID")})
public class MYRepairs
{
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "repid")
public int id;
@ColumnInfo(name = "repairID")
@NonNull
public String repairID;
@ColumnInfo(name = "repairPropID")
public int repairPropID;
}
...然后是
RepairsDao.java
@Dao
public interface MyRepairsDao
{
@Query("SELECT * FROM repairs WHERE repid = :repid LIMIT 1")
MYRepairs findRepairById(String repid);
@Query("SELECT * FROM repairs WHERE repairPropID = :repairPropID")
MYRepairs findRepairByPropID(int repairPropID);
@Query("SELECT * FROM repairs ORDER BY repairPropID ASC")
LiveData<List<MYRepairs>> getAllRepairs();
@Query("SELECT * FROM repairs WHERE repairPropID = :repairPropID")
LiveData<List<MYRepairs>> getPropertyRepairs(int repairPropID);
}
在:中ViewModel
public class repairViewModel extends AndroidViewModel
{
private MyRepairsDao myRepairDao;
private LiveData<List<MYRepairs>> repairLiveData;
private LiveData<List<MYRepairs>> repairPropertyLiveData;
private LiveData<String> filterLiveData = new MutableLiveData<String>();
public repairViewModel(@NonNull Application application)
{
super(application);
myRepairDao = DogwoodDatabase.getDatabase(application).repairDao();
repairLiveData = myRepairDao.getAllRepairs();
repairPropertyLiveData = myRepairDao.getPropertyRepairs(propertyID);
}
public LiveData<List<MYRepairs>> getAllRepairs()
{
return repairLiveData;
}
public LiveData<List<MYRepairs>> getPropertyRepairs(int propertyID)
{
return repairPropertyLiveData;
}
并且在:中RecyclerView.Adapter
public class repairListAdapter extends RecyclerView.Adapter<repairListAdapter.RepairViewHolder>
{
@NonNull
@Override
public repairListAdapter.RepairViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
final View itemView = layoutInflater.inflate(R.layout.repaircontent, parent, false);
return new repairListAdapter.RepairViewHolder(itemView);
}
在中-我们只想查看用户选择的属性的修复。 repairFragement接收到属性代码propertyID。 initData(()是已知的repairFragment
public class repairFragment extends Fragment
{
private repairListAdapter repairListAdapter;
private repairViewModel repairViewModel;
public void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
propertyID = getArguments().getString("ARG_PROPNUM_ID");
propNumID = Integer.parseInt(propertyID);
initData();
}
private void initData()
{
Log.e ("INIT", "ID is " + propertyID);
repairViewModel = new
ViewModelProvider(this).get(repairViewModel.class);
repairViewModel.getPropertyRepairs(propertyID).observe(this, new Observer<List<MYRepairs>>()
{
@Override
public void onChanged(@Nullable List<MYRepairs> MYRepairs)
{
repairListAdapter.setMYRepairList(MYRepairs);
}
});
}
此返回无记录。在理想的世界中,我的财产无法修复,但我不住在那个世界中!我已搜寻此板寻求过滤方面的帮助。
您能如何帮助我[仅修复用户选择的属性(propertyID)。
感谢Rachael
public LiveData<List<MYRepairs>> getPropertyRepairs(int propertyID)
{
repairPropertyLiveData = myRepairDao.getPropertyRepairs(propertyID);
return repairPropertyLiveData;
}
public LiveData<List<MYRepairs>> getPropertyRepairs() {
return repairPropertyLiveData;
}
和
private MutableLiveData<String> selectedPropertyId = new MutableLiveData<>();
private final repairPropertyLiveData = Transformations.switchMap(selectedPropertyId, (id) -> {
return myRepairDao.getPropertyRepairs(id);
});
public void updateSelectedProperty(String propertyId) {
selectedPropertyId.setValue(propertyId);
}
这样,您不会得到新的订阅,如果以后要选择其他属性ID,新订阅将不会被正确取消。