Creating screens and underlying data access layer dynamically on Android

爷,独闯天下 提交于 2020-01-14 03:22:05

问题


I want to allow the user to create "custom screens" dynamically by selecting from a existing set of fields at runtime i.e., everything should be done on App on the phone.

For example - existing set of fields = {name, location, picture, age}.

User A wants to create a new screen with fields name, location and age.

and User B wants to have a screen with only name and picture.

These screens should be persistent and should be able to save, query and edit information in a local database on the phone.

Any help about how to achieve this in Android will be appreciated.

Thanks.


回答1:


You will want to create these views dynamically in Java (not using layouts). I would put together some sort of XML schema, and then save it either in the DB or on the device. Then you could do things such as:

//pseudo-code
while (xmlDoc isn't empty) {
View v = null;
if (XML says to create a text view) {
    v = new TextView(this);
    ...
}
else if (XML says to create an ImageView) {
    v = new ImageView(this);
    ...
}
add v to the LinearLayout or whatever type of root view will be passed to setContentView().



回答2:


Create a PreferenceScreen and add some boolean preferences with the names of your fields. So, every user can configure which fields to show in your data activity.



来源:https://stackoverflow.com/questions/5461191/creating-screens-and-underlying-data-access-layer-dynamically-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!