Note that there are some explanatory texts on larger screens.

plurals
  1. POusing singleton as a global cache for selected database values
    text
    copied!<p>I like to use a Singleton as a globally accessible cache for certain database values. This is my definition of the singleton with sample variables gi1, and gDBAdapter:</p> <pre><code>public class GlobalVars { private Integer gi1; private WebiDBAdapter gDBAdapter; private static GlobalVars instance = null; protected GlobalVars() { // Exists only to defeat instantiation. } public static GlobalVars getInstance() { if(instance == null) { instance = new GlobalVars(); } // now get the current data from the DB, WebiDBAdapter myDBAdapter = new WebiDBAdapter(this); // &lt;- cannot use this in static context gDBAdapter = myDBAdapter; // &lt;- cannot use this in static context myDBAdapter.open(); Cursor cursor = myDBAdapter.fetchMainEntry(); startManagingCursor(cursor); // &lt;- the method startManagingCursor is undefined for the type GlobalVars // if there is no DB yet, lets just create one with default data if (cursor.getCount() == 0) { cursor.close(); createData(); // &lt;- the method createData is undefined for the type GlobalVars cursor = myDBAdapter.fetchMainEntry(); startManagingCursor(cursor);// &lt;- the method startManagingCursor is undefined for the type GlobalVars } gi1 = cursor.getInt(cursor // &lt;- Cannot make a static reference to the non-static field gi1 .getColumnIndexOrThrow(WebiDBAdapter.KEY1)); if (gi1 == null) gi1 = 0; cursor.close(); return instance; } </code></pre> <p>But all the references to the instance vars are not recognized.</p> <p>All the <code>// &lt;-</code> entries show the errors I get</p> <p>I guess there is some basics that I am not doing right here.</p> <p>What would be the right way to create the singleton and initialize its values with the values from the database?</p> <p>Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload