Search code examples
androidsqlitespinner

How to insert spinner selected position value and EditText value together into SQLite database?


I am trying to add spinner selected position value and edittext into sqlite database but I don't know how would I use Spinner position value during insertion into sqlite database.

Someone help me please.

Below is the spinner Item position code:

 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (parent.getItemAtPosition(position).equals("Select expense type")) {
            // Do nothing

        } else {
            item = parent.getItemAtPosition(position).toString();
            Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
        }
    }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {
        //Do nothing
 }

Below is the Code where I want to use Spinner Item Position selected value and edittext value togather for inserting into sqlite database. so how could I get spinner position value for use into InsertExpense() function .

public void InsertExpense() {
       addExpId.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {


            }
        });

Solution

  • declare a global variable to store the selected item in spinner

    String selectedItem=";
    

    and here

     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             //yourList is the array your are using in your spinner adapter
           selectedItem=yourList.get(position).toString();
        }
    
     @Override
     public void onNothingSelected(AdapterView<?> parent) {
            //Do nothing
     }
    

    and then use this variable in your insertion method