动态传递布局 ID
我想实现一个“最喜欢的列表”,假设我有一个空布局。在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.class
can 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅这个问题,我认为这就是您所需要的。基本上你可以通过使用字符串来获取资源...
并且 这里是 getIdentifier() 的文档。
Please see this question, I think it is what you need. Basically you can get the resource by using a string ...
And here is the documentation for getIdentifier().
如果您的布局数量有限且合理(您定义合理的%),我建议使用
只是静态映射:
这样您将获得编译时安全性,并且在运行时会更快。
If your amount of layouts is finite and reasonable (you define reasonable %) I would propose to use
just static map:
This way you will get compile time safety, and it would be faster at the runtime.