如何从 setOnItemSelectedListener 方法获取字符串?
How to get String from setOnItemSelectedListener method?
我是 Android 的新手,我正在尝试从微调器中获取字符串。我做了一些研究,但找不到任何有用的东西。无需尝试获取 String 代码即可正常工作。这是有效的代码:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我尝试使用 getter 方法,但没有出现任何错误:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我正在尝试使用以下方法将此字符串设置为文本视图:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivityClassName AnObject = new MainActivityClassName();
text.setText(AnObject.getSpinnerString());
我收到一条错误消息 "cannot resolve method getSpin()"。我确实意识到我没有将任何参数传递给该方法,但我不知道如何在此处执行此操作。我感谢以不同方式解决问题的任何帮助或任何其他建议。提前致谢。
如果有帮助,这是完整代码:
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView) findViewById(R.id.textView);
// spinner 1
final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
.createFromResource(this, R.array.lang,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner1.setAdapter(staticAdapter1);
// spinner 2
final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);
staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String spinnerLanguage = staticSpinner1.getSelectedItem().toString();
switch (spinnerLanguage) {
case "Afrikaans":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
.createFromResource(getBaseContext(), R.array.af,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterAF
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterAF);
break;
case "French":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.fr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterFR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterFR);
break;
case "English":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
.createFromResource(getBaseContext(), R.array.en,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterEN
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterEN);
break;
case "Turkish":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.tr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterTR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterTR);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivity AnObject = new MainActivity();
text.setText(AnObject.getSpinnerString());
}
}
阅读了那段代码后,为什么不将 "spinnerLanguage2" 提取到主 class 中,然后从那里访问它呢?在 OnItemSelected 方法中赋值。
编辑:
我之前的回答是错误的。尝试实现一个接口以便与其他接口通信 class。因此,调用该接口的方法可以传递值。我给你一个 link 这个(它解释了它的片段,但你也可以传达你的 classes):
http://developer.android.com/intl/es/training/basics/fragments/communicating.html
如果你真的必须在外面使用它,你可以这样做:
public void setTextView(String text){
text.setText(text);
}
并在您的 onItemSelected 中调用此方法:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextView(staticSpinner2.getSelectedItem().toString());
}
你可以这样做:
String selectedText = null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedText = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
现在您可以使用 selectedText
变量传递给您想要的任何方法。但是,请记住,除非您 select 微调器中的某些内容,否则它的值将是 null
.
我是 Android 的新手,我正在尝试从微调器中获取字符串。我做了一些研究,但找不到任何有用的东西。无需尝试获取 String 代码即可正常工作。这是有效的代码:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我尝试使用 getter 方法,但没有出现任何错误:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我正在尝试使用以下方法将此字符串设置为文本视图:
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpinnerString(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivityClassName AnObject = new MainActivityClassName();
text.setText(AnObject.getSpinnerString());
我收到一条错误消息 "cannot resolve method getSpin()"。我确实意识到我没有将任何参数传递给该方法,但我不知道如何在此处执行此操作。我感谢以不同方式解决问题的任何帮助或任何其他建议。提前致谢。
如果有帮助,这是完整代码:
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = (TextView) findViewById(R.id.textView);
// spinner 1
final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
.createFromResource(this, R.array.lang,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner1.setAdapter(staticAdapter1);
// spinner 2
final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);
staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String spinnerLanguage = staticSpinner1.getSelectedItem().toString();
switch (spinnerLanguage) {
case "Afrikaans":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
.createFromResource(getBaseContext(), R.array.af,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterAF
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterAF);
break;
case "French":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.fr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterFR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterFR);
break;
case "English":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
.createFromResource(getBaseContext(), R.array.en,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterEN
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterEN);
break;
case "Turkish":
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
.createFromResource(getBaseContext(), R.array.tr,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
staticAdapterTR
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
staticSpinner2.setAdapter(staticAdapterTR);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
assert staticSpinner2 != null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();
}
public String getSpin(String spinnerLanguage2) {
return spinnerLanguage2;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
MainActivity AnObject = new MainActivity();
text.setText(AnObject.getSpinnerString());
}
}
阅读了那段代码后,为什么不将 "spinnerLanguage2" 提取到主 class 中,然后从那里访问它呢?在 OnItemSelected 方法中赋值。
编辑: 我之前的回答是错误的。尝试实现一个接口以便与其他接口通信 class。因此,调用该接口的方法可以传递值。我给你一个 link 这个(它解释了它的片段,但你也可以传达你的 classes):
http://developer.android.com/intl/es/training/basics/fragments/communicating.html
如果你真的必须在外面使用它,你可以这样做:
public void setTextView(String text){
text.setText(text);
}
并在您的 onItemSelected 中调用此方法:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
setTextView(staticSpinner2.getSelectedItem().toString());
}
你可以这样做:
String selectedText = null;
staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedText = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
现在您可以使用 selectedText
变量传递给您想要的任何方法。但是,请记住,除非您 select 微调器中的某些内容,否则它的值将是 null
.