Akhilesh Chobey Akhilesh Chobey - 1 year ago 88
Android Question

Android SQLite : Vague behaviour of application

I am trying to edit and save a note in SQLite Database. However when I edit the array item

Akhilesh Chobey
, and press the back key, the following happens. And none of it is saved on restarting the application.

enter image description here


public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;

protected void onCreate(Bundle savedInstanceState) {

notesListView = (ListView) findViewById(R.id.notesListView);
notesArrayList = new ArrayList<String>();

if(Main2Activity.myDb != null) {
Cursor res = Main2Activity.myDb.getData();
if (res.getCount() == 0) {
Log.i("Error", "error");

while (res.moveToNext()) {

notesArrayList.add("Akhilesh Chobey");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);

notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", i);

Main2Activity(for editing note):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

protected void onCreate(Bundle savedInstanceState) {
editNote = (EditText) findViewById(R.id.noteEditText);

myDb = new DatabaseOperations(Main2Activity.this);

Intent intent = getIntent();
position = intent.getIntExtra("notePosition", -1);
if(position != -1){



public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (myDb != null) {
boolean isInserted = myDb.insertData(editNote.getText().toString());
if (isInserted) {
Toast.makeText(getApplicationContext(), "Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
MainActivity.notesArrayList.set(position, String.valueOf(charSequence));

Database Helper class:

public class DatabaseOperations extends SQLiteOpenHelper {

public static final String DatabaseName = "notes.db";
public static final String TableName = "notes";
public static final String Col1 = "text";

public DatabaseOperations(Context context) {
super(context, DatabaseName, null, 1);

public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TableName + " (text TEXT PRIMARY KEY AUTOINCREMENT) ");

public void onUpgrade(SQLiteDatabase db, int i, int i1) {

db.execSQL("DROP TABLE IF EXISTS " + TableName);


public boolean insertData(String note){

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col1, note);

long result = db.insert(TableName, null, contentValues);
if(result == -1){
return false;
}else {
return true;

public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TableName, null);
return result;

Answer Source

Your mistake is that you've been using


Which is wrong. You should use res.getString(res.getColumnIndex("text")) instead.

The getString method asks your Cursor to get a value of String type, getColumnIndex returns the column index given the column name. In your case you only have one column named text in your schema.

Please refer to the Cursor documentation for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download