0

我对下面的代码有问题,我有一个带有三个诗人名称的旋转器,当挑选一位诗人时,它设置了第二个旋转器的内容,以及当从第二个旋转器中选择一首诗时,该诗是设置文本视图内容的到这首诗。This works for every single one except the Judgment of Paris poem which when chose does nothing, can anyone see why?

这是复制并粘贴的字符串的名称

string name="judgement"

代码:

    // 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);
    if("Tam O Shanter".equals(poem.toString())){
         selectedPoem.setText(getString(R.string.tamO));
    }
    if("Auld Lang Syne".equals(poem.toString())){
     selectedPoem.setText(getString(R.string.auld));
   }
    if("Judgement Of Paris".equals(poem.toString())){
     selectedPoem.setText(getString(R.string.judgement));
   }
    if("The Minstrel".equals(poem.toString())){
     selectedPoem.setText(getString(R.string.minstrel));
   }
    if("A Poem Commemorating Robbie Burns".equals(poem.toString())){
     selectedPoem.setText(getString(R.string.rob));
   }
    if("My Bonnie Rowan Tree".equals(poem.toString())){
     selectedPoem.setText(getString(R.string.row));
   }

  }

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

XML 文件

    <string name="tamO">
But pleasures are like poppies spread, 
You seize the flower, its bloom is shed; 
Or like the snow falls in the river, 
A moment white--then melts for ever; 
Or like the borealis race, 
That flit ere you can point their place; 
Or like the rainbow\'s lovely form 
Evanishing amid the storm.-- 
Nae man can tether time or tide; 
The hour approaches Tam maun ride; 
That hour, o\' night\'s black arch the key-stane, 
That dreary hour he mounts his beast in; 
And sic a night he taks the road in 
As ne\'er poor sinner was abroad in.
</string>
<string name="auld">
For auld lang syne, my dear, 
For auld lang syne. 
We\'ll tak a cup o\' kindness yet, 
For auld lang syne. 

And surely ye\'ll be your pint stowp! 
And surely I\'ll be mine! 
And we\'ll tak a cup o\'kindness yet, 
For auld lang syne. 

We twa hae run about the braes, 
And pou\'d the gowans fine; 
But we\'ve wander\'d mony a weary fit, 
Sin\' auld lang syne. 
</string>
<string name="judgement">
Up the broad cliffs, that tower\'d immense to heaven,
Green wav\'d the lofty pines, on every side;
Save where, fair-opening to the beam of even,
A dale sloped gradual to the valley wide.

Echoed the vale with many a chearful note;
The various lowe of herds resounding long,
The shrilling pipe, the mellow horn remote,
And social clamours of the festive throng.

For now, low-hovering o\'er the western main, 
Where amber clouds begirt his dazzling throne,
The sun with ruddier verdure deck\'d the plain;
And lakes, and streams, and spires triumphal shone;
</string>
<string name="minstrel">
    Of chance or change O let not man complain,
Else shall he never never cease to wail:
For, from th\' imperial dome, to where the swain
Rears the lone cottage in the silent dale,
All feel th\' assault of fortune\'s fickle gale;
Art, empire, earth itself, to change are doom\'d;
Earthquakes have raised to heaven the humble vale,
And gulphs the mountain\'s mighty mass entomb\'d,
And where th\' Atlantick rolls wide continents have bloom\'d.

But sure to foreign climes we need not range,
Nor search the antient records of our race,
To learn the dire effects of time and change,
Which in ourselves, alas, we daily trace.
Yet at the darken\'d eye, the wither\'d face,
Or hoary hair, I never will repine:
But spare, O Time, whate\'er of mental grace,
Of candour, love, or sympathy divine,
Whate\'er of fancy\'s ray, or friendship\'s flame is mine.
</string>  
<string name="rob">
He touched our country\’s ancient harp
With truest patriotic fire;
Forth thrilling came soul-stirring strains,
Man\’s nobler actions to inspire.
The cottar\’s fireside, \‘neath his spell,
Becomes at once a hallowed shrine;
His  hymn to Mary swells the heart,
And fills the eye his Auld Lang Syne.

Not to his native land alone
His genius and his fame belong,
In other climes is treasured dear
His matchless legacy of song.
His melodies have echoing gone
To continents and isles afar;
 They  cheer and gladden hearts alike
\‘Neath Southern Cross and Polar Star.

</string>

<string name="row">
Thrice welcome sweet green spray
Culled from my Rowan Tree,  
By loved ones far away,
In Bonnie Amulree.

 In Boyhood\’s days thy root
Was planted by my hand;
Just ere I left my dear,
My Scottish fatherland.

Thou but a sapling then,
Though now a sheltering tree,
While warblers in thy boughs,
Sing sweetest melodies.
</string>

<string-array name="film_arrays">
    <item>Please Select...</item>
    <item>Sean Connery</item>
    <item>Billy Connolly</item>
    <item>James McAvoy</item>
    <item>Karen Gillan</item>
    <item>Kelly Macdonald</item>
    <item>Ashley Jensen</item>
</string-array>

<string-array name="poet_arrays">
    <item>Please Select...</item>
    <item>Robert Burns</item>
    <item>James Beattie</item>
    <item>Duncan MacGregor Crerar</item>
</string-array>

    <string-array name="poem_arrays1">
    <item>Please Select...</item>
    <item>Tam O Shanter</item>
    <item>Auld Lang Syne</item>
</string-array> 
    <string-array name="poem_arrays2">
    <item>Please Select...</item>
    <item>Judgment Of Paris</item>
    <item>The Minstrel</item>
</string-array>
    <string-array name="poem_arrays3">
    <item>Please Select a...</item>
    <item>A Poem Commemorating Robbie Burns</item>
    <item>My Bonnie Rowan Tree</item>
</string-array>
4

1 回答 1

0

你在比较

巴黎审判 (poem_arrays2) 到

巴黎审判(代码)。

这两个字符串显然是不同的。

顺便说一句,我真的会考虑在 xml 文件中使用数据库而不是硬编码字符串。数据库将为您提供一种防止此类错误发生的设计。它也更加通用和可扩展。如果您有更多诗人和/或诗歌,只需将它们添加到您的表格中,而无需更改任何代码。或者,您可以使用反映实体层次结构的 xml 文件,如下所示:

<poet name="Robert Burns">
    <poem name="Tam O Shanter">
        But pleasures are like poppies spread, 
        [...] 
    </poem>
    <poem name="Auld Lang Syne">
        For auld lang syne, my dear,
        [...] 
    </poem>
</poet>
于 2013-03-18T01:30:32.290 回答