如何使Android片段与recyclerview和greendao更通用?

如何使Android片段与recyclerview和greendao更通用?,android,sqlite,android-recyclerview,fragment,greendao,Android,Sqlite,Android Recyclerview,Fragment,Greendao,你好,我有一个android应用程序,带有sqlite数据库和GreenDao。 数据库如下所示:InventoryCategory(id,名称) 库存单位(id、名称) 存货项目(id、名称、金额、类别、单位) 在单独的片段中,我显示了它们的列表(使用recyclerView),用户可以添加/编辑/删除项目/类别/单元 我的问题是:如何以更通用的方式实现这一点,因为我认为这会导致太多的代码重复?尤其是类别和单元的片段,因为尽管对象类型不同,它们是相同的。我考虑过单位和类别的超类,比如ItemA

你好,我有一个android应用程序,带有sqlite数据库和GreenDao。 数据库如下所示:
InventoryCategory(id,名称)
库存单位(id、名称)
存货项目(id、名称、金额、类别、单位)

在单独的片段中,我显示了它们的列表(使用recyclerView),用户可以添加/编辑/删除项目/类别/单元

我的问题是:如何以更通用的方式实现这一点,因为我认为这会导致太多的代码重复?尤其是类别和单元的片段,因为尽管对象类型不同,它们是相同的。我考虑过单位和类别的超类,比如ItemAttribute,但GreenDao有问题,因为它需要具体的类型。
这就是我添加新类别的方式

private void showAddCategoryDialog() {
    int paddingDp = 20;
    final float scale = getResources().getDisplayMetrics().density;
    int paddingPx = (int) (paddingDp * scale + 0.5f);
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

    EditText addCategoryDialogEditText = new EditText(getContext());
    FrameLayout container = new FrameLayout(getContext());
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(paddingPx, 0, paddingPx, 0);
    addCategoryDialogEditText.setLayoutParams(params);
    container.addView(addCategoryDialogEditText);

    builder.setTitle("Add category");
    builder.setMessage("Type category name");
    builder.setView(container);
    builder.setPositiveButton("Add", (dialogInterface, i) -> {
    });

    builder.setNegativeButton("Close", ((dialogInterface, i) -> dialogInterface.cancel()));

    final AlertDialog dialog = builder.create();
    dialog.show();

    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener((view -> {
        categoryName = addCategoryDialogEditText.getText().toString();
        if (categoryName.isEmpty()) {
            Toast.makeText(getContext(), "Name cannot be empty", Toast.LENGTH_SHORT).show();
        } else {
            InventoryCategory category = new InventoryCategory();
            category.setName(categoryName);
            addNewCategory(category);
            dialog.dismiss();
        }
    }));

}

private void addNewCategory(InventoryCategory category) {
    AsyncSession asyncSession = ((MainActivity) getActivity()).getDaoSession().startAsyncSession();
    asyncSession.setListener(operation -> getCategories());
    asyncSession.insert(category);
}
这样我就可以从数据库中获取所有类别

private void getCategories() {
    AsyncSession asyncSession = ((MainActivity) getActivity()).getDaoSession().startAsyncSession();
    asyncSession.setListener(operation -> {
        categories.clear();
        categories.addAll((List<InventoryCategory>) operation.getResult());

        for(InventoryCategory ic : categories){
            Log.d("InvCat ", ic.getName());
        }
        getActivity().runOnUiThread(() -> categoryRowAdapter.notifyDataSetChanged());
    });
    asyncSession.loadAll(InventoryCategory.class);
}
private void getCategories(){
AsyncSession AsyncSession=((MainActivity)getActivity()).getDaoSession().startAsyncSession();
asyncSession.setListener(操作->{
类别。清除();
categories.addAll((List)operation.getResult());
用于(目录类别ic:类别){
Log.d(“InvCat”,ic.getName());
}
getActivity().runOnUiThread(()->categoryRowAdapter.notifyDataSetChanged());
});
asyncSession.loadAll(InventoryCategory.class);
}
对于InventoryUnit,它看起来完全相同,只是不同的类和方法名。 谢谢