Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I solved your problem. In case you cannot fully understand my explanation refer to <a href="http://developer.android.com/training/notepad/index.html" rel="nofollow">this wonderful tutorial</a>. The DatabaseHelper class should not be a class, it should be the sub class of another class. I have made many changes to your code. Just change your code like I have written. Enough said lets go to the code.</p> <p>There are very few changes in <strong>MainActivity.java</strong> i have commented on all the changes</p> <pre><code>package com.example.sample; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import android.os.Bundle; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.text.format.DateFormat; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { Button b1; EditText et1; protected DatabaseAdapter helper; // type of variable helper has been changed @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helper = new DatabaseAdapter(this); //the context of the current class is passed helper.open(); b1 = (Button)findViewById(R.id.button1); et1 = (EditText)findViewById(R.id.editText1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); Date dateobject; String date_var=(et1.getText().toString()); try { dateobject=formatter.parse(date_var); String date=new SimpleDateFormat().format(dateobject); helper.AddDetail(date); Toast.makeText(getApplicationContext(),""+date, Toast.LENGTH_SHORT).show(); helper.close(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } </code></pre> <p>Now come to <strong>DatabaseHelper.java</strong> i have made it a subclass of another class <strong>DatabaseAdapter.java</strong></p> <pre><code>package com.example.sample; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseAdapter { DatabaseHelper helper; SQLiteDatabase db; static final int VERSION=2; static final String DATA_NAME="Mydata"; private static final String query = "create table Mytable(id Integer primary key,Daate text not null)"; //the query has been changed private final Context mCtx; public DatabaseAdapter(Context ctx) { this.mCtx = ctx; } //DatabaseHelper has become a subclass public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATA_NAME, null, VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub arg0.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } public DatabaseAdapter open() throws SQLException { helper = new DatabaseHelper(mCtx); db=helper.getWritableDatabase(); return this; } public void close() { helper.close(); } public long AddDetail(String date) { Log.d("tag",date); ContentValues vals=new ContentValues(); vals.put("Daate",date); long a=db.insert("Mytable", null, vals); return a; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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