How to convert JSONArray to ListView?

后端 未结 4 1435
无人及你
无人及你 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:20

    If you just want to display a list of textViews you don't need to override anything, you can just add all of the items into an arrayList and use an arrayAdapter.

    Put a list view in your xml that is named android:list and then create your arrayAdapter with the textView you want to use.

    After that all you have to do is call setListAdapter(mArrayAdapter) and it should populate your list.

    ArrayList items = new ArrayList();
    for(int i=0; i < jArray.length() ; i++) {
        json_data = jArray.getJSONObject(i);
        int id=json_data.getInt("id");
        String name=json_data.getString("name");
        items.add(name);
        Log.d(name,"Output");
    }
    
    ArrayAdapter mArrayAdapter = new ArrayAdapter(this,  
               android.R.layout.simple_expandable_list_item_1, items));
    setListAdapter(mArrayAdapter)
    

    hope this helps!

提交回复
热议问题