How to convert JSONArray to ListView?

后端 未结 4 1437
无人及你
无人及你 2021-01-03 07:44

I have a code which does following -

  1. Connect to a web service via HttpClient to PHP file
  2. Returns a result from an SQL query
  3. Returns format i
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 08:08

    you could take a look at this webpage that has an example to do just what you are looking for: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

    Basically, inside your for loop where you are printing the names, you should be loading an array list containing the values you would like to insert to the list later. Notice that the example uses the following:

    ArrayList< HashMap < String, String > > mylist = new ArrayList < HashMap < String, String > > ();

    And you have an integer and a string to add to the structure so you could simply turn that ID into a String and problem solved. Afterwards you can use a list adapter without the need of creating a separate class:

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "name", "id" }, new int[] { R.id.item_title, R.id.item_subtitle });

    Where "name" and "id" will be the keys in your map for the name and id values returned by json and item_title, item_subtitle the views to "adapt" the text on.

    Hope I was clear enough, take a look at the example anyway its pretty straightforward.

提交回复
热议问题