基于用户输入的过滤器 - LiveData、Recycler、RoomView
Filter based on user input - LiveData, Recycler, RoomView
我的 Android 应用程序有一个 SQLite 数据库。数据库有一个 table 用于 properties ,另一个 table 用于 repairs 在每个 属性。
Table 维修 :
@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);
}
在 repairFragment 中 - 我们只想查看所选 属性 用户的 维修。 属性 代码 属性ID 由 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);
}
});
}
这 returns 没有记录。
在一个理想的世界里,我的财产没有维修,但我不生活在那个世界里!
我已搜索过此版块以寻求有关过滤的帮助。
您能否帮助我如何 过滤 仅针对选定的 属性 (属性ID) 用户的维修。
谢谢,瑞秋
天哪!我想到了。
正在修复ViewModel:
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);
}
这样,您就不会得到未正确失效的新订阅,以防您稍后 select 一个不同的 属性 id。
我的 Android 应用程序有一个 SQLite 数据库。数据库有一个 table 用于 properties ,另一个 table 用于 repairs 在每个 属性。
Table 维修 :
@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);
}
在 repairFragment 中 - 我们只想查看所选 属性 用户的 维修。 属性 代码 属性ID 由 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);
}
});
}
这 returns 没有记录。 在一个理想的世界里,我的财产没有维修,但我不生活在那个世界里!
我已搜索过此版块以寻求有关过滤的帮助。
您能否帮助我如何 过滤 仅针对选定的 属性 (属性ID) 用户的维修。
谢谢,瑞秋
天哪!我想到了。
正在修复ViewModel:
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);
}
这样,您就不会得到未正确失效的新订阅,以防您稍后 select 一个不同的 属性 id。