Home » Android » java – Android / MySQL: Populate Spinner from database with Initial Set Text "Please Select"-Exceptionshub

java – Android / MySQL: Populate Spinner from database with Initial Set Text "Please Select"-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I want to ask regarding my codes. I successfully create a spinner that lists data from the database.

In my case, this spinner will populate data from column “fac_name” at table “factory”. Now, at initial list of the spinner, I want to display a text “Please Select” and below the text will display the populate data from database. Below is my code.

Java

public static final String TAG_FAC_NAME = "fac_name";

public static final String JSON_ARRAY = "result";
private JSONArray result;
private ArrayList<String> factoryList;

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

    factoryList = new ArrayList<>();
    spFactory = findViewById(R.id.spFactory);
    spFactory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

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

        }
    });

    getData();

}

private void getData(){
    StringRequest stringRequest = new StringRequest(URLs.URL_SPINNER_FACTORY,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    JSONObject j = null;

                    try {
                        j = new JSONObject(response);

                        result = j.getJSONArray(JSON_ARRAY);

                        getFactory(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void getFactory(JSONArray j){
    for(int i=0;i<j.length();i++){
        try {
            JSONObject json = j.getJSONObject(i);

            factoryList.add(json.getString(TAG_FAC_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    spFactory.setAdapter(new ArrayAdapter<String>(AddTask.this, android.R.layout.simple_spinner_dropdown_item, factoryList));
}

URL_SPINNER_FACTORY (spinner_factory.php)

<?php 

    require_once "../config/configPDO.php";

    $query = $conn->prepare("SELECT * FROM ot_factory ORDER BY fac_id ASC");
    $query->execute();

    $factoryList = array();

    while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
        array_push($factoryList,array(

            'fac_name'=>$row['fac_name'],

            )
        );
    }

    echo json_encode(array('result'=>$factoryList));

?>

Can Nyone help me on this? Thank you..

How to&Answers:

you can do it by using the XML code. like

`<Spinner
  android:id="@+id/R.id.spFactory"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginStart="20dp"
  android:layout_marginLeft="20dp"
  android:layout_marginTop="15dp"
  android:layout_marginBottom="15dp"
  android:prompt="Please Select" />`

Answer:

don’t worry,

1)you can programmatically set prompt by .setPrompt(“Select an item”); just before setting spinner adpter
2)or you can use custom spinner view Don’t use a spinner. Instead, use a button and style with the title to look like a spinner (will have to find how I did this). When the user clicks the button, open a “select one” dialog and style it to look like a spinner’s dialog. I’ll try to put a code sample up shortly.