使用微调器设置文本不适用于我的列表中的一个项目,如何解决?

时间:2013-03-17 00:21:12

标签: android

我对下面的代码有疑问,我有一个具有三个诗人名字的微调器,当一个被挑选时,它设置第二个微调器的内容,并且当从第二个微调器中选择一个诗时设置文本查看内容到诗。这对每一个人都有效,除了巴黎诗歌的判断,当选择什么都不做时,谁能明白为什么?

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

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>

1 个答案:

答案 0 :(得分:0)

您正在比较

巴黎的判决(poem_arrays2)

巴黎的判决(代码)。

两个字符串明显不同。

BTW我真的会考虑在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>