我对此 Android 应用程序有非常具体的要求,我从服务器获取 JSON 文件,如下所示:
[
{"st":"f","t":"PDF","tbico":"107-widescreen.png","f":"PG033%20-%20Blue%20on%20Black.pdf"},
{"t":"Call","tbico":"BG092.png","dialNumBtn":"1231231234","st":"dialNumBtn"},
{"t":"Facebook Photos","tbico":"113navigation.png","fbPgID":"zoo"}
]
在此应用程序的预构建 iOS 版本中,布局将类似于 this link使用上面的 JSON ,我有一个任务是使用这个 JSON 并在 android 中以类似的方式显示它(使用 TabView),例如 this link 我的问题是:我如何创建不同的选项卡和 fragment Activity,根据上面的 json 进行 self 调整(例如:如果上面的 JSON 中多了一个对象,则在布局中再显示一个选项卡)?
这对我没有帮助:link ,我有解析 json 之类的东西,我需要有关如何显示 UI 元素的帮助
最佳答案
这里你需要一些严肃的 JSON 库,例如 Jackson .
创建你的基类(警告:伪代码,适应),这样你就可以:
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class MyBaseClass
{
@JsonProperty("t")
protected String title;
@JsonProperty("tbico")
protected final String iconName;
// etc
}
然后扩展MyBaseClass
以便为每个选项卡类型创建一个对象。
使用以下方法反序列化它:
// customize as needed, see javadoc
final ObjectMapper mapper = new ObjectMapper();
final List<MyBaseClass> l = mapper.readValue(theJson,
new TypeReference<List<MyBaseClass>>() {});
要么这样,要么遍历 JSON(使用 JsonNode,这非常适合)并一个接一个地创建选项卡。
关于java - Android动态TabView使用JSON配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17563698/