Toast won't show inside Android DatePickerDialog - firebase

I have pickDateTime function, that restricts the user of choosing times that are in past. Current time - is the "minimum" time allowed.
This is the function:
public void pickDateTime(View view) {
Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
currentMonth = c.get(Calendar.MONTH);
currentDay = c.get(Calendar.DAY_OF_MONTH);
currentHour = c.get(Calendar.HOUR_OF_DAY);
currentMinute = c.get(Calendar.MINUTE);
DatePickerDialog datePickerDialog = new DatePickerDialog(NewMissionUser.this
, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
Log.d(TAG,"Chose: year: " + year + " , month: " + (month+1) + " day: " + day);
missionYear = year;
missionMonth = (month+1);
missionDay = day;
TimePickerDialog timePickerDialog = new TimePickerDialog(NewMissionUser.this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
missionTime = missionDay+"/"+missionMonth+"/"+missionYear + " " +hourOfDay + ":" + minute;
Log.d(TAG,"missionTime: " + missionTime);
if (isPast(missionTime)){
Log.d(TAG,"mission time in past!");
Toast.makeText(getApplicationContext(), "APP Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
Toast.makeText(NewMissionUser.this, "Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
Toast.makeText(getBaseContext(), "BASE Mission inserted in past, ignoring date, default date is now.",Toast.LENGTH_LONG);
missionTime = currentDay+"/"+currentMonth+"/"+currentYear + " " +currentHour + ":" + currentMinute;
}
}
},currentHour, currentMinute, true);
timePickerDialog.show();
}
},currentYear, currentMonth, currentDay);
datePickerDialog.show();
}
This is the class:
public class NewMissionUser extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener, GmapFragmentNewMission.TextClicked
The problem is, that the Toast won't show up in either of these tries.
The Log DOES appear, and in debug I see that the code reaches this point and isPast function is behaving as expected.
Why the Toast won't come up? Thanks.

You're missing the call to show() the Toast:
Toast.makeText(getApplicationContext(), "...",Toast.LENGTH_LONG).show();

Related

How to Update/Delete with elements from two different tables SQLite

