gpt4 book ai didi

java - 如何从 setOnItemSelectedListener 方法获取字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 11:55:37 25 4
gpt4 key购买 nike

我是 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) {

}
});

我正在尝试使用以下方法将此字符串设置为 TextView :

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());

我收到一条错误消息“无法解析方法 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());

}

最佳答案

你可以这样做:

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 变量传递给您想要的任何方法。但是,请记住,除非您在微调器中选择某些内容,否则它的值为 null

关于java - 如何从 setOnItemSelectedListener 方法获取字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36131281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com