如何从多个jsonarray获取数据并在listview上显示

时间:2016-05-25 07:52:13

标签: java android json

这是我的JSON文件

{
"data": [{
    "title": "Algebra",
    "data": [
        {
        "title":"Factoring Formulas",
        "link":"Alge/eqA01.html"
        },
        {
        "title":"Product Formulas",
        "link":"Alge/eqA02.html"
        },
        {
        "title":"Roots Formulas",
        "link":"Alge/eqA03.html"
        },
        {
        "title":"Powers Formulas",
        "link":"Alge/eqA04.html"
        },
        {
        "title":"Logarithm Formulas",
        "link":"Alge/eqA05.html"
        },
        {
        "title":"Useful Formulas",
        "link":"Alge/eqA06.html"
        },
        {
        "title":"Complex Formulas",
        "link":"Alge/eqA07.html"
        },
        {
        "title":"Binomial Formulas",
        "link":"Alge/eqA08.html"
        }
    ]
},{
    "title": "Geometry",
    "data": [
        {
        "title":"Cone",
        "link":"geo/eqG08.html"
        },
        {
        "title":"Cylinder",
        "link":"geo/eqG10.html"
        },
        {
        "title":"Isosceles Triangle",
        "link":"geo/eqG01.html"
        },
        {
        "title":"Equilateral Traingle",
        "link":"geo/eqG02.html"
        },
        {
        "title":"Square",
        "link":"geo/eqG03.html"
        },
        {
        "title":"Sphere",
        "link":"geo/eqG09.html"
        },
        {
        "title":"Rechtangle",
        "link":"geo/eqG04.html"
        },
        {
        "title":"Rhombus",
        "link":"geo/eqG05.html"
        },
        {
        "title":"Parallelogram",
        "link":"geo/eqG06.html"
        },
        {
        "title":"Trapezoid",
        "link":"geo/eqG07.html"
        }
    ]
},{
    "title": "Analytical Geometry",
    "data": [
        {
        "title":"2-D Coordinate System",
        "link":"Anageo/eqAG01.html"
        },
        {
        "title":"Circle",
        "link":"Anageo/eqAG02.html"
        },
        {
        "title":"Hyerbola",
        "link":"Anageo/eqAG03.html"
        },
        {
        "title":"Ellipse",
        "link":"Anageo/eqAG04.html"
        },
        {
        "title":"Parabola",
        "link":"Anageo/eqAG05.html"
        }
    ]
},{
    "title": "Derivative",
    "data": [
        {
        "title":"Limits Formulas",
        "link":"Deriv/eqD01.html"
        },
        {
        "title":"Properties of Derivative",
        "link":"Deriv/eqD02.html"
        },
        {
        "title":"General Derivative Formulas",
        "link":"Deriv/eqD03.html"
        },
        {
        "title":"Trigonometric Formulas",
        "link":"Deriv/eqD04.html"
        },
        {
        "title":"Inverse Trigonometric Functions",
        "link":"Deriv/eqD05.html"
        },
        {
        "title":"Hyperbolic Functions",
        "link":"Deriv/eqD06.html"
        },
        {
        "title":"Inverse Hyperbolic Functions",
        "link":"Deriv/eqD07.html"
        }
    ]
},{
    "title": "Integration",
    "data": [
        {
        "title":"Properties of Integration",
        "link":"Integ/eqI01.html"
        },
        {
        "title":"Integration of Rational Functions",
        "link":"Integ/eqI02.html"
        },
        {
        "title":"Integration of Trigonometic Functions",
        "link":"Integ/eqI03.html"
        },
        {
        "title":"Integration of Hyperbolic Function",
        "link":"Integ/eqI04.html"
        },
        {
        "title":"Integration of Exponential and Log Function",
        "link":"Integ/eqI05.html"
        }
    ]
},{
    "title": "Trigonometry",
    "data": [
        {
        "title":"Basic",
        "link":"Trign/eqT08.html"
        },
        {
        "title":"General",
        "link":"Trign/eqT01.html"
        },
        {
        "title":"Sine rule Cosine rule",
        "link":"Trign/eqT09.html"
        },
        {
        "title":"Table of angle",
        "link":"Trign/eqT02.html"
        },
        {
        "title":"Angle transformation",
        "link":"Trign/eqT05.html"
        },
        {
        "title":"Half/Double/Multiple angle",
        "link":"Trign/eqT04.html"
        },
        {
        "title":"Sum of functions",
        "link":"Trign/eqT05.html"
        },
        {
        "title":"Product of Functions",
        "link":"Trign/eqT06.html"
        },
        {
        "title":"Powers of Functions",
        "link":"Trign/eqT07.html"
        },
        {
        "title":"Euler's Formula",
        "link":"Trign/eqT10.html"
        },
        {
        "title":"Allied Angle Table",
        "link":"Trign/eqT11.html"
        },
        {
        "title":"Negative Angle Identites",
        "link":"Trign/eqT12.html"
        }
    ]
},{
    "title": "Laplace",
    "data": [
        {
        "title":"Properties of Laplace Tranform",
        "link":"Lapla/eqL01.html"
        },
        {
        "title":"Functions of Laplace Tranform",
        "link":"Lapla/eqL02.html"
        }
    ]
},{
    "title": "Fourier",
    "data": [
        {
        "title":"Fourier Series",
        "link":"Four/eqFour01.html"
        },
        {
        "title":"Fourier Transform Operations",
        "link":"Four/eqFour02.html"
        },
        {
        "title":"Table of Fourier Transform",
        "link":"Four/eqFour03.html"
        }
    ]
},{
    "title": "Series",
    "data": [
        {
        "title":"Arithmetic Series",
        "link":"Series/eqS01.html"
        },
        {
        "title":"Geometric Series",
        "link":"Series/eqS02.html"
        },
        {
        "title":"Finite Series",
        "link":"Series/eqS03.html"
        },
        {
        "title":"Binomial Series",
        "link":"Series/eqS04.html"
        },
        {
        "title":"Power Series Expansions",
        "link":"Series/eqS05.html"
        }
    ]
},{
    "title": "Numerical Methods",
    "data": [
        {
        "title":"Lagrange, Newton's Interpolation",
        "link":"Inter/eqIN01.html"
        },
        {
        "title":"Newton's Forward/backward Difference",
        "link":"Inter/eqIN02.html"
        },
        {
        "title":"Numerical Integration",
        "link":"Inter/eqIN03.html"
        },
        {
        "title":"Roots of Equation",
        "link":"Inter/eqIN04.html"
        }
    ]
},{
    "title": "Vector Calculas",
    "data": [
        {
        "title":"Vector Identities",
        "link":"VectCal/eqVC01.html"
        }
    ]
},{
    "title": "Probability",
    "data": [
        {
        "title":"Basic",
        "link":"Prob/eqP01.html"
        },
        {
        "title":"Expectation",
        "link":"Prob/eqP02.html"
        },
        {
        "title":"Variance",
        "link":"Prob/eqP03.html"
        },
        {
        "title":"Distributions",
        "link":"Prob/eqP04.html"
        },
        {
        "title":"Permutations",
        "link":"Prob/eqP05.html"
        },
        {
        "title":"Combinations",
        "link":"Prob/eqP06.html"
        }
    ]
},{
    "title": "Beta gamma",
    "data": [
        {
        "title":"Beta Functions",
        "link":"beta/eqBeta01.html"
        },
        {
        "title":"Gamma Functions",
        "link":"beta/eqBeta02.html"
        },
        {
        "title":"Beta-Gamma Functions",
        "link":"beta/eqBeta03.html"
        }
    ]
},{
    "title": "Z-Transform",
    "data": [
        {
        "title":"Properties of Z-Transform",
        "link":"zTrans/eqZT01.html"
        },
        {
        "title":"Common Pairs",
        "link":"zTrans/eqZT02.html"
        }
    ]
}]

}

