我有下面的代码,一旦从第二个微调器中选择了一首诗,那么它应该在文本视图中显示一首诗摘录。摘录存储在字符串xml下的values文件夹中。问题是我不知道如何根据选择的内容将文本视图内容设置为诗歌,任何人都可以帮忙吗?
代码:
public class Poets extends Activity implements
OnItemSelectedListener {
// defining variables
Spinner PoetSpinner, PoemSpinner;
String Poets[] = { "", "Robert Burns", "James Beattie",
"Duncan MacGregor Crerar" };
String Poems1[] = { "", "Tam O Shanter", "Auld Lang Syne" };
String Poems2[] = { "", "Judgement Of Paris", "The Minstrel" };
String Poems3[] = { "", "A Poem Commemorating Robbie Burns",
"My Bonnie Rowan Tree" };
TextView selectedPoem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set to fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set content view
setContentView(R.layout.poet);
initialize(); // call this method
}
public void initialize() {
// initializing items that are in the class
PoetSpinner = (Spinner) findViewById(R.id.spPoet);
PoemSpinner = (Spinner) findViewById(R.id.spPoem);
// Hide the second spinner
PoemSpinner.setVisibility(View.GONE);
selectedPoem = (TextView) findViewById(R.id.tvSelectedPoem);
// setting up array adapters
//
// for each spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.poet_arrays,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
PoetSpinner.setAdapter(adapter);
PoetSpinner.setOnItemSelectedListener(this);
PoemSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object poem = parent.getItemAtPosition(position);
selectedPoem.setText(poem.toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
ArrayAdapter<CharSequence> adapter = null;
switch (pos) {
case 1:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays1, android.R.layout.simple_spinner_item);
break;
case 2:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays2, android.R.layout.simple_spinner_item);
break;
case 3:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays3, android.R.layout.simple_spinner_item);
break;
default:
// Hide the poem spinner
PoemSpinner.setVisibility(View.GONE);
break;
}
if (adapter != null) {
// Set the poem spinners content and show the spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
PoemSpinner.setAdapter(adapter);
PoemSpinner.setVisibility(View.VISIBLE);
}
}
答案 0 :(得分:2)
Resources res = getResources();
String text = res.getString(R.string.insert_poem_identifier_here);
该示例代码应该满足您的需求,只需插入所需的字符串资源标识符即可。