2013-03-17 44 views
0

我有一個問題,下面的代碼,我有一個微調,有三個詩人的名字,當一個被挑選,它設置第二個微調的內容,當從第二個微調設置文本中選擇一首詩查看內容給詩。這適用於除了巴黎詩歌的判斷之外的每一個人,當選擇什麼都不做時,任何人都能看到爲什麼?使用微調器設置文本不適用於我的列表中只有一個項目,如何解決?

這是字符串副本的名稱和粘貼

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> 
+0

請發表您的數組文件的內容 – 2013-03-17 00:48:37

+0

你的問題就沒有意義了。你說這個字符串是「判斷」,但是這首詩是「巴黎的審判」。他們如何結合在一起? – Squonk 2013-03-17 00:50:23

+0

@Squonk因爲它是如果字符串等於「巴黎判決」,那麼它將文本設置爲判斷。說得通。 – Thomas 2013-03-17 01:12:17

回答

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> 
相關問題