Android 创建动态字符串数组

发布于 2024-10-30 02:52:23 字数 440 浏览 0 评论 0原文

我需要一个动态字符串数组。我见过的所有示例都显示硬编码的字符串数组。基本上我有一个年份列表供用户选择。年份是硬编码的,但不幸的是,今年将不再是明年的相关选择。我不想在将来为此推送更新,这可以通过当前年份的某种系统调用+未来 X 年的列表的其余部分动态生成。

我不确定如何让变量编辑 XML,任何见解都值得

澄清: 我编辑了 Res>Values>strings.xml 以获取年份列表。我在列表项中手动输入年份,

它将是: 2011 年列表项 2012 年列表项 list-item 2013.......

但在 2012 年,较早的年份将不相关,因此我需要重新生成此字符串数组。有没有办法让输入的年份成为变量?

比如: 列表项年份 列表项 YEAR+1 list-item YEAR+2

和 java 中的一个函数会让列表知道这些值到底是什么,或者其他什么

I need a dynamic string-array. All the examples I have seen show hard-coded string-arrays. Basically I have a list of years for the user to select. The years are hard coded, but unfortunately current year will not be a relevant choice next year. I don't want to push updates for this in the future this could be dynamically generated with some kind of system call of the current year + X amount of years in the future for the rest of the list.

I am not sure how to make variables edit the XML, any insight appreciated

Clarification:
I have edited the Res>Values>strings.xml to have a list of years. I manually typed years in the list-item,

it would be:
list-item 2011
list-item 2012
list-item 2013..........

but in the year 2012, older years will not be relevant so I would need this string array regenerated. Is there a way to make the typed years be variables?

such as it would be:
list-item YEAR
list-item YEAR+1
list-item YEAR+2

and a function in the java will let the list know what those values really are, or something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-11-06 02:52:23

我不确定问题是什么。也许您不确定如何在 Java 中执行您之前在 XML 中执行的操作?

创建一个包含年份的 ArrayList 很简单。这可以通过使用 ArrayAdapter 支持 ListViewSpinner

ArrayList<Integer> years = new ArrayList<Integer>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1990; i <= thisYear; i++) {
    years.add(i);
}

I'm not sure what the problem is. Perhaps you're not sure how to do in Java something that you were previously doing in XML?

Creating an ArrayList of years is simple. This can back a ListView or a Spinner by using an ArrayAdapter.

ArrayList<Integer> years = new ArrayList<Integer>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1990; i <= thisYear; i++) {
    years.add(i);
}
牵你的手,一向走下去 2024-11-06 02:52:23

一旦你编译了 apk 并对其进行了签名,它就被密封了。此时无法编辑 XML。您的答案将是使用许多 Java 日期函数之一,这些函数根据已经理解的日历日期检索年份。

如果您有示例,请在此处发布

Once you compile you apk and it's signed, it's sealed. There is no way to edit XML at that point. Your answer will be to use one of the many java date functions that retrieves the year(s) based on already understood calendar dates.

If you have an example post it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文