首页 > 解决方案 > 基于用户输入的过滤器 - 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 - 我们只想查看用户选择的属性的修复。属性代码propertyIDrepairFragement接收。它是已知的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) 的维修。

谢谢,瑞秋

标签: sqlitefilterandroid-roomandroid-livedata

解决方案


天啊!我想到了。

在修复视图模型中:

public LiveData<List<MYRepairs>> getPropertyRepairs(int propertyID)
    {

        repairPropertyLiveData = myRepairDao.getPropertyRepairs(propertyID);
        return repairPropertyLiveData;
    }

推荐阅读