I am working on a student grade submission program that accepts the following inputs: Student ID, Student First Name, Student Last Name, Class ID, Class Name, Grade Points, and Letter Grade. In order to prevent data redundancy I have created three different tables. However when attempting to "modify" or "delete" an entry with two values from distinct tables I am running into a problem. (For instance delete WHERE student.id is equal to the student.id in STUDENT_TABLE and WHERE class.id is equal to the class.id in the CLASS_TABLE)
With that being said, here is my current Modify code for Modifying a Student Class. I need to modify in a way in which the student.id and class.id are associated (Which are Located in two different Tables)
modifyclass.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(studentid.getText().toString().trim().length()==0 || classid.getText().toString().trim().length()==0 || classname.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Student & Class ID to update class. \n\nAll other Field Entries will be ignored");
return;
}
Cursor c=db.rawQuery("SELECT * FROM CLASS_TABLE WHERE classid='"+classid.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE CLASS_TABLE SET classid='"+classid.getText()+"',classname='"+classname.getText()+"' WHERE studentid='"+studentid.getText()+"' AND classid='"+classid.getText()+"'");
showMessage("Success", "Class Record Modified");
}
else
{
showMessage("Error", "Invalid First and Last name or Class ID");
}
clearText();
}
});
EDIT:
Here are my tables for reference:
db=openOrCreateDatabase("STUDENTGRADES", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS STUDENT_TABLE (studentid TEXT, fname TEXT, lname TEXT)");
db.execSQL("CREATE TABLE IF NOT EXISTS CLASS_TABLE(classid TEXT PRIMARY KEY, classname TEXT UNIQUE)");
db.execSQL("CREATE TABLE IF NOT EXISTS GRADE_TABLE (studentid TEXT, classid TEXT, pointgrade INTEGER, lettergrade TEXT)");
You should never need to update multiple tables at once at least for a single change e.g. student's name has changed (student table would be changed) or say a grade changed (change the student's respective grade entry).
So I believe what would best suit are methods to update a Student's details (first or last name or both) and a method to alter the grades (again either or both).
As for deletion you could do this sequentially delete from the grades table and the from the student table or vice-versa.
As such I believe the following code includes methods deleteStudentInfo, changeStudentName, changeStudentFirstName, changeStudentLastName and changeStudentGrade and also includes example usage along with creating and populating the tables (you may wish to consider the revised schema):-
public class MainActivity extends AppCompatActivity {
public static final String DBNAME = "study";
public static final String STUDENT_TABLE_NAME = "STUDENT_TABLE";
public static final String COL_STUDENT_ID = "studentid";
public static final String COL_STUDENT_FIRSTNAME = "fname";
public static final String COL_STUDENT_LASTNAME = "lname";
public static final String CLASS_TABLE_NAME = "CLASS_TABLE";
public static final String COL_CLASS_ID = "classid";
public static final String COL_CLASS_NAME = "classname";
public static final String GRADE_TABLE_NAME = "GRADE_TABLE";
public static final String COL_GRADE_POINTGRADE = "pointgrade";
public static final String COL_GRADE_LETTERGRADE = "lettergrade";
public static final String BY_STUDENTID = COL_STUDENT_ID + "=?";
public static final String BY_CLASSID = COL_CLASS_ID + "=?";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase(DBNAME,Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS " + STUDENT_TABLE_NAME + " (" +
COL_STUDENT_ID + " TEXT PRIMARY KEY, " +
COL_STUDENT_FIRSTNAME + " TEXT," +
COL_STUDENT_LASTNAME + " TEXT)"
);
db.execSQL("CREATE TABLE IF NOT EXISTS " + CLASS_TABLE_NAME + "(" +
COL_CLASS_ID + " TEXT PRIMARY KEY," +
COL_CLASS_NAME + " TEXT UNIQUE " +
")"
);
db.execSQL("CREATE TABLE IF NOT EXISTS " + GRADE_TABLE_NAME + "(" +
COL_STUDENT_ID + " TEXT, " +
COL_CLASS_ID + " TEXT, " +
COL_GRADE_POINTGRADE + " INTEGER, " +
COL_GRADE_LETTERGRADE + " TEXT" +
")"
);
db.execSQL("INSERT OR IGNORE INTO " + STUDENT_TABLE_NAME +
" VALUES" +
"('00001','Fred','Smith')," +
"('00010','Mary','Thomas')," +
"('00910','Angela','Jones')"
);
db.execSQL("INSERT OR IGNORE INTO " + CLASS_TABLE_NAME +
" VALUES" +
"('001','English')," +
"('101','Mathematics')," +
"('201','Chemistry')"
);
db.execSQL("INSERT OR IGNORE INTO " + GRADE_TABLE_NAME +
" VALUES" +
" ('00001','001',99,'A'), -- Fred Smith has 99 point grade as an A in English\n" +
" ('00001','101',25,'F'), -- Fred Smith has 25 point grade as an F on Mathematics\n" +
" ('00010','201',76,'B'), -- Angela Jones 76 a B in Chemistry\n" +
" ('00910','101',50,'C'), \n" +
" ('00910','201',63,'C'),\n" +
" ('00910','001',89,'A')\n" +
";"
);
changeStudentName(db,"00001","Joe","Bloggs");
changeStudentFirstName(db,"00001","Harry");
changeStudentLastName(db,"00001","Hoffmann");
// e.g. won't change due to -1 (skip pointsgrade) and null (skip lettergrade)
changeStudentGrade(db,"00001","001",-1,null);
// Change both
changeStudentGrade(db,"00001","001",25,"D");
changeStudentGrade(db,"00001","001",27,null);
// Ooops invalid student id
if (deleteStudentInfo(db,"001")) {
Log.d("DELETION","Student 001 deleted.");
} else {
Log.d("DELETION","Ooops Student 001 not deleted?????");
}
// Corrected Student ID
if (deleteStudentInfo(db,"00001")) {
Log.d("DELETION","Student 001 deleted.");
} else {
Log.d("DELETION","Ooops Student 001 not deleted?????");
}
}
private boolean deleteStudentInfo(SQLiteDatabase db, String studentid) {
String tag = "STUDENT_DELETE";
String student_table = "STUDENT_TABLE";
String grade_table = "GRADE_TABLE";
long pre_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
long pre_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);
String whereclause = "studentid =?";
String[] whereargs = {studentid};
db.delete(student_table,whereclause,whereargs);
db.delete(grade_table,whereclause,whereargs);
long post_delete_student_count = DatabaseUtils.queryNumEntries(db,student_table);
long post_delete_grade_count = DatabaseUtils.queryNumEntries(db,grade_table);
Log.d(
tag,
"Number of Students deleted from " +
student_table + " is " +
String.valueOf(
pre_delete_student_count - post_delete_student_count
));
Log.d(
tag,
"Number of Grades deleted from " + grade_table + " is " +
String.valueOf(
pre_delete_grade_count - post_delete_grade_count
)
);
if ((pre_delete_student_count + pre_delete_grade_count) != (post_delete_student_count + post_delete_grade_count)) {
return true;
}
return false;
}
/**
* Flexible Student Name Change
*
* #param db The SQliteDatabase
* #param studentid The studentid (String)
* #param newfirstname The new firstname, null or blank to leave as is
* #param newlastname the new lastname, null or blank to leave as is
*/
private void changeStudentName(SQLiteDatabase db, String studentid, String newfirstname, String newlastname ) {
//Anything to do? if not do nothing
if ((newfirstname == null || newfirstname.length() < 1) && (newlastname == null || newlastname.length() < 1)) {
return;
}
ContentValues cv = new ContentValues();
if (newfirstname != null && newfirstname.length() > 0) {
cv.put(COL_STUDENT_FIRSTNAME,newfirstname);
}
if (newlastname != null && newlastname.length() > 0) {
cv.put(COL_STUDENT_LASTNAME,newlastname);
}
// Overcautious check
if (cv.size() < 1) {
return;
}
db.update(STUDENT_TABLE_NAME,cv,BY_STUDENTID,new String[]{studentid});
}
/**
* Change a Student's First Name (invokes changeStudentName method)
* #param db The SQLiteDatabase
* #param studentid The student's id (String)
* #param newfirstname The new first name to apply
*/
private void changeStudentFirstName(SQLiteDatabase db, String studentid, String newfirstname) {
changeStudentName(db,studentid,newfirstname,null);
}
/**
* Change a Student's Last Name (invokes changeStudentName method)
* #param db
* #param studentid
* #param newlastname
*/
private void changeStudentLastName(SQLiteDatabase db, String studentid, String newlastname) {
changeStudentName(db,studentid,null,newlastname);
}
/**
* Change a students grade (allowing just one (points/letter))
* #param db
* #param studentid
* #param classid
* #param newpointsgrade
* #param newlettergrade
*/
private void changeStudentGrade(SQLiteDatabase db, String studentid, String classid, int newpointsgrade, String newlettergrade) {
// Anything to do? if not do nothing
if (newpointsgrade < 0 && (newlettergrade == null || newlettergrade.length() < 1)) {
return;
}
ContentValues cv = new ContentValues();
if (newpointsgrade >= 0) {
cv.put(COL_GRADE_POINTGRADE,newpointsgrade);
}
if (newlettergrade != null && newlettergrade.length() > 0) {
cv.put(COL_GRADE_LETTERGRADE,newlettergrade);
}
String whereclause = COL_STUDENT_ID + "=? AND " + COL_CLASS_ID + "=?";
String[] whereargs = new String[]{studentid,classid};
db.update(GRADE_TABLE_NAME,cv,whereclause,whereargs);
}
}