这是MainActivity.java.I已通过intent传递JSONArray array1。

public static String str1 = null;
public String title2;
static final String TITLE = "title";
//public JSONObject object;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     ListView listview = (ListView) findViewById(R.id.listView1);


    StringBuffer str = new StringBuffer();
       try {
        BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open(
                "boardexam.json")));
        String temp;
        while ((temp = br.readLine()) != null)
            str.append(temp);
        br.close();
       } catch (IOException e) {
        e.printStackTrace();
      } 
      String readFile = str.toString();

     //String title1;
        try {

            JSONObject json = new JSONObject(readFile);
            JSONArray array = json.getJSONArray("data");

            ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
            int l = array.length();

            for(int i=0; i<l; i++) {
               JSONObject object  = array.getJSONObject(i);

               final JSONArray array1 = object.getJSONArray("data");



               //JSONOBject array2 = array1.get(i);
               HashMap<String,String> hashmap = new HashMap<String,String>();
               hashmap.put(TITLE,object.getString("title").toString());
              // hashmap.put("data", array1.toString());
               list.add(hashmap);

               Adapter1 arrayAdapter = new Adapter1(MainActivity.this,list);
               listview.setAdapter(arrayAdapter);
               listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {



                    Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                     Bundle b = new Bundle();
                    b.putString("data",array1.toString());

                    intent.putExtras(b);
                    startActivity(intent);
                }

               });

            }



        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

}

