Get spinner selected item id from mysql database

2017-10-25T19:26:54

I am very new in Android.I want store spinner selected item id, which are saved in mysql database.I already retrieve items from mysql database to my spinner.Now I want to store that spinner selected item id in a global variable.Please help me. This is my code

AddProject

 RequestQueue requestQueue;
private ArrayList<Category> categoriesList;

// Url to get all categories
private String URL_COMPANY = "http://www.mybusket.com/pmsapp/webs/get_all_company.php";
//Data add
private String INSERT_URL = "http://192.168.1.9:8082/pmsapp/pmsemp/webapi/project_add.php";

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

    Spinner = (Spinner) findViewById(R.id.spinner);
    ProjectName = (EditText)findViewById(R.id.projectName);
    IsBilling = (EditText)findViewById(R.id.IsBilling);
    BillingBy = (EditText)findViewById(R.id.billingBy);
    ProjectDes = (EditText)findViewById(R.id.projectDescription);

    Save = (Button)findViewById(R.id.save);

    requestQueue = Volley.newRequestQueue(getApplicationContext());

    categoriesList = new ArrayList<Category>();

    // spinner item select listener
    Spinner.setOnItemSelectedListener(this);
    new GetCompany().execute();

}


/**
 * Adding spinner data
 * */
private void populateSpinner() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getName());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    Spinner.setAdapter(spinnerAdapter);
}

private class GetCompany extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_COMPANY, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {

            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {

                    JSONArray categories = jsonObj.getJSONArray("categories");
                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getInt("companyid"),
                                catObj.getString("companyname"));


                        categoriesList.add(cat);
                    }

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

        }else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    //Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString() + " Selected" , Toast.LENGTH_LONG).show();

}

I want to store that company id when a item selected from spinner..Please help me.

Copyright License:
Author:「Ankita Das」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/46931207/get-spinner-selected-item-id-from-mysql-database

About “Get spinner selected item id from mysql database” questions

I am very new in Android.I want store spinner selected item id, which are saved in mysql database.I already retrieve items from mysql database to my spinner.Now I want to store that spinner selecte...
I am trying to send the selected item from the spinner to database. what should be the type of a spinner: type = (Spinner)findViewById(R.id.type); this statement is not working
I want to insert the selected item of spinner into database when save button is pressed. There are two spinner. One is populated from array and other is from sqlite d/b. How can I get the string of
hi i have this spinner dropdown that displays data from my database. In my database, i have this table named area and it has this fields, aid its primary key and location which is a varchar. so far...
I've been reading stuff about this for hours and still haven't figured out how to do it. I managed to populate a spinner from a cursor reading the database and now I'm trying to get the selected i...
I want to retrieve selected spinner item id from Sqlite database in Android.When i want to select any spinner item at that time i want to get that get id that spinner item which is i have to select.
I am populating a spinner from the database like this // Populating the City Spinner Cursor cities = db.cityList(); startManagingCursor(cities); // create an array to specify which
I am using two spinners in my application one is for 'username' and second spinner for 'course'. Still now i did load data from MySQL database and assign to spinner, its works fine! When i click...
In my Android App I hava a layout with to Spinners. What I want to do is given a selected item from the first spinner, fill up the second one. The first spinner is filled up from a cursor (result ...
I've a Spinner, with following values/properties. &lt;Spinner android:id="@+id/spin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layo

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.