是否有可能使用preferenceActivity使用SQLite,而不是RES / XML? [英] Is it Possible to Use PreferenceActivity with SQLite instead of res/xml?

查看:106
本文介绍了是否有可能使用preferenceActivity使用SQLite,而不是RES / XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

preferenceActivity 是其与Android的 RES / XML 紧密集成。所有你需要做的实现自我管理preference读取/保存的魔力,以及用户界面,就是定义:

The beauty of PreferenceActivity is its tight integration with Android's res/xml. All you need to do achieve the magic of self-managed preference reading/saving, along with the UI, is define:

public class MyPreferenceActivity extends PreferenceActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
  }
}

和定义任何< preferenceScreen> 结构,你需要在XML文件(S)

And define any <PreferenceScreen> structure you need in XML file(s).

但是,这也似乎是它的弱点:它是如此紧密集成,我不知道是否有可能使用preferenceActivity使用SQLite(更有​​条理preference管理)的没有的重造,而(即再从头开始编写一个完整的preferenceActivity)。

But this also seems to be its weakness: It is so tightly integrated, that I have no idea whether it is possible to use PreferenceActivity with SQLite (for a more structured preference management), without re-inventing the while (i.e. re-writing an entire "PreferenceActivity" from scratch).

例如,使用<一个href="http://stackoverflow.com/questions/6962942/what-method-is-called-when-$p$pferenceactivity-saves-values">OnShared$p$pferenceChangeListener可以提供一个网关,使用preferenceActivity使用SQLite,但它仍然需要 RES / XML 定义要到位 - 因此,其实我们仍然由<约束HREF =htt​​p://stackoverflow.com/questions/7251020/nested-keying-in-shared$p$pferences>的RES / XML的方法的限制。

For example, using OnSharedPreferenceChangeListener may provide a gateway to using PreferenceActivity with SQLite, but it still requires the res/xml definitions to be in place - so really we are still constrained by the limitations of the res/xml method.

有没有办法来吃了蛋糕,并把它太?即使用preferenceActivity在同一 SQLite的纾缓 RES / XML 的。

Is there a way to "eat the cake and have it too"? i.e. Use PreferenceActivity with SQLite in the same ease as res/xml's.

推荐答案

有趣的问题。

简短的回答:AFAIK,有没有办法可以使用​​SQLite与preferenceActivity没有这样做,因为它的目的不是以这种方式工作显著定制

Short answer: AFAIK, there is no way you can use SQLite with PreferenceActivity without doing significant customization as it is not designed to work in that manner.

这里的关键是,为什么你真正需要的SQLite管理preferences? SQLite的应该作为一种规则从未被用于较低的数据,可以无需关系结构来管理。对于istance,它非常有意义使用SQLite当你有类似的数据的多个实例,就像在一个表中的行。

The point here is why do you actually need SQLite for managing preferences? SQLite should as-a-rule never be used for lesser data that can be managed without requiring relational structure. For istance, it makes perfect sense to use SQLite when you have multiple instances of similar data like rows in a table.

在情况preferences,我找不出任何这样的实例。此外SQLite的命中相比SP上的应用程序的性能。明智地做出您的选择。

In case of Preferences, I cannot figure any such instances. Moreover SQLite hits the performance of the application compared to SP. Make your choices wisely.

更新: 如果你有多个preferences就像上面提到的,你可以使用SQLite和SP的组合问题。你不能使用SQLite取代SP肯定。有什么可以做,虽然是你需要保持一个独特的密钥这将成为该表的主键,然后在的preferenceActivity的onPause需要火插入/更新查询中的SQLite表。你必须要小心,确保正确的SP都显示,因此在preferenceActivity onResume你需要能够激发获取查询的唯一密钥,并设置相应的SP。

Update: In case you have multiple Preferences like in the question mentioned above you can use a combination of SQLite and SP. You cannot replace SP with SQLite for sure. What can be done though is that you need to keep a unique key which would become the primary key of the table and then in onPause of the PreferenceActivity you need to fire insert/update query in the SQLite table. You need to be careful and ensure correct SP are displayed and hence in onResume of PreferenceActivity you need to be able to fire fetch query with the unique key and set the SP accordingly.

这篇关于是否有可能使用preferenceActivity使用SQLite,而不是RES / XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