I am trying to make this simple app which calculates likes. Every-time you click on like button, it updates the counter in DB. The problem is, when I click it 5 times in one session, close and open again, it starts from 1 instead of 6 and shows 1,2,3,4,5,1. The value is lost once the session ends. I want it to be 6 and continue. How can I retrieve values from Database. Also, the rows are increasing. I want it to update the count in a single row and not add a new row every time I click the button.
This is my Main Activity:
public class MainActivity extends AppCompatActivity {
//declare
TextView showCounts;
MyDBHandler dbHandler;
Calculate obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize objects
showCounts = (TextView) findViewById(R.id.showCounts);
dbHandler = new MyDBHandler(this, null, null, 1);
obj = new Calculate();
}
public void likeButtonClicked(View view) //when like button is clicked
{
obj.set_likes(1); //clicked 1 time
boolean isLiked = dbHandler.incrementLikes(); //increment like in database
printDatabase(); //show db
if(isLiked) { //if like goes into db
Toast.makeText(MainActivity.this, "Liked successfully", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
}
public void dislikeButtonClicked(View view)//dislike button is clicked
{
obj.set_dislikes(1);
boolean isInserted = dbHandler.incrementDislikes();
printDatabase();
if (isInserted) {
Toast.makeText(MainActivity.this, "Added successfully", Toast.LENGTH_LONG).show();
} else
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
}
// print database
public void printDatabase()
{
Cursor res = dbHandler.dbToString();
if(res.getCount() == 0) //if table is empty
{
showCounts.setText("Error Nothing Found!");
return;
}
StringBuffer str = new StringBuffer(); //if not empty
while(res.moveToNext())
{
str.append("Likes: "+res.getString(0)+"\n");
}
showCounts.setText(str.toString()); //display database in textview
}
This is my Calculate class which has getters and setters:
public class Calculate {
private static int _likes;
private static int _dislikes;
public Calculate()
{ }
public int get_likes() {
return this._likes;
}
public void set_likes(int likes) {
_likes += likes;
}
public int get_dislikes() {
return this._dislikes;
}
public void set_dislikes(int dislikes) {
_dislikes += dislikes;
} }//class ends
This is my DBHandler
public class MyDBHandler extends SQLiteOpenHelper{
//Declarations
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "likeCalci.db";
public static final String TABLE_LIKECALCULATOR = "likeCalculator";
public static final String COLUMN_LIKE = "_likes";
public static final String COLUMN_DISLIKE = "_dislikes";
int like_count = 0;
int dislike_count = 0;
//Constructor
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//create table
String query = "CREATE TABLE " + TABLE_LIKECALCULATOR + "("+
COLUMN_LIKE + " INTEGER " + COLUMN_DISLIKE + " INTEGER " + ");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) //upgrade db
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIKECALCULATOR);
onCreate(db);
}
//call when like button is clicked
public boolean incrementLikes()
{
Calculate obj = new Calculate(); //obj
like_count = obj.get_likes(); //get number of likes and store it
ContentValues values = new ContentValues();
values.put(COLUMN_LIKE, like_count); //put values in column
SQLiteDatabase db = getWritableDatabase();
long result = db.insert(TABLE_LIKECALCULATOR, null, values); //insert likes in db
db.close();
if(result == -1) //if insert is unsuccessful
return false;
else //if insert is successful
return true;
}
public boolean incrementDislikes()
{
Calculate obj = new Calculate();
dislike_count = obj.get_dislikes();
ContentValues values = new ContentValues();
values.put(COLUMN_DISLIKE, dislike_count);
SQLiteDatabase db = getWritableDatabase();
long result = db.insert(TABLE_LIKECALCULATOR, null, values);
db.close();
if(result == -1)
return false;
else
return true;
}
//print database
public Cursor dbToString()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_LIKECALCULATOR,null);
return res;
}} //Class dbhandler ends
This is how the program runs: http://ift.tt/1R1vfrr
The dislike column is also not created. I started learning android few days back and this is taking a lot of time. Any times while dealing with Databases?
Aucun commentaire:
Enregistrer un commentaire