1. Are you ready for the Galaxy S20? Here is everything we know so far!

Spinner not getting populated with database records in android

Discussion in 'Android Development' started by Prashant Sontale, Sep 5, 2019.

  1. Prashant Sontale

    Thread Starter

    Hi. I am using Yii2 REST API. I have a table called studentrecords consisting of Id,FirstName,LastName, Class fields. I have a API which returns the json array containing stiudent FirstName and LastName. API is working fine. Now I have a android code where I use a spinner to display student names from the database.
    Code (Java):
    1. package com.example.anitaa.studentapplication;
    2.  
    3. import android.support.v7.app.AppCompatActivity;
    4. import android.os.Bundle;
    5. import android.widget.ArrayAdapter;
    6. import android.widget.Spinner;
    7.  
    8. import org.json.JSONArray;
    9. import org.json.JSONException;
    10. import org.json.JSONObject;
    11.  
    12. import java.util.ArrayList;
    13. import java.util.List;
    14.  
    15. public class UpdateActivity extends AppCompatActivity {
    16.     Spinner spinner;
    17.     ArrayAdapter adapter1;
    18.     String url1 = "http://192.168.1.6/student/web/studentrecords/studentnames";
    19.  
    20.     @Override
    21.     protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate ( savedInstanceState );
    23.         setContentView ( R.layout.activity_update );
    24.         spinner = (Spinner) findViewById ( R.id.spinner1 );
    25.         final List<String> list = new ArrayList<String> ();
    26.  
    27.         try {
    28.             JSONArray jsonarray=new JSONArray(url1);
    29.             for(int i=0;i<jsonarray.length();i++)
    30.             {
    31.  
    32.                 JSONObject jsonObject = jsonarray.getJSONObject ( i );
    33.                 list.add(jsonObject.getString ( "FirstName"));
    34.                 list.add( jsonObject.getString ("LastName"));
    35.             }
    36.         } catch (JSONException e) {
    37.             e.printStackTrace ();
    38.         }
    39.  
    40.         adapter1=new ArrayAdapter ( this,android.R.layout.simple_spinner_item,list );
    41.        // adapter1.setDropDownViewResource ( android.R.layout.simple_spinner_dropdown_item );
    42.         spinner.setAdapter ( adapter1 );
    43.  
    44.  
    45.  
    46.     }
    47. }
    48.  
    49.  
    50.  
    When I run it then spinner is not populated witth records. How to do?
     


Loading...

Share This Page

Loading...