动态传递布局 ID

发布于 2024-12-02 11:44:54 字数 1919 浏览 4 评论 0原文

我想实现一个“最喜欢的列表”,假设我有一个空布局。在onCreate()处有super.onCreate(savedInstanceState); setContentView(R.layout.main); 然后我打开一个包含我最喜欢的动物的数据库,加载一些内容并向布局添加动态按钮。

数据库架构是这样的:
CREATE TABLE Animals(_id INTEGER PRIMARY KEY, name TEXT NOT NULL,layout TEXT NOT NULL)

数据库中有:

_id - - - - - - - - name - - - - - - - - - 布局
1 - - - - - - - - -- cat - - - - - - - - -- R.layout.cat
2 - - - - - - - - -- 狗 - - - - - - - - -- R.layout.dog
3 - - - - - - - - -- 海龟 - - - - - - - - R.layout.turtle

当然是layout/xml-files (R.layout.cat, R.layout .dog、R.layout.turtle) 存在。

然后你会看到三个按钮,分别写着“猫”、“狗”和“乌龟”。在OnClickListener 中,有以下内容:

    Intent intent = new Intent(MyFavorites.this, Animal.class);  
    Log.d ("onClick", button.getmyLayout());  
    // Shows either R.layout.cat, R.layout.dog or R.layout.turtle 
    // depending on what button was pressed. This works fine.
    intent.putExtra("myLayout", button.getmyLayout());    
    // closes the database
    myDbHelper.close();
    startActivity(intent);

Animal.class 可以显示所有的animal-xml-文件,因为它们都具有相同的功能。这就是为什么布局总是传递给同一个类的原因!

Animal.class:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final int myLayout = getIntent().getIntExtra("myLayout", R.layout.main);
            setContentView(myLayout);
        }

现在问题来了。我传递一个字符串而不是一个整数,在日志猫中有:

键 myLayout 需要整数,但值是 java.lang.String。返回默认值2130903044。

默认值是 R.layout.main,您可以在自动生成的 R.class 中找到它,它是十六进制数,十进制数是 2130903044。所以我确实理解错误消息。但我想不出另一种方法来解决这个问题。我正在考虑 R.class 中的 getters 和 setters,但当然它们会在下一个构建时被删除。

I want to implement a 'favorite list' and let's assume that I have got an empty layout. At onCreate() there is super.onCreate(savedInstanceState); setContentView(R.layout.main); and then I open a database with my favorite animals, load a few things and add dynamically buttons to the layout.

The database schema is this:

CREATE TABLE animals(_id INTEGER PRIMARY KEY, name TEXT NOT NULL, layout TEXT NOT NULL)

and in the database there is:

_id - - - - - - - - name - - - - - - - - - layout
1 - - - - - - - - -- cat - - - - - - - - -- R.layout.cat
2 - - - - - - - - -- dog - - - - - - - - -- R.layout.dog
3 - - - - - - - - -- turtle - - - - - - - - R.layout.turtle

Of course the layouts/xml-files (R.layout.cat, R.layout.dog, R.layout.turtle) exist.

So then you see three buttons saying cat, dog and turtle. In the OnClickListener there is the following:

    Intent intent = new Intent(MyFavorites.this, Animal.class);  
    Log.d ("onClick", button.getmyLayout());  
    // Shows either R.layout.cat, R.layout.dog or R.layout.turtle 
    // depending on what button was pressed. This works fine.
    intent.putExtra("myLayout", button.getmyLayout());    
    // closes the database
    myDbHelper.close();
    startActivity(intent);

The Animal.classcan show all animal-xml-files because they all have the same functionality. This is why the layout is always passed on to the same class!

The Animal.class:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final int myLayout = getIntent().getIntExtra("myLayout", R.layout.main);
            setContentView(myLayout);
        }

Now here is the problem. I pass on a String and not an Integer and in the log cat there is:

Key myLayout expected Integer but value was a java.lang.String. The default value 2130903044 was returned.

The default value is R.layout.main which you find in the autogenerated R.class as a hex number and in decimal it is 2130903044. So I do understand the error message. But I cannot think of another way to solve this problem. I was thinking about getters and setters in the R.class but of course they will be deleted at the next build.

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

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

发布评论

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

评论(2

2024-12-09 11:44:54

请参阅这个问题,我认为这就是您所需要的。基本上你可以通过使用字符串来获取资源...

int i = this.getResources().getIdentifier("cat", "layout", this.getPackageName());

并且 这里是 getIdentifier() 的文档。

Please see this question, I think it is what you need. Basically you can get the resource by using a string ...

int i = this.getResources().getIdentifier("cat", "layout", this.getPackageName());

And here is the documentation for getIdentifier().

你是我的挚爱i 2024-12-09 11:44:54

如果您的布局数量有限且合理(您定义合理的%),我建议使用
只是静态映射:

static Map<String,Integer> layouts = new HashMap<String,Integer>()
static {
   layouts.put("cat",R.layout.cat);
   ...
   layouts.put("unicorn",R.layout.unicorn);
}

这样您将获得编译时安全性,并且在运行时会更快。

If your amount of layouts is finite and reasonable (you define reasonable %) I would propose to use
just static map:

static Map<String,Integer> layouts = new HashMap<String,Integer>()
static {
   layouts.put("cat",R.layout.cat);
   ...
   layouts.put("unicorn",R.layout.unicorn);
}

This way you will get compile time safety, and it would be faster at the runtime.

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