How to get data row by date wise where i pass date picker's date and store data as long format date

When i call getAllInvoice(Date date) to get observable invoice list by passing date from date picker but it's return empty list please help how to get invoice observable list passing date picker's date where i store date as long format in database that example i provided below.
public VBox getBody() {
DatePicker datePicker = new DatePicker();
datePicker.setPrefWidth(width);
datePicker.setValue(LocalDate.now());
Calendar calendar = Calendar.getInstance();
calendar.set(datePicker.getValue().getYear(),datePicker.getValue().getMonthValue(),datePicker.getValue().getDayOfMonth());
ListView<String> invoiceListView = new ListView<>();
invoiceListView.setPrefWidth(width);
invoiceListView.setPrefHeight(550);
ObservableList<Invoice> invoiceObservableList = connection.getAllInvoice(Date.valueOf(datePicker.getValue()));
datePicker.valueProperty().addListener((observable, oldValue, newValue) -> {
invoiceObservableList.clear();
invoiceObservableList.addAll(connection.getAllInvoice(Date.valueOf(datePicker.getValue())));
});
ListChangeListener<Invoice> listener = new ListChangeListener<Invoice>() {
#Override
public void onChanged(Change<? extends Invoice> c) {
ObservableList<String> invoiceList = FXCollections.observableArrayList();
for (Invoice invoice : invoiceObservableList) {
Customer customer = connection.getCustomerById(invoice.getCustomerId());
invoiceList.add("#" + invoice.getId() + " - " + customer.getName() + " (" + customer.getNumber() + ")");
}
invoiceListView.setItems(invoiceList);
}
};
invoiceObservableList.addListener(listener);
ObservableList<String> invoiceList = FXCollections.observableArrayList();
for (Invoice invoice : invoiceObservableList) {
Customer customer = connection.getCustomerById(invoice.getCustomerId());
invoiceList.add("#" + invoice.getId() + " - " + customer.getName() + " (" + customer.getNumber() + ")");
}
invoiceListView.setItems(invoiceList);
VBox invoicePreview = new VBox();//addMe
invoicePreview.setPrefWidth(360);
invoicePreview.setPrefHeight(560);
invoicePreview.setAlignment(Pos.CENTER);
//invoicePreview.setStyle("-fx-background-color: green");
//invoicePreview.setPadding(new Insets(15));
invoiceListView.setOnMouseClicked(event -> {
if (event.getClickCount() == 2) {
invoicePreview.getChildren().clear();
invoicePreview.setAlignment(Pos.TOP_CENTER);
Invoice invoice = invoiceObservableList.get(invoiceListView.getSelectionModel().getSelectedIndex());
InvoicePreview preview = new InvoicePreview(invoice,sizer.getValue(100));
invoicePreview.getChildren().add(preview.printPreview());
}
});
Label massage = new Label("Please select invoice first.");
massage.setFont(new Font(invoiceWidth*5/100));
invoicePreview.getChildren().add(massage);
VBox leftVBox = new VBox(datePicker, invoiceListView);
leftVBox.setSpacing(5);
HBox mainBody = new HBox(leftVBox,invoicePreview);
mainBody.setSpacing(5);
VBox root = new VBox(mainBody);
root.setId("body");
return root;
}
and this is the Connection class's getAllInvoice(Date date) method that return observable invoice list,
public ObservableList<Invoice> getAllInvoice(Date date) {
long longDate = date.getTime();
ObservableList<Invoice> list = FXCollections.observableArrayList();
String query = "SELECT * FROM " + TABLE_INVOICE + " WHERE " + INVOICE_DATE + " LIKE \'"+longDate+"\'";
System.out.println(query);
try {
connection = DriverManager.getConnection("jdbc:sqlite:" + DATA_BASE);
Statement state = connection.createStatement();
ResultSet resultSet = state.executeQuery(query);
while (resultSet.next()) {
list.add(new Invoice(resultSet.getInt(INVOICE_ID), resultSet.getInt(INVOICE_USER_ID), resultSet.getInt(INVOICE_CUSTOMER_ID), resultSet.getLong(INVOICE_DATE)));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
return list;
}
datadase file like as:
"id" "uId" "cusId" "date"
-------------------------------
"3" "1" "4" "1524636334412"
"4" "1" "4" "1524636355419"
"5" "1" "3" "1524636411858"
"6" "1" "3" "1524637462701"
"7" "1" "4" "1524638110920"
how can i get data passing Date as a argument?
Assuming that you want records for the same date (day of the month of the year) as the date passed (long) then you could use :-
SELECT *
FROM invoice
WHERE date / 86400000 = <your_date> / 86400000
<your_date> being the date passed
This is stripping of the milliseconds (i.e. divide by 1000), then stripping of the seconds (i.e divide by another 60), then stripping off the minutes (i.e. divide by another 60) and then stripping of the hours (divide by 24)
i.e. 1000 * 60 * 60 * 24 = 86400000
Based upon your example data then (where 1524636334412 is the datetime of the first row):-
SELECT *
FROM invoice
WHERE date / 86400000 = 1524636334412 / 86400000
results in :-
As such you could change :-
String query = "SELECT * FROM " + TABLE_INVOICE + " WHERE " + INVOICE_DATE + " LIKE \'"+longDate+"\'";
to be
String query = "SELECT * FROM " + TABLE_INVOICE + " WHERE " + INVOICE_DATE + " / 86400000 = " + longDate + " / 86400000";

Android Studio search existing sqlite db

So im developing an app and I want the user to search for a food item in my database and then to be returned with a list view of all food names that match the search criteria. I already have my slqite database created and added to the assets folder. The database is called foodDatabase.db
I have created the Database Helper like so:
package com.example.codebind.databasedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by tom on 15/03/2018.
*/
public class FoodDatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "FoodDatabase.db";
public static final String TABLE_NAME = "dataset";
public static final String COL_1 = "ID";
public static final String COL_2 = "Food";
public static final String COL_3 = "Description";
public static final String COL_4 = "Protein";
public static final String COL_5 = "Fat";
public static final String COL_6 = "Carbohydrate";
public static final String COL_7 = "Energy";
public static final String COL_8 = "Starch";
public static final String COL_9 = "Sugar";
public static final String COL_10 = "Cholesterol";
public FoodDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + TABLE_NAME +" (ID INTEGER
PRIMARY KEY AUTOINCREMENT,FOOD TEXT,DESCRIPTION TEXT,PROTEIN BLOB,FAT
BLOB,CARBOHYDRATE BLOB,ENERGY BLOB,STARCH BLOB,SUGAR BLOB,CHOLESTEROL BLOB)
");
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
and currently my MainActivity.java is clean. I haven't touched it yet.
I'm asking for guidance on how to add a listener so that when the user enters a food name the app will return all foods from the foodDatabase.db that meets the search query.
Thanks
The basis of your search will be a query based upon the FOOD column (I believe).
e.g. SELECT * FROM dataset WHERE FOOD LIKE '%your_food%'
For example assume that your database has data such as (note only food and id columns have been populated with data) :-
Then the query SELECT * FROM dataset WHERE FOOD LIKE '%mash%' would result in :-
i.e. Foods with an id of 2 and 3 contain the food mash
You could run this query using the SQLiteDatabase query method. The query method returns a Cursor with the extracted data. So a method in your DatabaseHelper could be :-
public Cursor getFoodsWithProvidedFood(String provided_food) {
return this.getWritableDatabase().query(
TABLE_NAME,
null,
COL_2 + " LIKE '%" + provided_food + "%' ",
null,
null,
null,
null
);
}
Converting this into a complete but very basic App you could have :-
The Database Helper - FoodDatabaseHelper.java
public class FoodDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "FoodDatabase.db";
public static final String TABLE_NAME = "dataset";
public static final String COL_1 = "ID";
public static final String COL_2 = "Food";
public static final String COL_3 = "Description";
public static final String COL_4 = "Protein";
public static final String COL_5 = "Fat";
public static final String COL_6 = "Carbohydrate";
public static final String COL_7 = "Energy";
public static final String COL_8 = "Starch";
public static final String COL_9 = "Sugar";
public static final String COL_10 = "Cholesterol";
SQLiteDatabase sqLiteDatabase; //<<<< Added
public FoodDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
sqLiteDatabase = this.getWritableDatabase(); //<<<< Amended
}
//#Override
public void onNotRecommendedCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table " + TABLE_NAME +
" (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"FOOD TEXT," +
"DESCRIPTION TEXT," +
"PROTEIN BLOB," +
"FAT BLOB," +
"CARBOHYDRATE BLOB," +
"ENERGY BLOB," +
"STARCH BLOB," +
"SUGAR BLOB," +
"CHOLESTEROL BLOB)");
}
#Override
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
"(" +
COL_1 + " INTEGER PRIMARY KEY, " +
COL_2 + " TEXT, " +
COL_3 + " TEXT, " +
COL_4 + " BLOB, " +
COL_5 + " BLOB, " +
COL_6 + " BLOB, " +
COL_7 + " BLOB, " +
COL_8 + " BLOB, " +
COL_9 + " BLOB, " +
COL_10 + " BLOB " +
")";
db.execSQL(crtsql);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(sqLiteDatabase);
}
public void insertFood(
String food,
String description,
byte[] protien,
byte[] fat,
byte[] carbohydrate,
byte[] energy,
byte[] starch,
byte[] sugar,
byte[] cholesterol) {
ContentValues cv = new ContentValues();
cv.put(COL_2,food);
cv.put(COL_3,description);
cv.put(COL_4,protien);
cv.put(COL_5,fat);
cv.put(COL_6,carbohydrate);
cv.put(COL_7,energy);
cv.put(COL_8,starch);
cv.put(COL_9,sugar);
cv.put(COL_10,cholesterol);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_NAME,null,cv);
}
public Cursor getFoodsWithProvidedFood(String provided_food) {
return this.getWritableDatabase().query(
TABLE_NAME,
null,
COL_2 + " LIKE '%" + provided_food + "%' ",
null,
null,
null,
null
);
}
}
Notes
It will likely cause fewer issue if you have just one place where you define table and column names.
hence the changed onCreate
although column names in SQLite are case-insensitive the Cursor's getColumnIndex method (as used in the main activity) is case-sensitive (IMO a bug).
AUTOINCREMENT doesn't do what it implies, rather INTEGER PRIMARY KEY itself makes a column one that increments adding AUTOINCREMENT is a special case that ensures that the id is greater at the expense of overheads. Hence AUTOINCREMENT has been removed.
Two additional methods have been added
insertFood to insert(add) data to the dataset table.
getFoodsWithProvidedFood as described above.
The Activity - MainActivity.java
public class MainActivity extends AppCompatActivity {
FoodDatabaseHelper foodDBHlpr;
Cursor mCsr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foodDBHlpr = new FoodDatabaseHelper(this);
byte[] dummy = new byte[]{0,1,2,3,4,5,6,7,8,9};
// Add some data oif none exists
if (DatabaseUtils.queryNumEntries(foodDBHlpr.getWritableDatabase(),FoodDatabaseHelper.TABLE_NAME) < 1) {
foodDBHlpr.insertFood("Fish and Chips", "The English Seaside meal",
dummy, dummy, dummy, dummy, dummy, dummy, dummy);
foodDBHlpr.insertFood("Bangers and Mash", "Yummy!!",
dummy, dummy, dummy, dummy, dummy, dummy, dummy);
foodDBHlpr.insertFood("Mashed Potatoe", "Boring",
dummy, dummy, dummy, dummy, dummy, dummy, dummy);
}
// get a Cursor with the extracted foods
mCsr = foodDBHlpr.getFoodsWithProvidedFood("Mash");
// Loop Through the Cursor
while (mCsr.moveToNext()) {
Log.d("FOODFOUND","You found the food called - " + mCsr.getString(mCsr.getColumnIndex(FoodDatabaseHelper.COL_2)));
}
if (mCsr.getCount() < 1) {
Log.d("FOODFOUND","No foods found that match the search criteria.");
}
mCsr.close(); //<<<< Should always close Cursors when done with them
}
}
Result in the Log :-
03-15 21:48:21.170 1702-1702/foodsdb.so49307874_foodsdb D/FOODFOUND: You found the food called - Bangers and Mash
03-15 21:48:21.170 1702-1702/foodsdb.so49307874_foodsdb D/FOODFOUND: You found the food called - Mashed Potatoe

