2013-07-24 60 views
0

我有一個問題在困擾着我。我在strings.xml中創建了一個名爲bookmark_titles的字符串數組。我想用它作爲我的警報對話框並使用它們填充一個列表,但是我無法在R.array中看到我的數組的名稱,它只有那些在android中構建的例如PhoneTypes。我如何引用我的數組?引用字符串數組

strings.xml 

<string name="app_name">Dialogs</string> 
<string name="action_settings">Settings</string> 

<string-array name="bookmark_titles"> 
    <item>Google</item> 
    <item>Bing</item> 
    <item>Gmail</item> 
</string-array> 

</resources> 

FireMissilesDialogFragment

public class FireMissilesDialogFragment extends DialogFragment{ 

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Books are :"); 
       .setItems(R.array., new DialogInterface.OnClickListener() { ---> can't see reference here 
        public void onClick(DialogInterface dialog, int which) { 
        // The 'which' argument contains the index position 
        // of the selected item 
       } 
     }); 
     return builder.create(); 

    } 
+1

你試過乾淨/重建嗎?無法看到它不會顯示爲「R.array.bookmark_titles」的原因。 –

+0

將它稱爲'R.array.bookmark_titles'。更多信息@ http://developer.android.com/guide/topics/ui/dialogs.html。另請查看http://developer.android.com/guide/topics/resources/string-resource.html#StringArray。 – Raghunandan

+0

@Joachim Isaksson是的,我清理了項目,同樣的問題 – Dodi

回答

1

嘗試 「數組」:

<array name="bookmark_titles"> 
<item>Google</item> 
<item>Bing</item> 
</array> 
+0

仍然看不到它 – Dodi

0

的文檔setItems()指出:

這應該是一個數組類型即R.array.foo

使用array代替string-array

<string name="bookmark_google">Google</string> 
<string name="bookmark_bing">Bing</string> 
<string name="bookmark_yahoo">Yahoo</string> 

<array name="pref_values_sort_list"> 
    <item>@string/bookmark_google</item> 
    <item>@string/bookmark_bing</item> 
    <item>@string/bookmark_yahoo</item> 
</array> 
+0

我不認爲這是一個問題。 http://developer.android.com/guide/topics/resources/string-resource.html#StringArray。我只是嘗試了與字符串數組爲我工作。 – Raghunandan

0

解決該問題。

我不得不刪除import.android.R,之後,它的工作!謝謝你們