Akhilesh Chobey Akhilesh Chobey - 5 months ago 14
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

MainActivity:

public class MainActivity extends AppCompatActivity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

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

while (res.moveToNext()) {
notesArrayList.add(String.valueOf(res));
}
}

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

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


Main2Activity(for editing note):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editNote = (EditText) findViewById(R.id.noteEditText);

myDb = new DatabaseOperations(Main2Activity.this);

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

editNote.setText(MainActivity.notesArrayList.get(position));

}
editNote.addTextChangedListener(this);
}

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

}

@Override
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));
MainActivity.adapter.notifyDataSetChanged();
}
}


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);
}

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

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

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

}

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

Your mistake is that you've been using

String.valueOf(res)

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.