这是MainActivity2.java,我将获得JSONArray。

static final String TITLE = "title";
static final String URL = "url";
private String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);

    ListView listview = (ListView) findViewById(R.id.listView1);
    Intent intent = getIntent();
    try {
        String  string  = intent.getStringExtra("data");
        JSONArray jsonArray = new JSONArray(string);
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();


        int l = jsonArray.length();

         for(int i=0; i<l; i++)
         {

             JSONObject obj = array.getJSONObject(i);
             HashMap<String,String> hashmap = new HashMap<String,String>();
             hashmap.put(TITLE,obj.getString("title"));

             list.add(hashmap);
             Adapter1 arrayAdapter = new Adapter1(MainActivity2.this,list);
             listview.setAdapter(arrayAdapter);

       }
                } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

}

这是我的问题。当我在litview中显示这个jsonarray的对象时,它只显示jsonarray的最后一个对象。如何显示jsonarray的完整对象。

{
    "title":"Properties of Z-Transform",
    "link":"zTrans/eqZT01.html"
    },
    {
    "title":"Common Pairs",
    "link":"zTrans/eqZT02.html"
    }

3 个答案:

答案 0 :(得分:0)

请从MainActivity2

进行for循环
Adapter1 arrayAdapter = new Adapter1(MainActivity2.this,list);
listview.setAdapter(arrayAdapter);

同样的事情MainActivity1使得for for for for循环

 Adapter1 arrayAdapter = new Adapter1(MainActivity.this,list);
           listview.setAdapter(arrayAdapter);
           listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {



                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                 Bundle b = new Bundle();
                b.putString("data",array1.toString());

                intent.putExtras(b);
                startActivity(intent);
            }

           });

答案 1 :(得分:0)

尝试以下代码

String result= YOUR JSON STRING;

        try
        {
            JSONObject jsonObject = new JSONObject(result);

            JSONArray jsonArray = jsonObject.getJSONArray("data"); // data array

            for(int i=0;i<jsonArray.length();i++) // get all value 
            {
                JSONObject jsonObject2 = jsonArray.getJSONObject(i); // get every json object title and data

                String title = jsonObject2.getString("title");
                Log.e("title", title);

                JSONArray data = jsonObject2.getJSONArray("data");

                for(int j=0;j<data.length();j++)
                {
                    String title1 = data.getJSONObject(i).getString("title");
                    Log.e("title1", title1);
                    String link = data.getJSONObject(i).getString("link");
                    Log.e("link", link);
                }

            }
        }
        catch(Exception e)
        {

        }

答案 2 :(得分:0)

将json数组放入String并使用Intent传递给下一个活动,然后在下一个活动中解析它。