繁体   English   中英

引用字符串数组

[英]Referencing string array

我有一个困扰我的问题。 我在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>

火导弹对话碎片

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

    }

尝试“数组”:

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

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>

解决了问题。

我必须删除import.android.R,在此之后它起作用了! 多谢你们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM