How to display database data after selecting the item from spinner?
<p>I want to display person details from reg_farmer table.. If i select an item from the spinner, i want to display the corresponding data matches with that item from the database.</p>

<p>Here is my android MainActivity.class file.. </p>

<pre><code>public class MainActivity extends AppCompatActivity {

//Declaring an Spinner
private Spinner spinner,spinner2;

//An ArrayList for Spinner Items
private ArrayList&lt;String&gt; states;
private ArrayList&lt;String&gt; district;

//JSON Array
private JSONArray States;
private JSONArray District;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing the ArrayList
    states = new ArrayList&lt;String&gt;();
    district = new ArrayList&lt;String&gt;();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner2 = (Spinner) findViewById(R.id.spinner2);

    //Adding an Item Selected Listener to our Spinner
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            String sid="";
            try {
                //Getting object of given index
                JSONObject json = States.getJSONObject(position);

                //Fetching id from that object
                sid = json.getString("id");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            getDistrict(sid);
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {
            Toast.makeText(MainActivity.this,"Nothing Selected",Toast.LENGTH_SHORT).show();
        }
    });

    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            String Dname="";
            try {
                //Getting object of given index
                JSONObject json = District.getJSONObject(position);

                //Fetching name from that object
                Dname = json.getString("dname");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Toast.makeText(MainActivity.this,Dname,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {
            Toast.makeText(MainActivity.this,"Empty",Toast.LENGTH_SHORT).show();

        }
    });

    //This method will fetch the States data from the URL
   getStates();

}

private void getStates(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Request.Method.POST,Config.DATA_State,
            new Response.Listener&lt;String&gt;() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        States = j.getJSONArray("States");


                        for(int i=0;i&lt; States.length();i++){
                            try {
                                //Getting json object
                                JSONObject state = States.getJSONObject(i);

                                //Adding the name of the state to array list
                                MainActivity.this.states.add(state.getString("name"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        //Setting adapter to show the items in the spinner
                        spinner.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, states));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getDistrict(final String sid){
    //Creating a string request
    StringRequest dRequest = new StringRequest(Request.Method.POST,Config.DATA_District,
            new Response.Listener&lt;String&gt;() {

                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        Log.i("tagconvertstr", "[" + response + "]");
                        j = new JSONObject(response);

                        boolean error = j.getBoolean("error");

                        // Check for error node in json
                        if (!error) {
                            //Storing the Array of JSON String to our JSON Array
                            District = j.getJSONArray("District");

                            //Toast.makeText(MainActivity.this, District.toString(), Toast.LENGTH_SHORT).show();
                            district.removeAll(district);
                            for (int i = 0; i &lt; District.length(); i++) {
                                try {
                                    //Getting json object
                                    JSONObject dist = District.getJSONObject(i);
                                    //Adding the name of the district to array list
                                    MainActivity.this.district.add(dist.getString("dname"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            //Setting adapter to show the items in the spinner
                            spinner2.setAdapter(new ArrayAdapter&lt;String&gt;(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, district));
                        }else {

                            String errorMsg = j.getString("error_msg");
                            Toast.makeText(MainActivity.this,
                                    errorMsg, Toast.LENGTH_LONG).show();
                        }
                        } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map&lt;String,String&gt; getParams(){
            Map&lt;String,String&gt; params = new HashMap&lt;String, String&gt;();
            params.put("state_id", sid);
            return params;
        }

    };

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(dRequest);
}
}
</code></pre>

<p>Here is my Config.java file.. In this file i give the url for fetching spinner items..</p>

<pre><code>public class Config {
//JSON URL
public static final String DATA_State = "http://10.0.3.2/cof/state.php";
public static final String DATA_District = "http://10.0.3.2/cof/district.php";

}
</code></pre>

<p>I want to display the details like this.. I want to view code,name and mobile number of the person when i choose the district and state from the spinner
<a href="https://i.stack.imgur.com/4YluI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4YluI.jpg" alt="I want to display the details like this.. I want to view code,name and mobile number of the person when i choose the district and state from the spinner"></a></p>