How to know what JTextField is coming into the DocumentListener

I am trying to use a DocumentListener on a number of JTextFields. I need to know which textField is coming into the DocumentEvent so I can do some specific processes. Below is how I have my DocumentListener coded and one of the JTextFields set up [thanks to this example: How to get JTextField name in which is Document placed? ](my textField is declared at a higher scope).
How do I fix this?
final DocumentListener documentListener = new DocumentListener() {
#Override
public void changedUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
#Override
public void insertUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
#Override
public void removeUpdate(DocumentEvent documentEvent) {
printIt(documentEvent);
}
private void printIt(DocumentEvent documentEvent) {
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
final JTextField textField = (JTextField) documentEvent.getDocument().getProperty("parent");
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + textField + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + textField + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + textField + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
};
My JTextField is coded like the following...
textFieldFencing_LC1 = new JTextField();
textFieldFencing_LC1.setHorizontalAlignment(SwingConstants.CENTER);
textFieldFencing_LC1.setFont(new Font("Tahoma", Font.PLAIN, 9));
textFieldFencing_LC1.setColumns(10);
textFieldFencing_LC1.setBounds(234, 535, 85, 14);
panelLC.add(textFieldFencing_LC1);
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent",textFieldFencing_LC1);
The output I want should look like this
Type : (parent: textFieldFencing_LC1) InsertLength: 1
Type : (parent: textFieldFencing_LC1) InsertLength: 1
The output I am getting looks like this ...
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource#384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 2
Type : (parent: javax.swing.JTextField[,234,535,85x14,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource#384cdfdd,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=10,columnWidth=0,command=,horizontalAlignment=CENTER]) InsertLength: 3
After reading the documentation on this Class, I realized the putProperty(Object,Object) method would allow me to put a String into it. So, now my listener on my JTextField looks like this...
textFieldFencing_LC1.getDocument().addDocumentListener(documentListener);
textFieldFencing_LC1.getDocument().putProperty("parent","LC1"); // String, String
Note, the second parameter in the putProperty() is a String with meaning for me, so I can check for LC#. While the update in the DocumentListener looks like this...
final DocumentEvent.EventType type = documentEvent.getType();
String typeString = null;
// Cast documentEvent to String
txtField.setText( (String) documentEvent.getDocument().getProperty("parent") );
if (type.equals(DocumentEvent.EventType.CHANGE)) {
typeString = "(parent: " + txtField.getText() + ") Change";
} else if (type.equals(DocumentEvent.EventType.INSERT)) {
typeString = "(parent: " + txtField.getText() + ") Insert";
} else if (type.equals(DocumentEvent.EventType.REMOVE)) {
typeString = "(parent: " + txtField.getText() + ") Remove";
}
System.out.print("Type : " + typeString);
final Document source = documentEvent.getDocument();
final int length = source.getLength();
System.out.println("Length: " + length);
}
Output now looks like...
Type : (parent: LC1) RemoveLength: 0
Type : (parent: LC1) InsertLength: 1
Type : (parent: LC1) InsertLength: 2
Type : (parent: LC1) InsertLength: 3
Bottom line: a referenced Class and two lines of code placed on the JTextFields I need to listen to so an automatic update occurs, is a lot better than adding a CaretListener on all of those fields.

Accessing records in Android using rawQuery and then displaying

I am working on several rawQueries to use to parse data from a table in Android. The below code works fine and returns the lowest rowid in the table.
public void firstRecord(View v){
Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
c.moveToFirst();
szList.add(c.getString(0));
Toast.makeText(getApplicationContext(), "Sucessful Event. szRowid is: " +szList +".", Toast.LENGTH_LONG).show();
}
I have two questions, and they are both extremely basic: 1) what is the best way to expand the above code to create language to capture the contents of other columns in this table at that specific rowid, (rowid, sampler, species, place), and display this in my application? Something like this perhaps:
((EditText)findViewById(R.id.edSpecies)).setText("");
with the proper reference replacing "" in .setText()?
String TABLE_SURVEY = "surveyDB";
String COL_ROW_ID = "rowid";
String COL_SAMPLER = "sampler";
String COL_SPECIES = "species";
String COL_PLACE = "place";
public ArrayList<SurveyRecord> getSurveyRecords()
{
ArrayList<SurveyRecord> records = new ArrayList<SurveyRecord>();
String query = "SELECT * FROM " + TABLE_SURVEY;
query += " WHERE " + COL_ROW_ID = " SELECT MIN ("
query += COL_ROW_ID + ") FROM " + TABLE_SURVEY;
Cursor c = db.rawQuery(query,null);
if(Cursor.moveToFirst())
{
do{
String sampler = c.getString(cursor.getColumnIndex(COL_SAMPLER));
String species= c.getString(cursor.getColumnIndex(COL_SPECIES));
String place = c.getString(cursor.getColumnIndex(COL_PLACE));
String rowId = c.getString(cursor.getColumnIndex(COL_ROW_ID));
records.add(new (rowId,species,place,sampler));
}while(c.moveToNext())
}
c.close();
}
public class SurveyRecord{
String mRowId;
String mSpecies;
String mPlace;
String mSampler;
public SurveyRecord(String rowId,String species,String place,String sampler)
{
this.mRowId = rowId;
this.mSpecies = species;
this.mPlace = place;
this.mSampler = sampler;
}
}
//Goes to the first record in the dataset
public void firstRecord(View v){
Cursor c = db.rawQuery("SELECT * FROM surveyDB WHERE rowid = (SELECT MIN(rowid) FROM surveyDB)",null);
c.moveToFirst();
((EditText)findViewById(R.id.edRowid))
.setText(c.getString(0));
((EditText)findViewById(R.id.edSpecies))
.setText(c.getString(1));
((EditText)findViewById(R.id.edArea))
.setText(c.getString(2));
((EditText)findViewById(R.id.edSampler))
.setText(c.getString(3));
}

Resources