Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Please find the sample code below : </p> <p>layout file : main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;Spinner android:id="@+id/cmbNames" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>layout : spinner_item.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Code : MainActivity.java</p> <pre><code>public class MainActivity extends Activity { private static final String NAME = "name"; //{"name":["abc","pqr","xyz"]} private String jsonNames = "{\"" + NAME + "\":" + "[\"abc\",\"pqr\",\"xyz\"]}"; private SimpleAdapter adapter; private Spinner cmbNames; private List&lt;HashMap&lt;String, String&gt;&gt; lstNames; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); populateSpinner(jsonNames); cmbNames = (Spinner) findViewById(R.id.cmbNames); adapter = new SimpleAdapter(this, lstNames, R.layout.spinner_item, new String[] { NAME }, new int[] { R.id.tvName }); cmbNames.setAdapter(adapter); } private void populateSpinner(String jsonString) { lstNames = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); try { JSONObject jsonObj = new JSONObject(jsonNames); JSONArray jArray = jsonObj.getJSONArray(NAME); for (int i = 0; i &lt; jArray.length(); i++) { addNewName(jArray.getString(i)); } } catch (JSONException e) { e.printStackTrace(); } } private void addNewName(String name) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(NAME, name); lstNames.add(map); } } </code></pre>
 

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