Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android doesn't have a ready-made adapter to populate a <code>ListView</code> with a JSON array, like it does for populating a <code>ListView</code> with database records.</p> <p>I recommend getting comfortable with populating a Java data structure of your choice with the JSON data, and getting comfortable with working with <code>ListViews</code>, populating them from different collections, with different custom rows.</p> <p>Here's a simple example of populating a <code>List</code> from a JSON array, and then using the <code>List</code> to populate a <code>ListView</code>.</p> <pre><code>import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { String jsonInput = "[\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\",\"ten\"]"; JSONArray jsonArray = new JSONArray(jsonInput); int length = jsonArray.length(); List&lt;String&gt; listContents = new ArrayList&lt;String&gt;(length); for (int i = 0; i &lt; length; i++) { listContents.add(jsonArray.getString(i)); } ListView myListView = (ListView) findViewById(R.id.my_list); myListView.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, listContents)); } catch (Exception e) { // this is just an example } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload