• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get the Id of selected database records in spinner

Hi. I am using spinner where it displayes the names of students. There is table studentrecords which stores Student Id, FirstName, LastName and class. The requirement is that when I select the student name, then its Id, FirstName,LastName, Class should be displayed on text box. I have the below code. How should I get the Id of the selected student.
Java:
package com.example.anitaa.studentapplication;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UpdateActivity extends AppCompatActivity {
    Spinner spinner;
    ArrayAdapter adapter1;
    EditText Id1,firstname1,lastname1,class1;
    Button button1;
    private JSONArray response;
    String url1 = "http://192.168.1.6/student/web/studentrecords";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_update );
        spinner = (Spinner) findViewById ( R.id.spinner1 );
        Id1 = (EditText) findViewById ( R.id.studentid );
        firstname1 = (EditText) findViewById ( R.id.firstname );
        lastname1 = (EditText) findViewById ( R.id.lastname );
        class1 = (EditText) findViewById ( R.id.studentclass );
        final List<String> list = new ArrayList<> ();



        JsonArrayRequest request = new JsonArrayRequest ( url1,
                new Response.Listener<JSONArray> () {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            list.add(0,"Select Student");
                            // response is the JSONArray that you need to iterate
                            for (int i = 0; i < response.length (); i++) {
                                JSONObject obj = response.getJSONObject ( i );
                                int StudentId = obj.getInt("Id");
                                String name= obj.getString ( "FirstName" );
                                String lastName = obj.getString ( "LastName" );



                                list.add(name+" "+lastName);




                            }

                            spinner_code ();


                        } catch (JSONException e) {
                            e.printStackTrace ();
                        }
                    }

                    private void spinner_code() {

                        adapter1=new ArrayAdapter ( UpdateActivity.this,android.R.layout.simple_spinner_item, list );
                        adapter1.setDropDownViewResource ( android.R.layout.simple_spinner_dropdown_item);
                        spinner.setAdapter ( adapter1 );
                        spinner.setOnItemSelectedListener ( new AdapterView.OnItemSelectedListener () {
                            @SuppressLint("ResourceType")
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                spinner.setSelection ( position );
                                if(spinner.getSelectedItem ().equals ( "Select Student" )) {
                                    Id1.setText("");

                                }else
                                {
                                   

                                }
                            }


                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        } );



                    }
                    private int getStudentId(int position)
                    {
                        int Student_Id =0;
                        try
                        {
                            JSONObject json = response.getJSONObject ( position );
                            Student_Id = json.getInt("Id");
                        } catch (JSONException e) {
                            e.printStackTrace ();
                        }
                        return Student_Id;
                    }





                }, new Response.ErrorListener () {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace ();
            }
        } );


        MySingleton.getInstance ( UpdateActivity.this ).addToRequestque ( request );


    }
}
 
Back
Top Bottom