Without further ado, let’s take a look at today’s results:

Effect 1 (Ordinary recursion) Effect TWO (Actual combat of the project)

Or the old routine, “from simple to luxury”, first to a simple understanding of what is recursion!

Introduction to the recursive

Recursion is calling yourself if the condition is satisfied, until the condition is not satisfied!

Baidu encyclopedia

Let’s start with a simple example:

public static void main(String[] args) {
        int test = 10;
        recursion(test);
 }
 
 / / recursion
 public static void recursion(int test) {
        test--;
        if (test > 5) { System.out.println(test); recursion(test); }}Copy the code

The running results are as follows:

9
8
7
6
Copy the code

If (test > 5) the test will continue until test < 5.

It’s a little bit like the for loop, but the recursion from if you play it to perfection is also called “High, high, high for loop.”

So how do I write this recursion for?

        int test = 10; . System.out.println("=======for========");

        for (int i = test; i > 0;) {
            i--;
            if (i > 5) { System.out.println(i); }}Copy the code

The running results are as follows:

9
8
7
6
Copy the code

Auxiliary graph:

The data analysis

Let’s start with today’s data:

(part)

{
            "code": 0."msg": "success"."data": [{"name": "Department of Safety and Environmental Protection"."childs": [{"name": "Fire Brigade"."id": "2351"."childs": [{"name": "Class"."id": "2919"
                                },
                                {
                                    "name": "Class 2"."id": "2920"
                                },
                                {
                                    "id": "2351"."name": "All"}]}, {"name": "Safety Management Division"."id": "2349"
                        },
                        {
                            "name": "Environmental Management Division"."id": "2350"
                        },
                        {
                            "name": "Safety Inspection Division"."id": "2352"
                        },
                        {
                            "id": "2331"."name": "All"}]."id": "2331"
                },
                {
                    "name": Kam Yuen Chemical Co., LTD.."childs": [{"name": "Thermal power branch"."id": "2550"."childs": [{"name": "Thermal engine section"."id": "2564"."childs": [{"name": "Run one shift"."id": "2868"
                                        },
                                        {
                                            "name": "Run shift three."."id": "2870"
                                        },
                                        {
                                            "name": "Run shift four."."id": "2871"
                                        },
                                        {
                                            "name": "The day"."id": "2872"
                                        },
                                        {
                                            "name": "Run Shift two"."id": "2869"
                                        },
                                        {
                                            "id": "2564"."name": "All"}]}, {"name": "Electrical section"."id": "2563". }, {"name": "Desulfurization section"."id": "2566". }, {"name": "Combustion and transportation section"."id": "2561". }, {"name": "Maintenance section"."id": "2565". }, {"name": "Common section"."id": "2562". }, {"id": "2550"."name": "All"}]}, {"name": "Safety and Environmental Protection Division"."id": "2544". }, {"name": "Raw material branch"."id": "2551". }, {"name": Calcium carbide branch."id": "2552". }, {"name": "Equipment Management Division"."id": "2549". },]"id": "2368"}... ] }Copy the code

Don’t you want to see this? Take a look at the pictures:

The same color is the same grade

Ok, the data is so simple, let’s understand how the data is parsed!

JSON parsing

JSON is the most primitive approach I’ve taken

JSON common methods instructions
JSONObject.getJSONArray( String key) Parsing array
JSONObject(String data) Analytical object
getString(String key) Parse the String in the object
getInt(String key) Parse an Int in an object
getBoolean(String key) Parse the Boolean in the object
getLong(String key) Parse longs in objects
getDouble(String key) Resolves a Double in an object

JSON is still very simple, do not understand the students have a look at this one feels quite long, should also be good!

Analytical data

Let’s start with the code:

val jsonData = JSONObject(RecursionUtil.data)
val dataList = jsonData.getJSONArray("data")


 // The simplest solution
    private fun buildRecursion1(dataList: JSONArray) {
        / / loop
        repeat(dataList.length()) {
            val name = dataList.getJSONObject(it).get("name")
            val id = dataList.getJSONObject(it).get("id")
            textView.append("$name\t$id\n")}}Copy the code
  • Recursionutil. data is the above data

This is pretty simple, parsing the first layer of data, the yellow layer

Take a look at the results:

Recursive Bean class data (Effect one)

Methods a

Each value is recursed in order

Let’s look at the code:

        val jsonData = JSONObject(RecursionUtil.data)
        val dataList = jsonData.getJSONArray("data")

    1 / / recursive
    private fun recursion1(dataList: JSONArray) {
        repeat(dataList.length()) {
            val name = dataList.getJSONObject(it).get("name")
            val id = dataList.getJSONObject(it).get("id")
            textView.append("$name\t$id\n")

            try {
                recursion1(dataList.getJSONObject(it).getJSONArray("childs"))}catch (e: Exception) {
                Log.i("SZJ has no data", e.message ? :"No CHlids data.")}}}Copy the code

Analysis:

Here again, it’s a little easier, if childs has data, just keep getting it recursively!

So this recursion is one level of recursion and then the next level of recursion

Take a look at the results:

Way 2

Layer by layer recursion

Let’s start with the code:


val jsonData = JSONObject(RecursionUtil.data)
val dataList = jsonData.getJSONArray("data")
        
        
  2 / / recursive
    private fun recursion2(dataList: JSONArray) {
        repeat(dataList.length()) {
            val name = dataList.getJSONObject(it).get("name")
            val id = dataList.getJSONObject(it).get("id")
            textView.append("$name\t$id\n")
        }

        repeat(dataList.length()) {
            try {
                recursion2(dataList.getJSONObject(it).getJSONArray("childs"))}catch (e: Exception) {
                Log.i("SZJ has no data", e.message ? :"No CHlids data.")}}}Copy the code

And the idea is very simple, the knowledge goes to the first level of recursion, when the recursion is done and then goes to the second level of recursion

Illustration:

After you recurse one level, you recurse the next level

Take a look at the results:

Effects of two

Take another look at the rendering:

Analysis of ideas:

  • Use Dialog inside RecyclerView to pop up data in Json at the bottom
  • Then when clicking on one layer of data, get the data from the next layer and pop up (until there is no data).

Dialog IS a Dialog fragment I use my own encapsulation, you can first understand

This code involves many classes, I will not copy, you download the code to see ~

The complete code

Original is not easy, your praise is my support!