Prashant Sontale
Newbie
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.
When I run it then spinner is not populated witth records. How to do?
Java:
package com.example.anitaa.studentapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class UpdateActivity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter adapter1;
String url1 = "http://192.168.1.6/student/web/studentrecords/studentnames";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_update );
spinner = (Spinner) findViewById ( R.id.spinner1 );
final List<String> list = new ArrayList<String> ();
try {
JSONArray jsonarray=new JSONArray(url1);
for(int i=0;i<jsonarray.length();i++)
{
JSONObject jsonObject = jsonarray.getJSONObject ( i );
list.add(jsonObject.getString ( "FirstName"));
list.add( jsonObject.getString ("LastName"));
}
} catch (JSONException e) {
e.printStackTrace ();
}
adapter1=new ArrayAdapter ( this,android.R.layout.simple_spinner_item,list );
// adapter1.setDropDownViewResource ( android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter ( adapter1 );
}
}
When I run it then spinner is not populated witth records. How to do?