Android 和资源的存储/加载首选项 - 如何实现一致性?
我正在编写一个应用程序,需要一些帮助来一致地存储和加载与某些资源相关的首选项。让我举个例子。
假设我有 10 个旋转器。它们都具有相同的功能但不同的含义。因为它们具有相同的功能,所以我可以将它们绑定到相同的 onItemSelectedListener
,然后该监听器将处理选定的值。
那么,如果我还想存储每个微调器的首选项怎么办?例如,当选择微调器值时,我将存储键“微调器SOMETHING”= SOME_DATA。
问题是 - 这个某事应该是什么?侦听器具有以下签名:
public void onItemSelected(AdapterView Parent, View v, int position, long id)
位置和 id 在这里没有帮助,因为它们是相对于每个微调器设置的。
然而,父级(在本例中是微调器本身)应该拥有我需要的所有信息。我尝试使用parent.getId()来获取每个微调器的资源ID,并使用关键的“微调器ID”存储首选项,但显然,当您向应用程序中添加更多资源时,这些资源ID会发生变化,因此我的设置将被重置,因为键改变。
我应该在这里做什么?我似乎找不到某种简单的parent.getName() 函数来返回每个微调器的一致名称。
谢谢。
I'm writing an application and need some help with consistently storing and loading preferences related to certain resources. Let me give an example.
Suppose I have 10 spinners. They all have the same functionality but different meaning. Because they have the same functionality, I can bind them to the same onItemSelectedListener
, which will then deal with the selected value.
Well, what if I also want to store the preferences for each spinner? For example, when a spinner value is selected, I'd store a key "spinner SOMETHING" = SOME_DATA.
The problem is - what should this SOMETHING be? The listener has the following signature:
public void onItemSelected(AdapterView parent, View v, int position, long id)
The position and id are not helpful here as they're set relative to each spinner.
However, the parent, which in this case is the spinner itself, should have all the information I need. I tried using parent.getId() to get a resource ID of each spinner and store the preference using a key "spinner ID" but apparently these resource IDs change when you add more resources into your app, so my settings would get reset because the keys change.
What should I do here? I can't seem to find a simple parent.getName() function of some sort that would return me a consistent name of each spinner.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getId
是我所知道的唯一对视图有唯一引用的东西,但正如你所说,视图在应用程序的下一个版本中可能不具有相同的 Id。开箱即用的最佳人类可读标识符是 getPrompt,它会简单地向您显示您设置为旋转器提示的内容。它应该是一个好的标识符,但如果您手动更改提示文本,它当然也容易损坏。
那么,最可靠的方法是在每个视图包含的通用容器 -
tag
中指定一个标识符。如果您已经将该标签用于其他用途,则 1.6+ API 包含指定多个标签并通过索引访问它们的功能。如果您需要支持较旧的 API 版本,您始终可以将标记设置为包含标识符和原始标记的包装对象。
The
getId
is the only thing I know of that has a unique reference to the View, but as you say, a view might not have the same Id in the next build of your app.The best human-readable identifier available out of the box would be
getPrompt
, which would simply show you whatever you have set as the spinners prompt. It should be an OK identifier, but it is of course also prone to breakage if you manually change your prompt texts.The most break-proof way, then, is to specify an identifier in the general container -
tag
- that every View contains.If you're already using the tag for something else, the 1.6+ APIs include the ability to specify several tags, and access them via index. If you need to support older API versions, you could always set the tag to a wrapper object that contains both the identifier, and your original tag.