java – 通过引用多个Spinners的setOnItemSelectedListener来传递String变量

好的,我已经
read around并且看到Java只通过值传递,而不是通过引用传递,所以我不知道如何实现这一点.

>我在Android活动中有6个Spinners,它们填充了不同的SQLite查询.
>填充每个Spinner并设置OnItemSelectedListener的代码非常类似,所以我希望重构一个方法并使用每个Spinner ID和Sqlite查询调用它6次.
>如何让Spinner onItemSelectedListener更改每个不同Spinner上的右实例成员?

public void fillSpinner(String spinner_name, final String field_name) {
// This finds the Spinner ID passed into the method with spinner_name
// from the Resources file. e.g. spinner1
int resID = getResources().getIdentifier(spinner_name, "id",
        getPackageName());
Spinner s = (Spinner) findViewById(resID);
final Cursor cMonth;
// This gets the data to populate the spinner, e.g. if field_name was
// strength = SELECT _id, strength FROM cigars GROUP BY strength
cMonth = dbHelper.fetchSpinnerFilters(field_name);
startManagingCursor(cMonth);
String[] from = new String[] { field_name };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter months = new SimpleCursorAdapter(this,
        android.R.layout.simple_spinner_item, cMonth, from, to);
months.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(months);
// This is setting the Spinner Item Selected Listener Callback, where
// all the problems happen
s.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        Cursor theCursor = (Cursor) parent.getSelectedItem();
        // This is the problem area.
        object_reference_to_clas_member_of_field_name = theCursor
                .getString(theCursor.getColumnIndex(field_name));
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // showToast("Spinner1: unselected");
    }
});

}

你可以像这个fillSpinner(“spinner1”,“strength”);这样调用这个方法.

它找到具有id spinner1的微调器,并在数据库中查询strength字段. field_name,这个例子中的强度必须被声明为要在onItemSelectedListener中使用的最终变量,否则我将得到错误不能引用在不同方法中定义的内部类中的非最终变量field_name.

但是,当使用每个不同的Spinner时,如何让onItemSelectedListener更改不同实例成员的值?这是所有重要的代码行:
object_reference_to_clas_member_of_field_name = theCursor .getString(theCursor.getColumnIndex(field_name));

我不能使用最终的String,因为当用户选择不同的值时,变量显然会发生变化.我已经阅读了很多内容并且难以解决问题.我可以复制并粘贴此代码6次并忘记重构但我真的很想知道优雅的解决方案.如果你不理解我的问题发表评论,我不确定我是否能很好地解释自己.

最佳答案 你可以通过传递额外的类作为fillSpinner方法的参数来做到这一点:

A.创建界面

public interface OnSpinnerValueSelected {
    void onValueSelected(String selectedValue);
}

B.稍微改变你的方法:

public void fillSpinner(String spinner_name, final String field_name,
                    final OnSpinnerValueSelected valueChangeListener) {

    // Prepare spinner

    s.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view,
                     int position, long id) {
            Cursor theCursor = (Cursor) parent.getSelectedItem();

            valueChangeListener.onValueSelected(theCursor
                           .getString(theCursor.getColumnIndex(field_name)));
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

C.提供听众:

fillSpinner("spinner1","strength", new OnSpinnerValueSelected() {
    public void onValueSelected(String selectedValue) {
        yourObject.setField(selectedValue);
    }
});
点赞