Helper
public boolean mMessagesSent(String ID,int Data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, ID);
contentValues.put(KEY_MESSAGES_SENT, Data);
db.update(TABLE_USER_DATA, contentValues, null, null);
return true;
}
Activity
mainData.mTotalMessages("MyData", +1);
mainData.mTotalMessagesSent("MyData",+1);
mainData.mMessages(MessageRecieverId,+1);
mainData.mMessagesSent(MessageRecieverId,+1);
Is this the correct method to update data... I want to increase the int value of data by 1 so i have put +1 but still the value is empty when i retrieve data
CODE AFTER FOLLOWING FIRST ANSWER
public boolean mMessagesSent(String ID,int Data) {
MainData mainData = new MainData(getApplicationContext());
SQLiteDatabase db = mainData.getWritableDatabase();
String newId = ID;
int newData = Data;
MainData helper = new MainData(this); //Change the name to your Helper Class name
Cursor data = helper.getData();
while (data.moveToNext()) {
newId = data.getString(data.getColumnIndex("Data"));
newData = data.getInt(data.getColumnIndex("TotalMessagesSent"));
}
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_DATA, newId);
contentValues.put(KEY_MESSAGES_SENT, (newData + 1)); //Change the value of newData(which is actually your old value) by incrementing
db.update(TABLE_USER_DATA, contentValues, null, null);
return true;
}
FETCHING
final MainData myDBHlpr = new MainData(getActivity());
Cursor csr = myDBHlpr.getAllQuestions(getActivity());
while (csr.moveToNext()) {
mTotalMessagesSent.setText(csr.getString(1));
mTotalMessagesRecieved.setText(csr.getString(csr.getColumnIndex("TotalMessagesRecieved")));
mTotalMessages.setText(csr.getString(csr.getColumnIndex("TotalMessages")));
}
csr.close();
If you meant to increase the existing value of data by once each time you call the function mMessagesSent()
You can consider to first take the value from the database if it exists, and increment the value. Create a function in Helper as shown below
This will return a Cursor to the Database, to read the values in the database. Do not put the function mMessagesSent in Helper class, put it in the Activity. Now in your mMessagesSent function, call the above function getData() as shown
I hope you got your answer.