首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用旋转器设置文本不适用于我的列表中的一个项目,如何解决?

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

Stack Overflow用户
提问于 2013-03-17 08:21:12
回答 1查看 533关注 0票数 0

我对下面的代码有一个问题,我有一个旋转器,它有三个诗人的名字,当其中一个被选中时,它设置第二个旋转器的内容,当从第二个旋转器中选择一首诗时,它将文本视图内容设置为该诗。

这是字符串copy and pasted的名称

代码语言:javascript
复制
string name="judgement"

代码:

代码语言:javascript
复制
    // 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文件

代码语言:javascript
复制
    <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>
EN

回答 1

Stack Overflow用户

发布于 2013-03-18 09:30:32

您正在比较

巴黎判决书(poem_arrays2)

巴黎审判(代码)。

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

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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15456295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档