我有一个想要解析的JSON文件。对于JSON文件中的每个条目,我希望显示一个ImageButton (这将是一个图标)和一个TextView (一个标题)。我想知道做这件事最好的方法是什么..
我不想创建一个列表视图..它更像是一个网格视图。我想每行2-3个图标(所以2-3列)有点像苹果的书架
我从..开始。
json = JSONfunctions.getJSONfromSDCard("/sdcard link to the file") //this gets the json file
JSONArray entries = json.getJSONArray("entries");
ArrayList<String> alTitle = new ArrayList<String>();
for(int i=0;i<entries.length();i++){
JSONObject e = entries.getJSONObject(i);
..//create arraylist to store entries?
//alTitle.add(e.getString("title"));
}
//create ImageButton and TextView?发布于 2011-08-01 04:13:00
您可以在此活动中使用listview或ListActivity。然后使用列表适配器为每个列表项创建布局。
SDK中提供了一些示例,可以帮助您快速入门。无论您在PC上安装此软件的位置,您都可以在android-sdk文件夹中找到它们。
互联网上也有很多教程,所以我不能一一列出,但这里有几个可以让你入门。这些是谷歌最热门的搜索结果。
Android Series: Custom ListView items and adapters
Android: Dealing with ListActivities, customized ListAdapters and custom-designed items
更新
抱歉,我错过了使用GridView的规范。officaial GridView documentation包含一个完整的示例,同样的思想也适用于使用适配器,您可以在Hello Views section of the official documentation中比较不同类型的视图
这是一个完整的例子,你需要在你的适配器中添加任何额外的控件。同样,sdk本身也有一些示例,这些示例对于Android开发人员来说是无价的。
发布于 2011-08-01 03:57:54
我建议你研究一下Android的GSON库。它应该比SDK中的org.json快得多,也更容易使用。我还遇到了一些关于股票json库的bug。
基本上,它添加了一个类似于Java的可序列化的接口。
http://code.google.com/p/google-gson/
发布于 2011-08-01 04:41:39
您可以解析JSON,创建一个ArrayList,然后将该ArrayList传递给ArrayAdapter。
使用这里提到的ArrayAdapter的自定义实现android, how to add ListView items from XML?
创建一个带有图标和TextView的布局XML,并在ArraAdapter实现的getView方法中对其进行压缩。填充值,就完成了。
https://stackoverflow.com/questions/6891593
复制相似问题