I'm new in Android Programming. I have a class with data from json on Multilevel ExpandableListview. I want to show the DivisionName value into first row(parent) of list, SubDivisionName value into second row(child) and all of Vehicle values into third row(grandchild). So, this is the json data
[
{
"DivisionID": "2c0e9dc1-a6a7",
"DivisionName": "Tyuio",
"SubDivision": [
{
"SubDivisionID": "70c3ac53-eec6",
"SubDivisionName": "FM2222",
"Vehicle": [
{
"Nopol": "00571564",
"LastUpdate": "Oct 10 2010 10:10AM",
"LastSpeed": 0,
"LastLon": 106.82176
"Location": "KNOWHERE"
},
{
"Nopol": "352848020936627",
"LastUpdate": "Oct 10 2010 10:10AM",
"LastSpeed": 0,
"LastLon": 10124.228
"Location": "KNOWHERE2"
}
]
}
]
}
]
I'm try to populate it into ArrayList. I've been able to parse a json data. I want to through json to list. This is the Multilevel expandableListview code
private static final String DIV_NAME= "DivisionName";
private static final String SUBDIV_NAME = "SubDivisionName";
private static final String TAG_NOPOL = "Nopol";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// third row (subchild-list)
ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
mItemListArray.add(new ItemList(TAG_NOPOL, TAG_LOCATION,TAG_LONG));
// second row (child-list)
pSubItemArrayList=new ArrayList<SubCategory>();
pSubItemArrayList.add(new SubCategory(SUBDIV_NAME, mItemListArray));
// first row (parent-list)
pProductArrayList=new ArrayList<Product>();
pProductArrayList.add(new Product(DIV_NAME, pSubItemArrayList));
I want to show tyuio into first row of list(parent), FM222 into second row of list(child), and all of vehicle values into third row(grandchild). These JSON data is dynamic. Is there a way to convert json into this arrayList? i have seen many tutorials but those wasn't helpful for me. I appreciate any help. Thanks in advance
You will need to cast the response to a list.
I would recommend to do the following:
Create a response class:
public class DivisionResponse {
@SerializedName("DivisionID")
public String divisionId;
@SerializedName("DivisionName")
public String divisionName;
@SerializedName("DivisionName")
public List< SubDivision> subdivision;
public class Subdivision{
@SerializedName("SubDivisionId")
public String SubDivisionId;
@SerializedName("SubDivisionName")
public String SubDivisionName;
@SerializedName("Vehicle")
public List<Vehicle> SubDivisionName;
public class Vehicle{
@SerializedName("Nopol")
public String nopol;
@SerializedName("LastUpdate")
public String LastUpdate;
@SerializedName("LastSpeed")
public String LastSpeed;
@SerializedName("LastLon")
public String LastLon;
@SerializedName("Location")
public String Location;
}
}
}
Then you would use gson to cast the response to this class. When you receive that object or response convert it to text.
List<DivisionResponse> response = gson.fromJson(theonjecttext, new TypeToken<List<DivisionResponse>>() {}.getType());
that should create a list with all the data you need.
Make sure gson is added to the dependencies of gradle
compile 'com.google.code.gson:gson:2.2.4'
This code is specific to your response provided.
The response class could be independent classes instead of inner classes. Its up to you.
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.