I wanted to populate a nested ArrayList.
Below is the JSON response from the server. Now I want sectionName as the headerView and sectionItems as the Child view of that header. So That, this layout will look like categorized items.
I have researched the same but couldn't able to find any easy and efficient solution. I don't want to create a separate flatten list because that would cost me time.
Now, How can I achieve this layout.
PS: I don't want to use any external library.
"data": [
{
"sectionName": "Fruits",
"sectionItems": [
{
"id": "123",
"name": "Apple",
"image": "url",
"price": "120",
"quntity": "600gm"
},
{
"id": "234",
"name": "Orange",
"image": "url",
"price": "340",
"quntity": "2kg"
},
{
"id": "334",
"name": "Banana",
"image": "url",
"price": "340",
"quntity": "2kg"
}
]
},
{
"sectionName": "Paints",
"sectionItems": [
{
"id": "345",
"name": "Red",
"image": "url",
"price": "120",
"quntity": "1l"
},
{
"id": "346",
"name": "White",
"image": "url",
"price": "120",
"quntity": "1l"
},
{
"id": "347",
"name": "Purple",
"image": "url",
"price": "120",
"quntity": "1l"
}
]
}
EDIT: I have implemented a nested recycler view but couldn't able to render all the child element. there are more than 80 items on the nested list but I can see only the 18.
To answer your question, in my opinion the best idea here is to flatten the list. There are other possibilities but do they really fit the design of the RecyclerView? I don't think so. Some time ago I wrote a blog post to show how easy you can achieve sectioned RecyclerView:
https://brona.blog/2020/06/sectioned-recyclerview-in-three-steps/
Related
I'm writing a messenger like app. I already have a recyclerview for drawing messages list. But also, i need to draw some attachments inside of each message(like images, sound, files and so). The data strucutre of message is:
"id": 2,
"text": "Test",
"sendTime": "21:31 12.10.22",
"userId": 1,
"user": {
"id": 1,
"login": "geckon01",
"email": "geckon01#example.com",
"role": "host",
"lastOnline": "2022-10-23T11:03:57.6349417",
"avatarFileId": 0,
"avatarBase64": null
},
"attachments": [{
"id": 1,
"type": "Image",
"data": null,
"user": {
"id": 1,
"login": "geckon01",
"email": "geckon01#example.com",
"role": "host",
"lastOnline": "2022-10-23T11:03:57.6349417",
"avatarFileId": 0,
"avatarBase64": null
},
"file": {
"id": 1,
"directory": "graphics",
"fileName": "1c08af89-41f2-4f85-bb5a-3336a37e51bf.jpg",
"type": "Graphics",
"fileOwner": {
"id": 1,
"login": "geckon01",
"email": "geckon01#example.com",
"role": "host",
"lastOnline": "2022-10-23T11:03:57.6349417",
"avatarFileId": 0,
"avatarBase64": null
}
},
"messageId": 2
}]
}
So, i need to draw different view for attachemnt depend on its type. I'm thought to use fragments, but in my opinion it can be slow.
Complite message, should be like this, where item1,3,4 can be image,file,text and so.
You can use ConcatAdapter. The idea is you create different adapter for each view type. Here's a pseudo code
val concatAdapter = ConcatAdapter()
val headerAdapter = HeaderAdapter()
val messagesAdapter = MessagesAdapter()
val attachmentAdapter = AttachmentAdapter()
concatAdapter.add(headerAdapter)
concatAdapter.add(messagesAdapter)
concatAdapter.add(attachmentAdapter)
recyclerView.adapter = concatAdapter
This is my first android project (I'm a php developer) but i'm struggling to see how to take this example which uses a nested structure under data for the json array whereas my data is not double nested like this example.
The excample code I am trying to modify is from here https://github.com/datanapps/RetrofitKotlin and the key differences in the json are todo with the nesting.
Original
{
"page": 1,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 1,
"email": "george.bluth#reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
{
"id": 2,
"email": "james.smith#reqres.in",
"first_name": "James",
"last_name": "Smith",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
}
]
}
whereas mine is simply
[
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
},
{
"_id": "62f4cb4e131d9d00046a1b9c",
"device": "AndroidAPS-DexcomG6",
"date": 1660209967000,
"dateString": "2022-08-11T09:26:07.000Z",
"isValid": true,
"sgv": 133,
"direction": "Flat",
"type": "sgv",
"created_at": "2022-08-11T09:26:38.326Z",
"mills": 1660209998326
}
]
Specifically I want to query the SGV values, direction and date
Thanks
You "return type" of "method" in Retrofit "interface" will be:
List<MyModel>
And MyModel:
data class MyModel(val _id:String, val device:String,...)
I am a newbie to android. I am stuck at JSON parsing.
Following is my JSON, in this, the type of menu categories will be dynamic, and I want to arrange this JSON in sectioned recycler view.
Please help me out.
This is my JSON
{
"menu_items": {
"Plat": [{
"menu_name": "jambon à l'os",
"menu_cost": "18.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
},
{
"menu_name": "tacos",
"menu_cost": "6.00",
"menu_count": "1",
"category": "Plat",
"menu_tax": "5.50",
"code": "€"
}
],
"Entrée": [{
"menu_name": "avocat",
"menu_cost": "7.50",
"menu_count": "1",
"category": "Entrée",
"menu_tax": "5.50",
"code": "€"
}]
},
"order_data": {
"order_id": "278",
"order_number": "REF-5d5e6b86",
"created_at": "1566468998",
"instructions": "",
"fullname": "Rohan Chitnis",
"phonenumber": "7610771871",
"useraddress": "Brest, France",
"image_path": "https://restau-at-home.bzh/uploads/attachments/Jellyfish_2019_08_21_10_49_21.jpg",
"wp": "pune",
"code": "€",
"promocode": null,
"promo_value": null,
"promo_type": null
},
"order_details": {
"total_items": "3",
"order_total": "31.50"
},
"promo_applied": "0",
"error": {
"code": "13",
"status": "200",
"message": "Ok"
}
}
Check this out. Create a generic response class. Extend your classes from that class. To do this properly you can look at Java Generics and Inheritance topics if you are using Java. And then search about Sectioned RecyclerView in Android. Here is a library for this.
Here's the JSON where jsonObject key is dynamic :
{
"CH000032": [
{
"type": "event",
"details": {
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}
}
],
"CH000033": [
{
"type": "event",
"details": {
"programID": "EP10000132",
"programType": "EPISODE",
"title": "A Chic Bar in Ibiza",
"seriesTitle": "Two and a Half Men",
"seasonNumber": 12,
"epsiodeNumber": 2,
"year": "2014",
"rating": "TV-14",
"durationSec": 1260,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Comedy",
"Romance"
],
"description": "Alan has second thoughts about getting married when Walden has him sign a prenup.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_crop,h_405,w_270//l_Copyright_e3jt1k/v1508831090/2AndHalfmen_splkro.jpg"
}
}
]
}
I would like to parse this JSON. Please let me know how should the class
be written with #SerializedName annotation using Retrofit.
Note : CH000032, CH000033 etc are dynamic.
You can use Map<String, ModelClassName> in your model class for dynamic like below :-
public class Data {
#SerializedName("your_key")
#Expose
private Map<String, ModelClassName> result;
//....
}
this can help to parse dynamic key in retrofit.
What you are asking will take much work , instead a better approach will be to change the structure o json to something like this -
"data": [
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
},
{
"type": "event",
"programID": "MV10000032",
"programType": "MOVIE",
"title": "Titanic",
"year": "1997",
"rating": "PG-13",
"durationSec": 11640,
"startTimeSec": "",
"endTimeSec": "",
"language": "ENG",
"isHD": true,
"Genres": [
"Movies",
"Action"
],
"description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
"imageUrl": "http://res.cloudinary.com/dte07foms/image/upload/c_scale,h_405,w_270/l_Copyright_e3jt1k/v1508831090/Titanic_b0hqo0.jpg"
}]
I have a JSON array which looks like the following. I need to populate a listview using this array but I have another array inside the JSON (for the comments) which I hope to use to populate a linearlayout inside a list item using addview(). How can I design my list adapter, I need the comments to be clickable too.
[
{
"name": "Hopewell Mutanda",
"answer": "This is my answer",
"votes": 10,
"date": 20-06-2014,
"comments": [
{
"comment": "This is a great answer",
"name": "Gloria Nyakudya",
"date": 21-10-2014,
"vote": 20,
},
{
"comment": "This is the second great answer",
"name": "Pelagia Chirenga",
"date": 21-10-2014,
"vote": 10,
},
{
"comment": "This is the third great answer",
"name": "Pelagia Chirenga",
"date": 21-10-2014,
"vote": 0,
},
]
},
{
"name": "Tinashe Makaza",
"answer": "This is the second answer",
"votes": 10,
"date": 20-06-2014,
"comments": [
{
"comment": "This is a great answer",
"name": "Gloria Nyakudya",
"date": 21-10-2014,
"vote": 20,
},
{
"comment": "This is the second great answer",
"name": "Pelagia Chirenga",
"date": 21-10-2014,
"vote": 10,
},
{
"comment": "This is the third great answer",
"name": "Pelagia Chirenga",
"date": 21-10-2014,
"vote": 0,
},
]
},
]
This is the link to the image of something I hope to achieve. Every value in this layout is hard coded
http://encodable.com/uploaddemo/files/Screenshot_2014-12-02-23-55-33.png
You should have a look at ExpandableListView. This is the default two level ListView. Seems to me that fits your needs exactly.