【发布时间】:2016-10-14 12:48:40
【问题描述】:
我正在尝试用 Retrofit 映射到 2 个对象的内容填充 GridView。
我已经让改造部分工作了,并且我通过它成功地接收到我需要的数据。
但是,在这种情况下,我不确定如何填写我的GridView,因为我有混合数据类型,而且我几乎可以肯定我需要一个自定义适配器。
本来想根据ArrayAdapter做一个自定义的适配器,但是不知道怎么实现,网上搜索了一下,还是不太明白。
我的GridView 目前非常基本(老实说,我不确定它是否具有所有必需的属性),但它是:
<GridView
android:id="@+id/gv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</GridView>
我的类通过 Retrofit 将其数据映射到它们,如下所示:
NewsAPI.java:
public class NewsAPI {
String status;
String source;
ArrayList<Articles> articles;
public NewsAPI(String status, String source, ArrayList<Articles> articles) {
this.status = status;
this.source = source;
this.articles = articles;
}
}
Articles.java:
public class Articles {
String title;
String description;
public Articles(String title, String description) {
this.title = title;
this.description = description;
}
}
提前致谢!
编辑:这是我在通过 Retrofit 解析之前拥有的原始 JSON 数据的示例。
{
"status": "ok",
"source": "techcrunch",
"sortBy": "top",
-"articles": [
-{
"author": "Darrell Etherington",
"title": "Six new Sun and Moon Pokémon evolutions revealed",
"description": "Pokémon Sun and Moon's release for 3DS rapidly approaches, and the Pokémon Company is unveiling eight new Pokémon unique to the new games, including six..",
"url": "http://social.techcrunch.com/2016/10/14/six-new-sun-and-moon-pokemon-evolutions-revealed/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/poke-compressor.gif?w=680&h=400&crop=1",
"publishedAt": "2016-10-14T14:29:12Z"
},
-{
"author": "Brian Heater",
"title": "That Nike-branded Apple Watch arrives October 28",
"description": "Maybe you’ve been waiting for the Nike+ version to arrive before running out to buy the Apple Watch Series 2. Because, you know, life is a marathon, not a..",
"url": "http://social.techcrunch.com/2016/10/14/nike-apple-watch/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/09/img_8919.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-14T13:33:25Z"
},
-{
"author": "Darrell Etherington",
"title": "Iron Man offers to voice Mark Zuckerberg’s real-life Jarvis AI",
"description": "Facebook CEO Mark Zuckerberg sets an ambitious goal every year for himself, and this year it was to create an AI assistant for his home, modelled in part on..",
"url": "http://social.techcrunch.com/2016/10/14/iron-man-offers-to-voice-mark-zuckerbergs-real-life-jarvis-ai/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/91kwg8.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-14T12:06:22Z"
},
-{
"author": "Natasha Lomas",
"title": "Instagram now has an app for Windows 10 tablets",
"description": "Instagram has expanded its app availability to users of tablets running Microsoft's Windows 10. The Facebook-owned, visual content social sharing platform..",
"url": "http://social.techcrunch.com/2016/10/14/instagram-now-has-an-app-for-windows-10-tablets/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/screen-shot-2016-10-14-at-12-49-56-pm.png?w=622&h=400&crop=1",
"publishedAt": "2016-10-14T11:10:47Z"
},
-{
"author": "Ingrid Lunden",
"title": "Spotify co-founder Martin Lorentzon steps down as chairman, CEO Daniel Ek steps up",
"description": "A changing of the guard is underway at Spotify, the streaming music service with 40 million paying users and over 100 million overall that competes with the..",
"url": "http://social.techcrunch.com/2016/10/14/spotify-co-founder-martin-lorentzon-steps-down-as-chairman-ceo-daniel-ek-steps-up/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2014/05/screen-shot-2014-05-21-at-11-19-55-am.png?w=764&h=364&crop=1",
"publishedAt": "2016-10-14T10:32:48Z"
},
-{
"author": "Natasha Lomas",
"title": "Softbank and Saudi Arabia’s PIF planning $100BN tech fund",
"description": "Softbank has announced it is creating a new global tech investment fund, seeded with $25 billion of its own money. The fund, which will be London, UK based --..",
"url": "http://social.techcrunch.com/2016/10/14/softbank-and-saudi-arabias-pif-planning-100bn-tech-fund/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2015/01/softbank-img.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-14T08:33:08Z"
},
-{
"author": "Sarah Buhr",
"title": "GM’s car sharing company Maven hits the streets of San Francisco",
"description": "Maven, a car sharing company out of General Motors, is driving its way into San Francisco today. The announcement made late Thursday evening is one part of a..",
"url": "http://social.techcrunch.com/2016/10/13/gms-one-way-ride-startup-maven-hits-the-streets-of-san-francisco/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/01/gmmaven02.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-14T04:00:54Z"
},
-{
"author": "Khaled \"Tito\" Hamze",
"title": "Crunch Report | Soylent Bars Are Getting Recalled",
"description": "Hyperloop One raises $50 million from DP World Group of Dubai, Soylent Bars are being recalled, Silicon Valley elites donate to controversial Prop Q,..",
"url": "http://social.techcrunch.com/2016/10/13/crunch-report-soylent-bars-are-getting-recalled/",
"urlToImage": "https://img.vidible.tv/prod/2016-10/13/580007b7869ea9143336a6ec_o_U_v1.jpg?w=764&h=400",
"publishedAt": "2016-10-14T03:00:40Z"
},
-{
"author": "Rudina Seseri",
"title": "The AI disruption wave",
"description": "Information technology evolves through disruption waves. First the computer, then the web and eventually social networks and smartphones all had the power to..",
"url": "http://social.techcrunch.com/2016/10/13/the-ai-disruption-wave/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/gettyimages-4964795042560x-80.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-14T03:00:39Z"
},
-{
"author": "John Mannes",
"title": "Returnly raises $3.2M to immediately refund your money after an online return",
"description": "Returning a product you bought online is inherently a statement of dislike, but smart retailers are quickly learning that they're far better off if they..",
"url": "http://social.techcrunch.com/2016/10/13/returnly-raises-3-2m-to-immediately-refund-your-money-after-an-online-return/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2016/10/gettyimages-495813985.jpg?w=764&h=400&crop=1",
"publishedAt": "2016-10-13T23:45:18Z"
}
]
}
【问题讨论】:
-
您使用改造从两个不同的 API 加载文章并将它们合并到 NewsAPI 中,并希望在您的 GridView 中将它们一起显示在同一级别?我做对了吗?
-
@MohsenMirhoseiniArgi 不,我从同一个 API 加载 JSON 数据,但文章本身是 NewsAPI 对象内部的一个数组。我在我的问题中添加了 JSON 数据的示例。
标签: java android gridview android-adapter android-gridview