title: How to update listview immediately after data insert to SQLite
body: <p>I have class called Custom which extend fragment. OnCreateView it will call populateListView method in the same class by passing in view v into it. onClicking the fab buttom, it will open a new activity called CustomForm which is a form letting user to fill thus to insert data into SQLite database.</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      final View v = inflater.inflate(R.layout.fragment_custom, container, false);
      openDB();
  
      ListView myList = (ListView) v.findViewById(R.id.customlistview);
      populateListView(v);
      View fabbutton = v.findViewById(R.id.fab);
      fabbutton.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent myIntent = new Intent(getActivity(), CustomForm.class);
              startActivity(myIntent);
  
          }
      });
      return v;
  }
  
  public void populateListView(View v) {
      Cursor cursor = dbAdapter.getAllRows();
      String[] fromFieldNames = new String[]{DBAdapter.KEY_DRUG_NAME, DBAdapter.KEY_DRUG_OTHER_NAME, DBAdapter.KEY_DRUG_DOSE_1};
      int[] toViewIDs = new int[]{R.id.lineone, R.id.linetwo, R.id.dose1};
      SimpleCursorAdapter myCursorAdapter;
      myCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.custom_row, cursor, fromFieldNames, toViewIDs, 0);
      ListView myList = (ListView) v.findViewById(R.id.customlistview);
      myList.setAdapter(myCursorAdapter);
  
  }
  </code></pre>
  
  <p>Here is the CustomForm Class</p>
  
  <pre><code>public class CustomForm extends AppCompatActivity {
          View create = findViewById(R.id.create);
          create.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Toast.makeText(getApplicationContext(), "Drug Item Created", Toast.LENGTH_SHORT).show();
                  if (!TextUtils.isEmpty(custom_drug_name_main.getText())) {
                      dbAdapter.insertRow(custom_drug_name_main.getText().toString(), custom_drug_name_other.getText().toString(), dose1.getText().toString());
  
                  } else {
  
                  }
                  finish();
  
  
  
              }
          });
  }
  </code></pre>
  
  <p>Here is my adapter class</p>
  
  <pre><code>public class DBAdapter {
  
      private static final String TAG = "DBAdapter"; //used for logging database version changes
  
      // Field Names:
      public static final String KEY_ROWID = "_id";
      public static final String KEY_DRUG_NAME = "drugName";
      public static final String KEY_DRUG_OTHER_NAME = "drugOtherName";
      public static final String KEY_DRUG_DOSE_1 = "dose1";
  
      public static final String[] ALL_KEYS = new String[]{KEY_ROWID, KEY_DRUG_NAME, KEY_DRUG_OTHER_NAME,KEY_DRUG_DOSE_1};
  
      // DataBase info:
      public static final String DATABASE_NAME = "custom_drug";
      public static final String DATABASE_TABLE = "custom_drug_table";
      public static final int DATABASE_VERSION = 3; // The version number must be incremented each time a change to DB structure occurs.
  
      //SQL statement to create database
      private static final String DATABASE_CREATE_SQL =
              "CREATE TABLE " + DATABASE_TABLE
                      + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                      + KEY_DRUG_NAME + " TEXT NOT NULL, "
                      + KEY_DRUG_OTHER_NAME + " TEXT,"
                      + KEY_DRUG_DOSE_1 + " TEXT NOT NULL"
                      + ");";
  
      private final Context context;
      private DatabaseHelper myDBHelper;
      private SQLiteDatabase db;
  
      public DBAdapter(Context ctx) {
          this.context = ctx;
          myDBHelper = new DatabaseHelper(context);
      }
  
      // Open the database connection.
      public DBAdapter open() {
          db = myDBHelper.getWritableDatabase();
          return this;
      }
  
      // Close the database connection.
      public void close() {
          myDBHelper.close();
      }
  
      // Add a new set of values to be inserted into the database.
      public long insertRow(String drugName, String drugOtherName, String dose1) {
          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_DRUG_NAME, drugName);
          initialValues.put(KEY_DRUG_OTHER_NAME, drugOtherName);
          initialValues.put(KEY_DRUG_DOSE_1, dose1);
  
          // Insert the data into the database.
          return db.insert(DATABASE_TABLE, null, initialValues);
      }
  
      // Delete a row from the database, by rowId (primary key)
      public boolean deleteRow(long rowId) {
          String where = KEY_ROWID + "=" + rowId;
          return db.delete(DATABASE_TABLE, where, null) != 0;
      }
  
      public void deleteAll() {
          Cursor c = getAllRows();
          long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
          if (c.moveToFirst()) {
              do {
                  deleteRow(c.getLong((int) rowId));
              } while (c.moveToNext());
          }
          c.close();
      }
  
      // Return all data in the database.
      public Cursor getAllRows() {
          String where = null;
          Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
          if (c != null) {
              c.moveToFirst();
          }
          return c;
      }
  
      // Get a specific row (by rowId)
      public Cursor getRow(long rowId) {
          String where = KEY_ROWID + "=" + rowId;
          Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                  where, null, null, null, null, null);
          if (c != null) {
              c.moveToFirst();
          }
          return c;
      }
  
      // Change an existing row to be equal to new data.
      public boolean updateRow(long rowId, String drugName, String drugOtherName) {
          String where = KEY_ROWID + "=" + rowId;
          ContentValues newValues = new ContentValues();
          newValues.put(KEY_DRUG_NAME, drugName);
          newValues.put(KEY_DRUG_OTHER_NAME, drugOtherName);
          // Insert it into the database.
          return db.update(DATABASE_TABLE, newValues, where, null) != 0;
      }
  
  
      private static class DatabaseHelper extends SQLiteOpenHelper {
          DatabaseHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase _db) {
              _db.execSQL(DATABASE_CREATE_SQL);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
              Log.w(TAG, "Upgrading application's database from version " + oldVersion
                      + " to " + newVersion + ", which will destroy all old data!");
  
              // Destroy old database:
              _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
  
              // Recreate new database:
              onCreate(_db);
          }
      }
  }
  </code></pre>
  
  <p>My question is: how to call populateListView method from the Custom class so that once the data is inserted, the listview is updated immediately. I am new to programming, I hope I made my question clear. Thank you.</p>