title: how to load spinner items from SQlite databse real-time?
body: <p>I have an activity which has a spinner and its adapter fills with database items. there's an add alertdialog which allows user to add new items into database. the problem is that the new items won't show up in spinner unless user gets back to the parent activity and open this activity again. is there anyway to load spinner items real-time?</p>
  <p>Here is the database. <strong>I didn't get into details on how the database is created cause obviously it's fine and working</strong>. the problem is somewhere else</p>
  <pre><code>public class MyDatabaseHelper extends SQLiteOpenHelper {
  
      private static final String GROUP_TABLE_NAME = &quot;Groups&quot;;
      private static final String GROUP_ID = &quot;_id&quot;;
      private static final String GROUP_TITLE = &quot;group_title&quot;;
  
      void addGroup(String title) {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues cv = new ContentValues();
          cv.put(GROUP_TITLE, title);
          long result = db.insert(GROUP_TABLE_NAME, null, cv);
          if (result == -1) {
              Toast.makeText(context, &quot;Failed&quot;, Toast.LENGTH_SHORT).show();
          } else {
              Toast.makeText(context, &quot;Added successfully!&quot;, Toast.LENGTH_SHORT).show();
          }
      }
  
      Cursor readAllGroups() {
          String query = &quot;SELECT * FROM &quot; + GROUP_TABLE_NAME;
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = null;
          if (db != null) {
              cursor = db.rawQuery(query, null);
          }
          return cursor;
      }
  }
  
  </code></pre>
  <p>here is the activity which the spinner is in. <code>addGroup</code> is onclick method for a button which opens the alertdialog and so on...</p>
  <pre><code>
  public class GoodsAdd extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.goods_add);
          makeSpinner();
      }
  
      private void makeSpinner() {
          ArrayList&lt;String&gt; groups = new ArrayList&lt;&gt;();
          MyDatabaseHelper myDB = new MyDatabaseHelper(GoodsAdd.this);
          Cursor cursor = myDB.readAllGroups();
          if (cursor.getCount() != 0) {
              while (cursor.moveToNext()) {
                  groups.add(cursor.getString(1));
              }
          }
          Spinner spinner = findViewById(R.id.group_spinner);
          spinner.setOnItemSelectedListener(this);
          ArrayAdapter ad = new ArrayAdapter(this, R.layout.spinner_item, groups);
          ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spinner.setAdapter(ad);
      }
  
      public void addGroup(View view) {
          AlertDialog.Builder alert = new AlertDialog.Builder(this);
          final EditText edittext = new EditText(this);
          edittext.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
          alert.setMessage(&quot;new group name:&quot;);
          alert.setView(edittext);
  
          alert.setPositiveButton(&quot;submit&quot;, (dialog, whichButton) -&gt; {
              MyDatabaseHelper myDB = new MyDatabaseHelper(GoodsAdd.this);
              String title = edittext.getText().toString().trim();
              myDB.addGroup(title);
              Toast.makeText(GoodsAdd.this, title + &quot; !!!!!&quot;, Toast.LENGTH_SHORT).show();
          });
          alert.show();
      }
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {}
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {}
  
  }
  </code></pre>