好的,我read around并且看到Java只通过值传递,而不是通过引用传递,所以我不知道如何实现这一点。
如何让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");
调用此方法。
找到标识为spinner1
的微调器,并在数据库中查询strength
字段。 field_name,这个例子中的强度必须被声明为要在onItemSelectedListener中使用的最终变量,否则我将得到错误Cannot refer to a non-final variable field_name inside an inner class defined in a different method
。
但是,当使用每个不同的Spinner时,如何让onItemSelectedListener更改不同实例成员的值?这是所有重要的代码行:
object_reference_to_clas_member_of_field_name = theCursor .getString(theCursor.getColumnIndex(field_name));
我无法使用最终的String,因为当用户选择不同的值时,变量显然会发生变化。我已经阅读了很多内容并且难以解决问题。我可以复制并粘贴此代码6次并忘记重构但我真的很想知道优雅的解决方案。如果你不理解我的问题发表评论,我不确定我是否能很好地解释自己。
答案 0 :(得分:1)
将您的侦听器重构为新的“类”。根据需要使用正确的参数/实例进行初始化,以便重复使用“代码”。
答案 1 :(得分:1)
您可以通过将其他类作为fillSpinner
方法的参数传递来执行此操作:
一个。创建interface
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) {
}
});
}
℃。提供听众:
fillSpinner("spinner1","strength", new OnSpinnerValueSelected() {
public void onValueSelected(String selectedValue) {
yourObject.setField(selectedValue);
}
});
答案 2 :(得分:0)
是的,这就是我管理它的方式,但我仍然接受新建议以获得已接受的答案,我也创造了赏金。
我没有创建一个像panzerschreck建议的新类,所以我发布这个作为我自己问题的新答案。但是我只是在监听器中创建了一个if..then..else语句来检查选择了哪个微调器,然后设置一个不同的实例成员。
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Cursor theCursor = (Cursor) parent.getSelectedItem();
if (field_name.equalsIgnoreCase("strength")) {
strength=theCursor.getString(theCursor.getColumnIndex(field_name));
} else if (field_name.equalsIgnoreCase("ring")) {
ring_gauge=theCursor.getString(theCursor.getColumnIndex(field_name));
} else if (field_name.equalsIgnoreCase("country")) {
country=theCursor.getString(theCursor.getColumnIndex(field_name));
} else if (field_name.equalsIgnoreCase("wrapper")) {
wrapper=theCursor.getString(theCursor.getColumnIndex(field_name));
} else if (field_name.equalsIgnoreCase("length")) {
length=theCursor.getString(theCursor.getColumnIndex(field_name));
} else if (field_name.equalsIgnoreCase("price")) {
price=theCursor.getString(theCursor.getColumnIndex(field_name));
}
// showToast(category);
}
public void onNothingSelected(AdapterView<?> parent) {
// showToast("Spinner2: unselected");
}
});
以下是班级成员
private String strength,ring_gauge,country,wrapper,length,price;
有点破解但没有Java允许通过引用真正传递对象,这就是我所能做的。