title: How I get Selected Spinner Value Id From Sqlite Database
body: <p>I want COMPANY_ID as selected Spinner Value From SQLite Database &amp; toast it for further use.
  Can anyone help me or suggest me how to get COMPANY_ID </p>
  
  <p>Here is Activty Code </p>
  
  <pre><code>   public class MainActivity extends Activity implements
          AdapterView.OnItemSelectedListener {
  
  
  
  
      DatabaseHelper databaseHelper;
      Context context;
      NestedScrollView nestedScrollView;
      private ProgressDialog pdialog;
      RequestQueue requestQueue;
      private Spinner appCompatSpinner;
      private ArrayList&lt;DataCompany&gt; dataCompanies;
      private ArrayList&lt;Company&gt; labels;
      private ArrayList&lt;Data&gt; data;
      private ArrayList&lt;BankInfo&gt; bankInfos;
      private EditText InsureName, BankHypotical;
      private MaterialRippleLayout NextButtonmaterialRippleLayout, SaveButtonmaterialRippleLayout, materialRippleNext;
      private String SpinnerData;
      private NameAdapter nameAdapter;
      private List&lt;BankInfo&gt; names;
      protected ViewDialog viewDialog;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_snv_main);
          viewDialog = new ViewDialog(this);
          viewDialog.setCancelable(false);
  
          appCompatSpinner = findViewById(R.id.selectSpinner_Bank_List);
          context = this;
  
          databaseHelper = new DatabaseHelper(context);
  
          appCompatSpinner.setOnItemSelectedListener(this);
  //        loadSpinnerData();
          if (Utils.isNetworkAvailable(getApplicationContext())) {
              Call&lt;NewInsuranceModel&gt; call = RetrofitClient
                      .getInstance().getApi().newInsuranceModel("get_new_insurance_fields");
  
              showProgressDialog();
              call.enqueue(new Callback&lt;NewInsuranceModel&gt;() {
  
                  @Override
                  public void onResponse(Call&lt;NewInsuranceModel&gt; call, retrofit2.Response&lt;NewInsuranceModel&gt; response) {
                      NewInsuranceModel newInsuranceModel = response.body();
                      hideProgressDialog();
  
  
                      if (newInsuranceModel.getStatus().toString().equalsIgnoreCase("true")) {
  
                          dataCompanies = new ArrayList&lt;DataCompany&gt;();
                          dataCompanies.add(new DataCompany("Select Insurance Company"));
                          try {
                              for (int i = 0; i &lt; dataCompanies.size(); i++) {
  
  
                                  DataCompany dataCompany = newInsuranceModel.getDataCompany().get(i);
                                  SaveIntoDatabase task = new SaveIntoDatabase();
                                  task.execute(dataCompany);
                                  dataCompanies.add(newInsuranceModel.getDataCompany().get(i));
                                  initCustomSpinner();
                              }
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
  
  
                      } else {
  
                          Toast.makeText(MainActivity.this, newInsuranceModel.getMsg() + "", Toast.LENGTH_SHORT).show();
                      }
                  }
  
                  @Override
                  public void onFailure(Call&lt;NewInsuranceModel&gt; call, Throwable t) {
  
                      hideProgressDialog();
                      Toast.makeText(MainActivity.this, " Again Error", Toast.LENGTH_LONG).show();
  
                  }
              });
  
  
          } else {
  
              loadSpinnerData();
          }
  
  
          nestedScrollView = findViewById(R.id.nestedScrollView);
          InsureName = findViewById(R.id.text_insured_name);
          BankHypotical = findViewById(R.id.text_bank_hypothecation);
          NextButtonmaterialRippleLayout = findViewById(R.id.next_button_main_activity);
  
  
          NextButtonmaterialRippleLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  NextPage();
  
  
              }
          });
      }
  
  
      private void loadSpinnerData() {
          databaseHelper = new DatabaseHelper(getApplicationContext());
  
  
          try {
  
  
  
              ArrayList&lt;Company&gt; labels = databaseHelper.getAllLabels();
              ArrayAdapter dataAdapter = new ArrayAdapter&lt;&gt;(this,
                      android.R.layout.simple_spinner_item, labels);
              appCompatSpinner.setAdapter(dataAdapter);
             appCompatSpinner.setPrompt("Select Insurance Company");
              dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          SpinnerData = parent.getItemAtPosition(position).toString();
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          // TODO Auto-generated method stub
  
      }
  
  
      public class SaveIntoDatabase extends AsyncTask&lt;DataCompany, DataCompany, Boolean&gt; {
  
          private final String TAG = SaveIntoDatabase.class.getSimpleName();
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
  
          @Override
          protected Boolean doInBackground(DataCompany... params) {
              DataCompany dataCompany = params[0];
              databaseHelper.addDataCompany(dataCompany);
              return null;
          }
      }
  
  
      protected void hideProgressDialog() {
          viewDialog.dismiss();
      }
  
      protected void showProgressDialog() {
          viewDialog.show();
      }
  
      protected void showProgressDialog(String message) {
          showProgressDialog();
      }
  
      private void initCustomSpinner() {
          CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(MainActivity.this, dataCompanies);
          appCompatSpinner.setAdapter(customSpinnerAdapter);
          appCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  SpinnerData = parent.getItemAtPosition(position).toString();
                  // Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
                  // sstate = (String) parent.getItemAtPosition(position);
  
  
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
              }
          });
      }
  
  
      public class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
  
          private final Context activity;
          private ArrayList&lt;DataCompany&gt; asr;
  
          public CustomSpinnerAdapter(Context context, ArrayList&lt;DataCompany&gt; asr) {
              this.asr = asr;
              activity = context;
          }
  
          public int getCount() {
              return asr.size();
          }
  
          public Object getItem(int i) {
              return asr.get(i);
          }
  
          public long getItemId(int i) {
              return (long) i;
          }
  
          @Override
          public View getDropDownView(int position, View convertView, ViewGroup parent) {
              TextView txt = new TextView(MainActivity.this);
              txt.setPadding(16, 16, 16, 16);
              txt.setTextSize(18);
              txt.setGravity(Gravity.CENTER);
              txt.setText(asr.get(position).getCompanyName());
              txt.setTextColor(Color.parseColor("#2196F3"));
              return txt;
          }
  
          public View getView(int i, View view, ViewGroup viewgroup) {
              TextView txt = new TextView(MainActivity.this);
              txt.setGravity(Gravity.CENTER);
              txt.setPadding(16, 16, 16, 16);
              txt.setTextSize(14);
              txt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_drop_down_white_24dp, 0);
              txt.setText(asr.get(i).getCompanyName());
              txt.setTextColor(Color.parseColor("#FFFFFF"));
              return txt;
          }
      }
  
      private void NextPage() {
  
          String editInsureName = InsureName.getText().toString();
          String editBankHypotical = BankHypotical.getText().toString();
          int pos = appCompatSpinner.getSelectedItemPosition();
          if (pos == 0) {
              Snackbar snackbar = Snackbar
                      .make(nestedScrollView, "Select Insurance Company", Snackbar.LENGTH_LONG);
              snackbar.show();
              View snackBarView = snackbar.getView();
              snackBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
              appCompatSpinner.requestFocus();
  
          } else if (TextUtils.isEmpty(editInsureName)) {
              Snackbar snackbar = Snackbar
                      .make(nestedScrollView, "Enter Insured Name / Society Name", Snackbar.LENGTH_LONG);
              snackbar.show();
              View snackBarView = snackbar.getView();
              snackBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
              InsureName.requestFocus();
          } else if (TextUtils.isEmpty(editBankHypotical)) {
              Snackbar snackbar = Snackbar
                      .make(nestedScrollView, "Enter Bank Hypothecation With", Snackbar.LENGTH_LONG);
              snackbar.show();
              View snackBarView = snackbar.getView();
              snackBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
              BankHypotical.requestFocus();
          } else {
              Intent nextIntent = new Intent(MainActivity.this, FarmerInformationActivity.class);
              //  nextIntent.putExtra("id", dataCompanies.get(appCompatSpinner.getSelectedItemPosition()).getId());
              nextIntent.putExtra("id", labels.get(appCompatSpinner.getSelectedItemPosition()).getCompanyId());
              nextIntent.putExtra("farmer_insure_name", InsureName.getText().toString());
              nextIntent.putExtra("farmer_bank_hypo", BankHypotical.getText().toString());
              startActivity(nextIntent);
              Snackbar snackbar = Snackbar.make(nestedScrollView, "Saved Successfully", Snackbar.LENGTH_LONG);
              snackbar.show();
              View snackBarView = snackbar.getView();
              snackBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
          }
      }
  
  }
  </code></pre>
  
  <p>Saving Data in SQLite From JSON here is code</p>
  
  <pre><code> public class SaveIntoDatabase extends AsyncTask&lt;DataCompany, DataCompany, Boolean&gt; {
  
          private final String TAG = SaveIntoDatabase.class.getSimpleName();
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
          }
          @Override
          protected Boolean doInBackground(DataCompany... params) {
              DataCompany dataCompany = params[0];
              databaseHelper.addDataCompany(dataCompany);
              return null;
          }
      }
  </code></pre>
  
  <p>Database Helper Class</p>
  
  <p>I am adding values in Sqlite Database from Json and here is code</p>
  
  <pre><code>     public void addDataCompany(DataCompany dataCompany) {
              SQLiteDatabase db = this.getWritableDatabase();
              ContentValues contentValues = new ContentValues();
              contentValues.put(COMPANY_ID, dataCompany.getId() + "");
              contentValues.put(COMPANY_NAME, dataCompany.getCompanyName() + "");
              // contentValues.put(BANK_COLUMN, bank);
  
              try {
                  db.insert(TABLE_NAME_OFFLINE_LOAD_DATA, null, contentValues);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              db.close();
  
          }
  
  
    public List&lt;String&gt; getAllLabels(){
          List&lt;String&gt; labels = new ArrayList&lt;String&gt;();
  
          // Select All Query
          String selectQuery = "SELECT  * FROM " + TABLE_NAME_OFFLINE_LOAD_DATA;
  
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery(selectQuery, null);
  
          // looping through all rows and adding to list
          if (cursor.moveToFirst()) {
              do {
                  labels.add(cursor.getString(1));
              } while (cursor.moveToNext());
          }
  
          // closing connection
          cursor.close();
          db.close();
  
          // returning lables
          return labels;
      }
  </code></pre>
  
  <p>Thanks in advance</p>