I am following this AlarmClock tutorial (project .zip also available on GitHub) on how to build my alarm for Android. I fixed some code that was bad, etc, but it still doesn't work as it should.
I've been getting a lot of SQLiteDatabase leaks and I fixed them (I think), but according to my observations data is still updated wrongly every time I delete an alarm or restart the application.
There are some other minor bugs, like the one where new alarms aren't added to database at all, but that is another problem.
So, I believe problem lies somewhere here:
package com.example.vedran.valarm;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import com.example.vedran.valarm.AlarmContract.Alarm;
import java.util.ArrayList;
import java.util.List;
public class AlarmDBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME = "alarmclock.db";
private static AlarmDBHelper database;
private static final String SQL_CREATE_ALARM =
"CREATE TABLE " + Alarm.TABLE_NAME + " (" +
Alarm._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Alarm.COLUMN_NAME_ALARM_NAME + " TEXT," +
Alarm.COLUMN_NAME_ALARM_TIME_HOUR + " INTEGER," +
Alarm.COLUMN_NAME_ALARM_TIME_MINUTE + " INTEGER," +
Alarm.COLUMN_NAME_ALARM_REPEAT_DAYS + " TEXT," +
Alarm.COLUMN_NAME_ALARM_REPEAT_WEEKLY + " BOOLEAN," +
Alarm.COLUMN_NAME_ALARM_TONE + " TEXT," +
Alarm.COLUMN_NAME_ALARM_ENABLED + " BOOLEAN" + " )";
private static final String SQL_DELETE_ALARM =
"DROP TABLE IF EXISTS" + Alarm.TABLE_NAME;
public AlarmDBHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ALARM);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ALARM);
onCreate(db);
}
public static synchronized AlarmDBHelper getInstance(Context context){
if(database == null){
database = new AlarmDBHelper(context.getApplicationContext());
}
return database;
}
/**
* objasnim kasnije
* #param c
* #return
*/
private AlarmModel populateModel(Cursor c){
AlarmModel model = new AlarmModel();
model.id = c.getLong(c.getColumnIndex(Alarm._ID));
model.name = c.getString(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_NAME));
model.timeHour = c.getInt(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_TIME_HOUR));
model.timeMinute = c.getInt(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_TIME_MINUTE));
model.repeatWeekly = c.getInt(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_REPEAT_WEEKLY)) != 0;
model.alarmTone = c.getString(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_TONE)) != "" ? Uri.parse(c.getString(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_TONE))) : null;
model.isEnabled = c.getInt(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_ENABLED)) != 0;
String[] repeatingDays = c.getString(c.getColumnIndex(Alarm.COLUMN_NAME_ALARM_REPEAT_DAYS)).split(",");
for(int i=0;i<repeatingDays.length;i++){
model.setRepeatingDay(i, !repeatingDays[i].equals("false"));
}
return model;
}
private ContentValues populateContent(AlarmModel model){
ContentValues values = new ContentValues();
values.put(Alarm.COLUMN_NAME_ALARM_NAME, model.name);
values.put(Alarm.COLUMN_NAME_ALARM_TIME_HOUR, model.timeHour);
values.put(Alarm.COLUMN_NAME_ALARM_TIME_MINUTE, model.timeMinute);
values.put(Alarm.COLUMN_NAME_ALARM_REPEAT_WEEKLY, model.repeatWeekly);
values.put(Alarm.COLUMN_NAME_ALARM_TONE, model.alarmTone != null ? model.alarmTone.toString() : "");
values.put(Alarm.COLUMN_NAME_ALARM_ENABLED, model.isEnabled);
StringBuilder buildRepeatingDays = new StringBuilder();
for(int i=0; i<7; i++){
buildRepeatingDays.append(model.getRepeatingDay(i));
buildRepeatingDays.append(",");
}
String repeatingDays = buildRepeatingDays.toString();
values.put(Alarm.COLUMN_NAME_ALARM_REPEAT_DAYS, repeatingDays);
return values;
}
public long createAlarm(AlarmModel model){
ContentValues values = populateContent(model);
SQLiteDatabase db = this.getWritableDatabase();
long stat = db.insert(Alarm.TABLE_NAME, null, values);
db.close();
return stat;
}
public long updateAlarm(AlarmModel model){
ContentValues values = populateContent(model);
SQLiteDatabase db = this.getWritableDatabase();
long stat = db.update(Alarm.TABLE_NAME, values, Alarm._ID + " = " + String.valueOf(model.id), null);
db.close();
return stat;
}
public AlarmModel getAlarm(long id){
SQLiteDatabase db = this.getReadableDatabase();
String select = "SELECT * FROM " + Alarm.TABLE_NAME + " WHERE " + Alarm._ID + " = " + String.valueOf(id);
Cursor c = db.rawQuery(select, null);
if(c.moveToNext()){
AlarmModel model = populateModel(c);
c.close();
db.close();
return model;
}
c.close();
db.close();
return null;
}
public List<AlarmModel> getAlarms(){
SQLiteDatabase db = this.getReadableDatabase();
String select = "SELECT * FROM " + Alarm.TABLE_NAME;
Cursor c = db.rawQuery(select, null);
List<AlarmModel> alarmList = new ArrayList<>();
while(c.moveToNext()){
alarmList.add(populateModel(c));
}
c.close();
db.close();
if(!alarmList.isEmpty()){
return alarmList;
}
return null;
}
public int deleteAlarm(long id) {
SQLiteDatabase db = this.getWritableDatabase();
/*int br = db.delete(Alarm.TABLE_NAME, Alarm._ID + " = ?",
new String[]{String.valueOf(id)});*/
int br = db.delete(Alarm.TABLE_NAME, Alarm._ID + " = " + String.valueOf(id), null);
db.close();
return br;
}
}
And here are some methods (entire Classes and Activities are too huge to add them here, but they are available on GitHub) that deal with my database:
Inside AlarmListActivity
public void setAlarmEnabled(long id, boolean isEnabled){
AlarmManagerHelper.cancelAlarms(this);
AlarmModel model = dbHelper.getAlarm(id);
if(model!=null) {
model.isEnabled = isEnabled;
dbHelper.updateAlarm(model);
}
//mAdapter.setAlarms(dbHelper.getAlarms());
//mAdapter.notifyDataSetChanged();
AlarmManagerHelper.setAlarms(this);
}
public void deleteAlarm(long id) {
final long alarmId = id;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please confirm deletion")
.setTitle("Delete alarm?")
.setCancelable(true)
.setNegativeButton("Cancel", null)
.setPositiveButton("Ok", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Cancel Alarms
AlarmManagerHelper.cancelAlarms(mContext);
//Delete alarm from DB by id
dbHelper.deleteAlarm(alarmId);
//Refresh the list of the alarms in the adaptor
mAdapter.setAlarms(dbHelper.getAlarms());
//Notify the adapter the data has changed
mAdapter.notifyDataSetChanged();
//Set the alarms
AlarmManagerHelper.setAlarms(mContext);
}
}).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
mAdapter.setAlarms(dbHelper.getAlarms());
mAdapter.notifyDataSetChanged();
/*if(dbHelper.getAlarms() == null){
throw new NullPointerException("told ya db was empty!");
}*/
}
}
Related
I am new to android development and can't get this to work. I am trying to populate a listview in my AdminActivity with the int value that I am passing from a previous list view. That int value then needs to be used to populate the ListView with the users that match the int value in the booking column of the users table.
Here is the DBHelper class:
package com.example.shashank.fffffffffffffffffffffffffff;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "Login.db";
public static final String FLIGHTS = "FLIGHTS";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_DESTINATION = "DESTINATION";
public static final String COLUMN_PRICE = "PRICE";
public static final String COLUMN_DEPARTURE_TIME = "DEPARTURE_TIME";
public static final String COLUMN_ARRIVAL_TIME = "ARRIVAL_TIME";
public static final String COLUMN_DURATION = "DURATION";
public static final String COLUMN_AVAILABLE_SEATS = "AVAILABLE_SEATS";
public static final String USERS = "users";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String EMAIL = "email";
public static final String BALANCE = "balance";
public static final String BOOKING = "booking";
public DBHelper(Context context) {
super(context, "Login.db", null, 1);
}
#Override
public void onCreate(SQLiteDatabase MyDB) {
String createTable1 = ("create Table " + USERS + "(" + USERNAME + " TEXT primary key, " + PASSWORD + " TEXT, " + EMAIL + " TEXT UNIQUE, " + BALANCE + " REAL, " + BOOKING + " INTEGER)");
MyDB.execSQL(createTable1);
MyDB.execSQL("CREATE TABLE " + FLIGHTS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DESTINATION + " TEXT, " + COLUMN_PRICE + " REAL, " + COLUMN_DEPARTURE_TIME + " TEXT, " + COLUMN_ARRIVAL_TIME + " TEXT, " + COLUMN_DURATION + " TEXT, " + COLUMN_AVAILABLE_SEATS + " INTEGER)");
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_DESTINATION, "Johannesburg");
insertValues.put(COLUMN_PRICE, 1200);
insertValues.put(COLUMN_DEPARTURE_TIME, "12:00");
insertValues.put(COLUMN_ARRIVAL_TIME, "14:15");
insertValues.put(COLUMN_DURATION, "2:15");
insertValues.put(COLUMN_AVAILABLE_SEATS, 10);
MyDB.insert(FLIGHTS, null, insertValues);
ContentValues insertValues2 = new ContentValues();
insertValues2.put(COLUMN_DESTINATION, "Johannesburg");
insertValues2.put(COLUMN_PRICE, 1000);
insertValues2.put(COLUMN_DEPARTURE_TIME, "16:00");
insertValues2.put(COLUMN_ARRIVAL_TIME, "18:15");
insertValues2.put(COLUMN_DURATION, "2:15");
insertValues2.put(COLUMN_AVAILABLE_SEATS, 22);
MyDB.insert(FLIGHTS, null, insertValues2);
ContentValues insertValues3 = new ContentValues();
insertValues3.put(COLUMN_DESTINATION, "Durban");
insertValues3.put(COLUMN_PRICE, 800);
insertValues3.put(COLUMN_DEPARTURE_TIME, "12:00");
insertValues3.put(COLUMN_ARRIVAL_TIME, "14:00");
insertValues3.put(COLUMN_DURATION, "2:00");
insertValues3.put(COLUMN_AVAILABLE_SEATS, 2);
MyDB.insert(FLIGHTS, null, insertValues3);
ContentValues insertValues4 = new ContentValues();
insertValues4.put(COLUMN_DESTINATION, "Port Elizabeth");
insertValues4.put(COLUMN_PRICE, 700);
insertValues4.put(COLUMN_DEPARTURE_TIME, "08:00");
insertValues4.put(COLUMN_ARRIVAL_TIME, "09:10");
insertValues4.put(COLUMN_DURATION, "1:10");
insertValues4.put(COLUMN_AVAILABLE_SEATS, 0);
MyDB.insert(FLIGHTS, null, insertValues4);
ContentValues insertValues5 = new ContentValues();
insertValues5.put(COLUMN_DESTINATION, "Port Elizabeth");
insertValues5.put(COLUMN_PRICE, 700);
insertValues5.put(COLUMN_DEPARTURE_TIME, "12:00");
insertValues5.put(COLUMN_ARRIVAL_TIME, "13:10");
insertValues5.put(COLUMN_DURATION, "1:10");
insertValues5.put(COLUMN_AVAILABLE_SEATS, 22);
MyDB.insert(FLIGHTS, null, insertValues5);
ContentValues insertValues6 = new ContentValues();
insertValues6.put(COLUMN_DESTINATION, "Durban");
insertValues6.put(COLUMN_PRICE, 900);
insertValues6.put(COLUMN_DEPARTURE_TIME, "14:00");
insertValues6.put(COLUMN_ARRIVAL_TIME, "16:00");
insertValues6.put(COLUMN_DURATION, "2:00");
insertValues6.put(COLUMN_AVAILABLE_SEATS, 11);
MyDB.insert(FLIGHTS, null, insertValues6);
}
#Override
public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
MyDB.execSQL("drop Table if exists " + USERS);
MyDB.execSQL("drop Table if exists " + FLIGHTS);
onCreate(MyDB);
}
public Boolean insertData(String username, String password, String email, Double balance){
SQLiteDatabase MyDB = this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(USERNAME, username);
contentValues.put(PASSWORD, password);
contentValues.put(EMAIL, email);
contentValues.put(BALANCE, balance);
long result = MyDB.insert(USERS, null, contentValues);
if(result==-1) return false;
else
return true;
}
public Boolean checkusername(String username) {
SQLiteDatabase MyDB = this.getWritableDatabase();
Cursor cursor = MyDB.rawQuery("Select * from " + USERS + " where " + USERNAME + " = ?", new String[]{username});
if (cursor.getCount() > 0)
return true;
else
return false;
}
public Boolean checkusernamepassword(String username, String password){
SQLiteDatabase MyDB = this.getWritableDatabase();
Cursor cursor = MyDB.rawQuery("Select * from " + USERS + " where " + USERNAME + " = ? and " + PASSWORD + " = ?", new String[] {username,password});
if(cursor.getCount()>0)
return true;
else
return false;
}
public List<FlightsModel> getEveryone(){
List<FlightsModel> returnList = new ArrayList<>();
String queryString = "SELECT * FROM " + FLIGHTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString, null);
if(cursor.moveToFirst()){
do {
int id = cursor.getInt(0);
String destination = cursor.getString(1);
double price = cursor.getDouble(2);
String departure = cursor.getString(3);
String arrival = cursor.getString(4);
String duration = cursor.getString(5);
int space = cursor.getInt(6);
FlightsModel newFlight = new FlightsModel(id, destination, price, departure, arrival, duration, space);
returnList.add(newFlight);
}while (cursor.moveToNext());
}
else{
}
cursor.close();
db.close();
return returnList;
}
#SuppressLint("Range") // suppress Bug/issue with getColumnIndex
public FlightsModel getFlightById(int id) {
FlightsModel rv;
SQLiteDatabase db = this.getWritableDatabase();
// Uses the query convenience method rather than raw query
Cursor csr = db.query(FLIGHTS,null,COLUMN_ID+"=?",new String[]{String.valueOf(id)},null,null,null);
if (csr.moveToFirst()) {
rv = new FlightsModel(
csr.getInt(csr.getColumnIndex(COLUMN_ID)),
csr.getString(csr.getColumnIndex(COLUMN_DESTINATION)),
csr.getDouble(csr.getColumnIndex(COLUMN_PRICE)),
csr.getString(csr.getColumnIndex(COLUMN_DEPARTURE_TIME)),
csr.getString(csr.getColumnIndex(COLUMN_ARRIVAL_TIME)),
csr.getString(csr.getColumnIndex(COLUMN_DURATION)),
csr.getInt(csr.getColumnIndex(COLUMN_AVAILABLE_SEATS))
);
} else {
rv = new FlightsModel();
}
csr.close();
// No need to close the database (inefficient to keep opening and closing db)
return rv;
}
#SuppressLint("Range")
public UsersModel getPasswordByName(String name){
UsersModel rv;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cr = db.query(USERS, null, USERNAME+"=?", new String[]{name}, null, null, null);
if (cr.moveToFirst()) {
rv = new UsersModel(
cr.getString(cr.getColumnIndex(USERNAME)),
cr.getString(cr.getColumnIndex(PASSWORD)),
cr.getString(cr.getColumnIndex(EMAIL)),
cr.getDouble(cr.getColumnIndex(BALANCE)),
cr.getInt(cr.getColumnIndex(BOOKING))
);
} else rv = new UsersModel();
cr.close();
return rv;
}
public int setBookingByUserName(int bookingAmount, String userName) {
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingAmount);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
}
public double makingPayment(double balance, String userName){
ContentValues cv = new ContentValues();
cv.put(BALANCE,balance);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{userName});
}
public int setAvailableSeatsAfterPayment(int seats, int flightID){
ContentValues cv = new ContentValues();
cv.put(COLUMN_AVAILABLE_SEATS,seats);
return this.getWritableDatabase().update(FLIGHTS,cv,COLUMN_ID+"=?",new String[]{String.valueOf(flightID)});
}
public int cancelBooking(int bookingAmount, String userName){
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingAmount);
return this.getWritableDatabase().update(USERS,cv,USERNAME+"=?",new String[]{String.valueOf(userName)});
}
#SuppressLint("Range")
public UsersModel getUsersModelByName(String userName) {
UsersModel rv = new UsersModel();
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(USERS,null,USERNAME+"=?", new String[]{userName},null,null,null);
if (csr.moveToFirst()) {
rv = new UsersModel(
csr.getString(csr.getColumnIndex(USERNAME)),
csr.getString(csr.getColumnIndex(PASSWORD)),
csr.getString(csr.getColumnIndex(EMAIL)),
csr.getDouble(csr.getColumnIndex(BALANCE)),
csr.getInt(csr.getColumnIndex(BOOKING))
);
}
csr.close();
return rv;
}
public boolean makeBooking(String userName, int bookingId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(BOOKING,bookingId);
return db.update(USERS,cv,USERNAME + "=?", new String[]{userName}) > 0;
}
public String makeBookReceiptFile(String userName, Context context) {
String rcpts_directory = "receipts";
String rv = "";
SQLiteDatabase db = this.getWritableDatabase();
UsersModel currentUser = getUsersModelByName(userName);
FlightsModel currentFlightsModel = new FlightsModel();
if (currentUser.booking > 0) {
currentFlightsModel = getFlightById(currentUser.booking);
if (currentFlightsModel.getId() < 1) {
rv = "INVALID - unable to extract booking for id " + currentUser.booking;
}
} else {
rv = "INVALID - unable to extract user who's name is " + userName;
}
if (rv.length() > 0) return rv;
String rcpt_filename =
currentUser.getName() + "-" +
currentFlightsModel.getDestination() + "-" +
currentFlightsModel.getDeparture_time() + "-" +
currentFlightsModel.getArrival_time()
;
File rcpt = new File(context.getFilesDir().getPath() + File.separatorChar + rcpts_directory + File.separatorChar + rcpt_filename);
rcpt.getParentFile().mkdirs();
try {
FileWriter fw = new FileWriter(rcpt);
fw.write("Invoice for flight:" + "\n" +
"\n" + "-----------------------------------------------" +
"\n" + "User: " + userName +
"\n" + "Destination: " + currentFlightsModel.getDestination()+
"\n" + "Departure time at: " + currentFlightsModel.getDeparture_time() +
"\n" + "Arrival time at: " + currentFlightsModel.getArrival_time() +
"\n" + "Duration of flight: " + currentFlightsModel.getDuration() +
"\n" + "Amount paid: " + "R" + currentFlightsModel.getPrice()
);
fw.flush();
fw.close();
rv = rcpt.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
rv = "IOERROR - " + e.getMessage();
}
return rv;
}
}
Here is the usersmodel class:
package com.example.shashank.fffffffffffffffffffffffffff;
public class UsersModel {
private String name, password, email;
private double balance;
int booking;
public UsersModel(String name, String password, String email, double balance, int booking) {
this.name = name;
this.password = password;
this.email = email;
this.balance = balance;
this.booking = booking;
}
public UsersModel() {
}
public int getBooking() {
return booking;
}
public void setBooking(int booking) {
this.booking = booking;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Override
public String toString() {
return "UsersModel{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
Here is the AdminActivity:
package com.example.shashank.fffffffffffffffffffffffffff;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
public class AdminActivity extends AppCompatActivity {
ListView userList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
userList = findViewById(R.id.userList);
int intValue;
Intent mIntent = getIntent();
intValue = mIntent.getIntExtra("intVariableName", 0);
intValue = intValue + 1;
Toast.makeText(AdminActivity.this, Integer.toString(intValue), Toast.LENGTH_SHORT).show();
}
}
I want to use the intValue and populate the list with all the users where the intValue matches the int value in their booking column.
Any help will be appreciated thank you
First Add a method to DBHelper to get the list of users as a Cursor by the int (BOOKING) such as :-
public Cursor getUsersByBookingNumber(int bookingNumber) {
SQLiteDatabase MyDB = this.getWritableDatabase();
/* return cursor with ALL columns AND a column called _ID which has the rowid */
return MyDB.query(USERS, new String[]{"*","rowid AS " + BaseColumns._ID},BOOKING+"=?",new String[]{String.valueOf(bookingNumber)},null,null,null);
}
Note that Cursor Adapters MUST have a column name _id (BaseColumns._ID resolves to _id) so this is achieved by getting the rowid column AS _id.
A ListView requires an adapter, Cursor Adapters are designed for use with Cursors so SimpleCursorAdapter is used.
An adapter requires a layout that will be used to display each item in the list (an item can consist of multiple views). In this case the stock layout simple_list_item2 will be used (you would probably want to create your own layout). You setup the adapter and then tell the ListView what adapter to use.
All of the above is done in the respective activity. To demonstrate MainActivity has been used, it is :-
public class MainActivity extends AppCompatActivity {
DBHelper dbHelper;
Cursor userListViewCursor; /* The Cursor to be used for the ListView */
SimpleCursorAdapter sca; /* The adapter for the ListView */
ListView userListView; /* The ListView */
int currentBookingNumber; /* the int */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userListView = this.findViewById(R.id.activity_admin);
/* Prepare Database */
dbHelper = new DBHelper(this);
/* Add some test data */
dbHelper.insertData("Fred","password","fred#email",0.00);
dbHelper.insertData("Mary","password","mary#email",0.00);
dbHelper.insertData("Jane","password","jane#email",0.00);
dbHelper.insertData("Tom","password","tom#email",0.00);
dbHelper.insertData("Sue","password","sue#email",0.00);
dbHelper.insertData("Bob","password","bob#email",0.00);
dbHelper.makeBooking("Fred",1);
dbHelper.makeBooking("Mary",2);
dbHelper.makeBooking("Sue",3);
dbHelper.makeBooking("Jane",1);
dbHelper.makeBooking("Tom",2);
dbHelper.makeBooking("Bob",1);
currentBookingNumber = 1; /*(mimic getting the booking number)*/
/* setup the ListView as the adapter will be null */
setupOrRefreshLIstView(currentBookingNumber);
}
private void setupOrRefreshLIstView(int bookingNumber) {
userListViewCursor = dbHelper.getUsersByBookingNumber(bookingNumber);
if (sca == null) {
sca = new SimpleCursorAdapter(
this,
/* The layout for each item in the ListView */
android.R.layout.simple_list_item_2,
userListViewCursor,
/* Columns in the Cursors */
new String[]{DBHelper.USERNAME,DBHelper.EMAIL},
/* id of the View in the ListView's item layout that corresponds with the column in the Cursor*/
/* NOTE 1st column corresponds to first id and so on so :- */
/* USERNAME column value is placed into text1 View, EMAIl column into text2 */
/* simple list item has 2 TextViews with ids text1 and text2 */
new int[]{android.R.id.text1,android.R.id.text2},
0
);
/* Tie the adapter sca to the ListView userListView */
userListView.setAdapter(sca);
/* set listeners here if required e.g. */
userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#SuppressLint("Range")
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l /* will be the value of _id column */) {
Toast.makeText(view.getContext(),
"You clicked on the row with _id " + l
+ " the user is " + userListViewCursor.getString(userListViewCursor.getColumnIndex(DBHelper.USERNAME))
+ " the email is " + userListViewCursor.getString(userListViewCursor.getColumnIndex(DBHelper.EMAIL))
/* even though balance etc is not shown as the values are in the cursor they can be retrieved */
+ " the balance is " + userListViewCursor.getDouble(userListViewCursor.getColumnIndex(DBHelper.BALANCE))
,
Toast.LENGTH_SHORT).show();
}
});
} else {
sca.swapCursor(userListViewCursor);
}
}
/* if the activity is resumed then refresh the ListView just in case the data has changed */
#Override
protected void onResume() {
super.onResume();
setupOrRefreshLIstView(currentBookingNumber);
}
#Override
protected void onDestroy() {
super.onDestroy();
userListViewCursor.close(); /*should always close a cursor when done with it */
}
}
Note that some testing data is generated and loaded to demonstrate.
An onItemClickListener has been added, this will toast the details of the clicked item.
Running the above results in :-
I have one of the weirdest problems I've had so far. I am creating an app which sends information about books and authors through different activities. I am sending them with the help of intents. My problem is within the authors section. I am sending name, age and the picture. Name and age are send successfully and to my knowledge with the debugger, the image is sent successfully as well. However, Picasso refuses to load it in ImageView. There are no errors shown and I have no idea what is going on. Here is some code.
This function resets the information in my database:
public void resetAuthor()
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME_AUTHORS);
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + TABLE_NAME_AUTHORS + "'");
addBegginingInfoAuthor("J.K.Rowling", 54, "https://www.biographyonline.net/wp-content/uploads/2014/05/jk-rowling4.jpg");
addBegginingInfoAuthor("J.R.R. Tolken", 81, "https://www.biography.com/.image/t_share/MTE5NTU2MzE2Mzg4MzYxNzM5/jrr-tolkien-9508428-1-402.jpg");
addBegginingInfoAuthor("Arthur Conan Doyle", 71, "https://upload.wikimedia.org/wikipedia/commons/b/bd/Arthur_Conan_Doyle_by_Walter_Benington%2C_1914.png");
}
AuthorAdapter class. This is the adapter for my recycler view which displays details about the author. It is also where I start up the intent to move to the other activity:
#Override
public void onBindViewHolder(#NonNull AuthorsViewHolder holder, int position) {
if (!mCursor.moveToPosition(position))
{
return;
}
final int author_id = mCursor.getInt(mCursor.getColumnIndex(DatabaseHelper.AUTHORS_COL_1));
final String name = mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.AUTHORS_COL_2));
final String age = mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.AUTHORS_COL_3));
final String image = mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.AUTHORS_COL_4));
holder.authorIndex.setText(String.valueOf(author_id));
holder.authorName.setText(name);
holder.authorAge.setText(String.valueOf(age));
holder.authorName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), displayBooksFromAuthor.class);
intent.putExtra("Author_id", author_id);
intent.putExtra("Author_name", name);
intent.putExtra("Author_age", age);
intent.putExtra("Author_image", image);
mContext.startActivity(intent);
}
});
}
And finally, my DisplayAuthorDetails class which receives the data from the intent and displays it:
int author_id = intent.getIntExtra("Author_id", 0);
String author_name = intent.getStringExtra("Author_name");
String author_age = intent.getStringExtra("Author_age");
String author_picture = intent.getStringExtra("Author_image");
et_name.setText(author_name);
et_age.setText(author_age);
et_id.setText(String.valueOf(author_id));
Picasso.get().load(author_picture).into(author_image);
I have the library dependency and also the two permissions in the manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
implementation 'com.squareup.picasso:picasso:2.71828
The weirdest part of this problem is that when I create a brand new project and try to load an image it loads successfully. Any help is appreciated. Thank you for your time
EDIT
DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Books and Authors.db";
public static final String TABLE_NAME_AUTHORS = "Authors_table";
public static final String AUTHORS_COL_1 = "Author_ID";
public static final String AUTHORS_COL_2 = "Name";
public static final String AUTHORS_COL_3 = "Age";
public static final String AUTHORS_COL_4 = "Author_picture";
public static final String TABLE_NAME_BOOKS = "Books_table";
public static final String BOOKS_COL_1 = "Book_ID";
public static final String BOOKS_COL_2 = "Author_Foreign";
public static final String BOOKS_COL_3 = "Title";
public static final String BOOKS_COL_4 = "Price";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME_BOOKS + " (Book_ID INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT," + "Author_Foreign INT," + "Price TEXT)");
db.execSQL("create table " + TABLE_NAME_AUTHORS + " (Author_ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT," + "Age INTEGER," + "Author_picture TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_AUTHORS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_BOOKS);
onCreate(db);
}
public boolean insertDataBooks(String title, float price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(BOOKS_COL_3, title);
contentValues.put(BOOKS_COL_4, price);
long result = db.insert(TABLE_NAME_BOOKS, null, contentValues);
if (result == 1) {
return false;
} else {
return true;
}
}
public boolean insertDataAuthors(String name, int age) {
SQLiteDatabase mydb = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AUTHORS_COL_2, name);
contentValues.put(AUTHORS_COL_3, age);
long result = mydb.insert(TABLE_NAME_AUTHORS, null, contentValues);
if (result == 1) {
return false;
} else {
return true;
}
}
public boolean addBegginingInfo(int foreign_author_id, String title, double price) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(BOOKS_COL_2, foreign_author_id);
contentValues.put(BOOKS_COL_3, title);
contentValues.put(BOOKS_COL_4, price);
long result = db.insert(TABLE_NAME_BOOKS, null, contentValues);
if (result == 1) {
return false;
} else {
return true;
}
}
public boolean addBegginingInfoAuthor(String name, int age, String image)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(AUTHORS_COL_2, name);
contentValues.put(AUTHORS_COL_3, age);
contentValues.put(AUTHORS_COL_4, image);
long result = db.insert(TABLE_NAME_AUTHORS, null, contentValues);
if (result == 1) {
return false;
} else {
return true;
}
}
public Integer deleteBook(String index) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME_BOOKS, "Book_ID = ?", new String[]{index});
}
public Integer deleteAuthor(String authorIndex) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME_AUTHORS, "Author_ID = ?", new String[]{authorIndex});
}
public void resetBookTable() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME_BOOKS);
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + TABLE_NAME_BOOKS + "'");
addBegginingInfo(1, "Harry Potter", 9.99);
addBegginingInfo(1, "Fantastic Beasts", 14.99);
addBegginingInfo(2, "Lord of the Rings", 8.99);
addBegginingInfo(2, "The Hobbit", 12.99);
addBegginingInfo(3, "Sherlock Holmes", 6.99);
addBegginingInfo(3, "Valley of Fear", 7.99);
}
public void resetAuthor()
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME_AUTHORS);
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + TABLE_NAME_AUTHORS + "'");
addBegginingInfoAuthor("J.K.Rowling", 54, "https://www.biographyonline.net/wp-content/uploads/2014/05/jk-rowling4.jpg");
addBegginingInfoAuthor("J.R.R. Tolken", 81, "https://www.biography.com/.image/t_share/MTE5NTU2MzE2Mzg4MzYxNzM5/jrr-tolkien-9508428-1-402.jpg");
addBegginingInfoAuthor("Arthur Conan Doyle", 71, "https://upload.wikimedia.org/wikipedia/commons/b/bd/Arthur_Conan_Doyle_by_Walter_Benington%2C_1914.png");
}
}
After many hours of trying I finally found out that to fix this issue I just had to re-install the app on the emulator.
Heyhey :)
I looked at several Questions to the same topic, but I found no solution to my problem.
A NullPointerException at this.getReadableDatabase(); appears...
Here's my Code:
public class DatabaseHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "TODO_APP";
/*--------------------------- TABLE ITEMS START ---------------------------*/
private static final String TABLE_ITEMS = "items";
private static String KEY_ID_ITEMS = "id_items";
private static final String KEY_CATEGORY_ITEMS = "category";
private static final String KEY_DATETIME_ITEMS = "datetime";
private static String KEY_NAME_ITEMS = "name";
private static final String KEY_DESCRIPTION_ITEMS = "description";
private static final String KEY_ALARM_ITEMS = "alarm";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// create table ITEMS
String CREATE_ITEMS_TABLE = "CREATE TABLE " + TABLE_ITEMS + "("
+ KEY_ID_ITEMS + " INTEGER PRIMARY KEY," + KEY_CATEGORY_ITEMS
+ " INTEGER," + KEY_DATETIME_ITEMS
+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP," + KEY_NAME_ITEMS
+ " TEXT," + KEY_DESCRIPTION_ITEMS + " TEXT," + KEY_ALARM_ITEMS
+ " INTEGER" + ");";
db.execSQL(CREATE_ITEMS_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);
// Create tables again
this.onCreate(db);
}
public void addItem(DB_Item item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY_ITEMS, item.getCategory());
values.put(KEY_NAME_ITEMS, item.getName());
values.put(KEY_DESCRIPTION_ITEMS, item.getDescription());
values.put(KEY_ALARM_ITEMS, item.getAlarm());
// Inserting Row
db.insert(TABLE_ITEMS, null, values);
db.close(); // Closing database connection
}
public DB_Item getItem(String name) {
//!!!!! Here is one problem !!!!!
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_ITEMS, new String[] { KEY_ID_ITEMS,
KEY_CATEGORY_ITEMS, KEY_DATETIME_ITEMS, KEY_NAME_ITEMS,
KEY_DESCRIPTION_ITEMS, KEY_ALARM_ITEMS },
KEY_NAME_ITEMS = " = ?", new String[] { String.valueOf(name) },
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
// DB_Item (int category, String name, String description, int
// alarm)
DB_Item item = new DB_Item(cursor.getInt(1), cursor.getString(3),
cursor.getString(4), cursor.getInt(5));
return item;
}
public List<DB_Item> getAllItems() {
List<DB_Item> itemList = new ArrayList<DB_Item>();
// SELECT ALL
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
//!!!!!!!!!! here is the other Problem
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
// go through all rows and then adding to list
if (cursor.moveToFirst()) {
do {
DB_Item item = new DB_Item();
item.setCategory(Integer.parseInt(cursor.getString(0)));
// TODO
// adding to list
itemList.add(item);
} while (cursor.moveToNext());
}
return itemList;
}
[and so on, didn't copy the whole code]
Hope you can help me...
The error appears definitely at SQLiteDatabase db = this.getReadableDatabase();
Output:
09-03 08:34:17.863: E/AndroidRuntime(7074): java.lang.RuntimeException: Unable to start activity ComponentInfo{todo.todo_list/todo.view.StartApp}: java.lang.NullPointerException
09-03 08:34:17.863: E/AndroidRuntime(7074): at todo.database.DatabaseHandler.getAllItems(DatabaseHandler.java:144)
09-03 08:34:17.863: E/AndroidRuntime(7074): at todo.helper.SortItems.sortItemsCategory(SortItems.java:16)
09-03 08:34:17.863: E/AndroidRuntime(7074): at todo.view.StartApp.onCreate(StartApp.java:36)
chnage you class to something like this:
package com.mhp.example;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
/*--------------------------- TABLE ITEMS START ---------------------------*/
private static final String TABLE_ITEMS = "items";
private static String KEY_ID_ITEMS = "id_items";
private static final String KEY_CATEGORY_ITEMS = "category";
private static final String KEY_DATETIME_ITEMS = "datetime";
private static String KEY_NAME_ITEMS = "name";
private static final String KEY_DESCRIPTION_ITEMS = "description";
private static final String KEY_ALARM_ITEMS = "alarm";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "dbname";
private static final int DATABASE_VERSION = 1;
tring CREATE_ITEMS_TABLE = "CREATE TABLE " + TABLE_ITEMS + "("
+ KEY_ID_ITEMS + " INTEGER PRIMARY KEY," + KEY_CATEGORY_ITEMS
+ " INTEGER," + KEY_DATETIME_ITEMS
+ " TIMESTAMP DEFAULT CURRENT_TIMESTAMP," + KEY_NAME_ITEMS
+ " TEXT," + KEY_DESCRIPTION_ITEMS + " TEXT," + KEY_ALARM_ITEMS
+ " INTEGER" + ");";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(CREATE_ITEMS_TABLE);
} catch (SQLException e) {
e.printStackTrace();
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public void addItem(DB_Item item) {
ContentValues values = new ContentValues();
values.put(KEY_CATEGORY_ITEMS, item.getCategory());
values.put(KEY_NAME_ITEMS, item.getName());
values.put(KEY_DESCRIPTION_ITEMS, item.getDescription());
values.put(KEY_ALARM_ITEMS, item.getAlarm());
// Inserting Row
db.insert(TABLE_ITEMS, null, values);
}
public DB_Item getItem(String name) {
Cursor cursor = db.query(TABLE_ITEMS, new String[] { KEY_ID_ITEMS,
KEY_CATEGORY_ITEMS, KEY_DATETIME_ITEMS, KEY_NAME_ITEMS,
KEY_DESCRIPTION_ITEMS, KEY_ALARM_ITEMS },
KEY_NAME_ITEMS = " = ?", new String[] { String.valueOf(name) },
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
// DB_Item (int category, String name, String description, int
// alarm)
DB_Item item = new DB_Item(cursor.getInt(1), cursor.getString(3),
cursor.getString(4), cursor.getInt(5));
return item;
}
public List<DB_Item> getAllItems() {
List<DB_Item> itemList = new ArrayList<DB_Item>();
// SELECT ALL
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
Cursor cursor = db.rawQuery(selectQuery, null);
// go through all rows and then adding to list
if (cursor.moveToFirst()) {
do {
DB_Item item = new DB_Item();
item.setCategory(Integer.parseInt(cursor.getString(0)));
// TODO
// adding to list
itemList.add(item);
} while (cursor.moveToNext());
}
return itemList;
}
}
NOTE: when you create object from DBAdapter,you should open() db and after your work close() it.
DBAdapter db = new DBAdapter(contex);
db.open();
//do you work
db.close();
Just use getReadableDatabase() without this.
When I add data into sqlite and open it for the first time, the data inside will be displayed. However, if I try opening it again nothing will show up even though there is data inside. May I know what is the problem?
Class that performs the activity:
public class Watchlist extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private String tableName = DatabaseHandler.TABLE_ITEM;
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("This data is retrieved from sqlite");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
DatabaseHandler db = new DatabaseHandler(this.getApplicationContext());
newDB = db.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT * FROM " + tableName, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String pid = c.getString(c.getColumnIndex("id"));
String name = c.getString(c.getColumnIndex("name"));
String price = c.getString(c.getColumnIndex("price"));
String date = c.getString(c.getColumnIndex("created_at"));
Log.d("pid",pid);
results.add("Name: " + name + "\n Price: " + price + "\n Date posted: " + date);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}
}
}
Database handler:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "itemManager";
// table name
public static final String TABLE_ITEM = "item";
// Item Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_CREATED_AT = "created_at";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ITEM_TABLE = "CREATE TABLE " + TABLE_ITEM + "("
+ KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
+ KEY_NAME + " TEXT, "
+ KEY_PRICE + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_ITEM_TABLE);
}
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEM);
// Create tables again
onCreate(db);
}
/**
* Storing item details in database
* */
public void addItem(Items item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, item.getName()); // item name
values.put(KEY_PRICE, item.getPrice()); // item price
values.put(KEY_CREATED_AT, item.getDate()); // Created At
// Inserting Row
db.insert(TABLE_ITEM, null, values);
db.close(); // Closing database connection
}
/**
* Getting item data from database
* */
public List<Items> getAllItems(){
List<Items> itemList = new ArrayList<Items>();
String selectQuery = "SELECT * FROM " + TABLE_ITEM;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
if (cursor.moveToFirst()) {
do {
Items item = new Items();
item.setID(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setPrice(cursor.getString(2));
item.setDate(cursor.getString(2));
// Adding item to list
itemList.add(item);
} while (cursor.moveToNext());
}
// return item list
return itemList;
}
/**
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_ITEM;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
// Updating item
public int updateItem(Items item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, item.getName());
values.put(KEY_PRICE, item.getPrice());
values.put(KEY_CREATED_AT, item.getDate());
// updating row
return db.update(TABLE_ITEM, values, KEY_ID + " = ?",
new String[] { String.valueOf(item.getID()) });
}
// Deleting item
public void deleteItem(Items item) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ITEM, KEY_ID + " = ?",
new String[] { String.valueOf(item.getID()) });
db.close();
}
}
Well, don't you delete all the records before closing the database?
} finally {
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}
That would explain that behaviour.
I am trying to update current credits column of the only row in the database using a drop down spinner which gets values from an arraylist. Very unsure about how to go about doing this operation. Thank you for any help in advance.
My database code:
package com.example.parkangel;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class UDbHelper extends SQLiteOpenHelper
{
public static final String KEY_ROWID = "_id";
public static final String KEY_PFNAME = "payeeFname";
public static final String KEY_PSNAME = "payeeSname";
public static final String KEY_CARD = "card";
public static final String KEY_CREDITS = "credits";
private static final String DATABASE_NAME = "UserData.db";
private static final String DATABASE_TABLE = "UserTable";
private static final int DATABASE_VERSION = 1;
//private UDbHelper dbHelper;
//private final Context ourContext;
private static UDbHelper instance;
private SQLiteDatabase ourDatabase;
public UDbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static UDbHelper getInstance(Context context)
{
if (instance == null)
{
instance = new UDbHelper(context);
}
return instance;
}
#Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PFNAME + " TEXT NOT NULL, " + KEY_PSNAME + "
TEXT NOT NULL, " +
KEY_CARD + " TEXT NOT NULL, " + KEY_CREDITS + " TEXT
NOT NULL);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public synchronized UDbHelper open() throws SQLException
{
System.out.println ("running open");
if(ourDatabase == null || !ourDatabase.isOpen())
ourDatabase = getWritableDatabase();
return this;
}
public String getData()
{
// TODO Auto-generated method stub
String[] columns = new String[] {KEY_ROWID, KEY_PFNAME, KEY_PSNAME,
KEY_CARD, KEY_CREDITS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null,
null, null, null);
String result = " ";
int iRow = c.getColumnIndexOrThrow(KEY_ROWID);
int iPFname = c.getColumnIndexOrThrow(KEY_PFNAME);
int iPSname = c.getColumnIndexOrThrow(KEY_PSNAME);
int iCard = c.getColumnIndexOrThrow(KEY_CARD);
int iCredits = c.getColumnIndexOrThrow(KEY_CREDITS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " +
c.getString(iPFname) + " " +
c.getString(iPSname)
+ " " + c.getString(iCard) + " " +
c.getString(iCredits) + "\n";
}
return result;
}
}
My main activity code I will be doing the operation through:
package com.example.parkangel;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class Balance extends Activity{
Button add;
TextView display;
Spinner spinner3;
String[] money = {"Select amount", "£1", "£2", "£5", "£10"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_layout);
TextView tv = (TextView) findViewById(R.id.firstn);
UDbHelper db = new UDbHelper(this);
db.open();
String data = db.getData();
db.close();
tv.setText(data);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(Balance.this,
android.R.layout.simple_spinner_item, money);
spinner3 = (Spinner) findViewById (R.id.moneytoadd);
spinner3.setAdapter(adapter3);
add = (Button) findViewById(R.id.topup);
}
public void onClick(View arg0)
{
}
public void updateActivity(View view){
Intent book = new Intent(Balance.this, BookTicket.class);
startActivity(book);
}
public void addBalance(View view){
Intent addB = new Intent(Balance.this, Balance.class);
startActivity(addB);
}
public void doUpdate(View view){
Intent upd = new Intent(Balance.this, UpdateTicket.class);
startActivity(upd);
}
}
Your question is fairly compound, getting selected field from spinner, updating database... I'm not going to provide a complete answer, but this should get you started:
This is how you would get the selected field from the spinner, which you can then use to update your database. One word of warning, I believe setOnItemSelectedListener is called when it is initially set, so you may need to ignore the first call.
Spinner spinner;
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2,
long arg3) {
if(! (view instanceof TextView)){
// view is probably a textview, but record type if not.
System.out.println("incorrect view type " + view.getClass().getSimpleName());
return;
}
EditText et = (EditText) view;
String fieldName = et.getText().toString().trim();
//Now we got selected name, send name
//to a function that updates our database.
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I didn't find the problem but I can tell you that your code is VERY INEFFICIENT. You are using String to build your result in the getData method. Each time you try to append the new data (Row) to the old one you are creating a new string to hold the new data. If you are retrieving 1000 row from the database, this means that you are creating 1000 string to build the final one. I recommend using StringBuilder instead as following:
StringBuilder strBuilder= new StringBuilder();
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
strBuilder.append( c.getString(iRow) + " " +
c.getString(iPFname) + " " +
c.getString(iPSname)
+ " " + c.getString(iCard) + " " +
c.getString(iCredits) + "\n");
}
return str= strBuilder.toString();
Here is an example of how to insert into your DB:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PFNAME , editText1.getText().toString());
values.put(KEY_PSNAME , stringArrayList.get(position));
values.put(KEY_CARD , "12345677");
values.put(KEY_CREDITS , "55");
// Inserting Row
db.insert(DATABASE_TABLE, null, values);
db.close(); // Closing database connection