引用字符串数组
我有一个问题在困扰着我。我在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();
}
尝试 “数组”:
<array name="bookmark_titles">
<item>Google</item>
<item>Bing</item>
</array>
仍然看不到它 – Dodi
的文档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>
我不认为这是一个问题。 http://developer.android.com/guide/topics/resources/string-resource.html#StringArray。我只是尝试了与字符串数组为我工作。 – Raghunandan
解决该问题。
我不得不删除import.android.R,之后,它的工作!谢谢你们
你试过干净/重建吗?无法看到它不会显示为“R.array.bookmark_titles”的原因。 –
将它称为'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
@Joachim Isaksson是的,我清理了项目,同样的问题 – Dodi