7040

Android use DatabaseHandler out of Activity

Question:

Hi all i'm new in Android and this application is my first app for use database. in my application i have DatabaseHandler class with this simple structure

public class DatabaseHandler extends SQLiteOpenHelper{ private static String DB_PATH = ""; private static final String DATABASE_NAME = "tsms"; private static String RECEIVE_FIELDS_TABLE = "ReceiveFields"; private static final String COLUMN_ID = "id"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); }

now in other class as name TSMS i can fill Fields ReceivedFields as :

public class ReceiveFields { public long lastId; public String smsNumber; public String mobileNumber; public String senderName; public String smsBody; public DateTimeD1 receiveDate; public ReceiveFields(){ } public ReceiveFields(long lastId, String smsNumber, String mobileNumber, String senderName, String smsBody, DateTimeD1 receiveDate) { this.lastId = lastId; this.smsNumber = smsNumber; this.mobileNumber = mobileNumber; this.senderName = senderName; this.smsBody = smsBody; this.receiveDate = receiveDate; }

TSMS class function to fill without any error and i want to save it to database:

for (int i = 0; i <= strings.length - 1; i++) { String[] str1 = WSDLHelper.convert3(strings[i]); try { receiveArray.add(new ReceiveFields( Long.valueOf(str1[0]), str1[1], str1[2], URLDecoder.decode(str1[3], "UTF-8"), URLDecoder.decode(str1[4], "UTF-8"), WSDLHelper.convertDate(str1[5]))); } catch (UnsupportedEncodingException ex) { throw new TException(PublicErrorList.NOT_EXIST_ERROR_DETAIL); } }

now i want to save records to database into for i'm define DatabaseHandler db = new DatabaseHandler(this); before that so i'm getting this error and i can not resolve that:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/WP7Mn.png" data-original="https://i.stack.imgur.com/WP7Mn.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

You have to pass context to DatabaseHandler class. So you can do it with Activity, Service or Broadcast Receiver. So change this contructor

public ReceiveFields(){ }

to

public ReceiveFields(Context ctx){ this.ctx=ctx; //create a field Context ctx; }

Pass context from activity, service or receiver and you can use ctx instead of this.

Recommend

  • android.database.sqlite.SQLiteException: table has no such column
  • Insert values in to SQLite database ? java.lang.NullPointerException [duplicate]
  • static database class to use with any activity
  • More efficient ways to store information in Android Database
  • How do I translate LR(1) Parse into a Abstract syntax tree?
  • ConnectivityManager.CONNECTIVITY_ACTION deprecated
  • Best practice to eliminate magic numbers within a member function
  • Iron Router: How do I send data to the layout?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Wrong row deleted from custom listview with spinner
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Retrieving value from sql ExecuteScalar()
  • Deleting and Updating values from a cusrsor adapter
  • Can Jackson SerializationFeature be overridden per field or class?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Javascript Callbacks with Object constructor
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • JTable with a ScrollPane misbehaving
  • how does django model after text[] in postgresql [duplicate]
  • How to stop GridView from loading again when I press back button?
  • LevelDB C iterator
  • unknown Exception android
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?