如何优化只有类更改的重复代码?

时间:2016-01-12 16:32:05

标签: java android class reflection enums

我在Java中有一个基本问题,困扰了我很长一段时间。请考虑以下示例:

ArrayAdapter<CarDetailsAdapter.OwnerType> ownerTypeArrayAdapter = new ArrayAdapter<CarDetailsAdapter.OwnerType>(
                CarDetailsActivity.this, android.R.layout.simple_spinner_dropdown_item, CarDetailsAdapter.OwnerType.values());
        ownerSpinner.setAdapter(ownerTypeArrayAdapter);
        ownerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                owner_result = Integer.toString(((CarDetailsAdapter.OwnerType) ownerSpinner.getSelectedItem()).value);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        ArrayAdapter<CarDetailsAdapter.DriverType> driver_type = new ArrayAdapter<CarDetailsAdapter.DriverType> (
        CarDetailsActivity.this, android.R.layout.simple_spinner_dropdown_item, CarDetailsAdapter.DriverType.values());
        driverSpinner.setAdapter(driver_type);
        driverSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                driver_result = Integer.toString(((CarDetailsAdapter.DriverType) driverSpinner.getSelectedItem()).value);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

上述代码可以解释如下:

1 - 创建一个OwnerType / DriverType类型的ArrayAdapter(Iterator),它是两个不同的枚举 2 - 将适配器连接到微调器上 3 - 在微调器中选择一个值(基于onItemSelected侦听器)时,根据变量更新结果

虽然代码是针对Android的,但我认为基础知识也可以扩展到Java。

除了输入类(枚举)即CarDetailsAdapter.OwnerType & CarDetailsAdapter.DriverType之外,两个代码段实际上都是相同的。

是否有某种方法可以将此功能转换为函数,以便我不必冗余地重复相同的代码行?我已经尝试过几个小时的反思,希望它可以解决问题,但我想我错过了一些东西。

由于

1 个答案:

答案 0 :(得分:1)

这样的事情可行:

interface Callback<T> {
    void complete(T result);
}

public <T> void spinner(int spinnerId, Class<? extends Enum> enumClass, final Callback<T> callback) {

    final Spinner spinner = (Spinner) getActivity().findViewById(spinnerId);
    ArrayAdapter<Enum> arrayAdapter = new ArrayAdapter<Enum>(
            getActivity(), android.R.layout.simple_spinner_dropdown_item, enumClass.getEnumConstants());
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            T result = (T) spinner.getSelectedItem();
            callback.complete(result);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

// ....

spinner(R.id.ownerSpinner, CarDetailsAdapter.OwnerType.class, new Callback<CarDetailsAdapter.OwnerType>() {
        @Override
        public void complete(CarDetailsAdapter.OwnerType result) {
            owner_result = result;
        }     
    });

您可能也想要返回微调器或以其他方式调整。