sqlite - 基于用户输入的过滤器 - LiveData、Recycler、RoomView
问题描述
我的 Android 应用程序有一个 SQLite 数据库。数据库有一个属性表,以及每个属性的修复表。
表维修 :
@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);
}
在视图模型中:
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);
}
在repairFragment - 我们只想查看用户选择的属性的修复。属性代码propertyID 由repairFragement接收。它是已知的initData ()
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);
}
});
}
这将返回 NO RECORDS。在一个理想的世界里,我的财产不会有任何维修,但我并不生活在那个世界里!
我已经搜索了这个板以寻求过滤方面的帮助。
您能否帮助我了解如何仅过滤用户选择的属性 (propertyID) 的维修。
谢谢,瑞秋
解决方案
天啊!我想到了。
在修复视图模型中:
public LiveData<List<MYRepairs>> getPropertyRepairs(int propertyID)
{
repairPropertyLiveData = myRepairDao.getPropertyRepairs(propertyID);
return repairPropertyLiveData;
}
推荐阅读
- r - 如何在 R 中为具有大名的类别制作条形图(直方图)?
- bootstrap-4 - 如何在不使用css的情况下在引导程序中将标签向右对齐
- laravel - Vapor - 如何增加部署到登台和生产的文件夹大小?
- flutter - How to place the Add Credit & Debit Card Form in a Tabbed Bar within a box Overlapping App Bar?
- java - 具有多个项目布局的 ListView 适配器不起作用
- ios - Xcode 执行成功构建,但无法在物理设备上启动
- oracle - Oracle DBMS_SCHEDULE 在每周的每一天早上 8 点、中午 12 点、下午 4 点和晚上 8 点运行
- html - GitHub 存储库网站的 index.html 文件
- typescript - 为什么在使用类型映射时必须在“any”类型前面添加“keyof”
- c++ - 名称必须后跟“::”在 vs 错误修复建议之后不起作用