I have searched and found: How to check if a value already exists in the database in Android. The problem I am having is that the toast never shows and the data is still entered in the database when the values that are being entered match what is already in the database. I have seen post about using UNIQUE to make sure data is not duplicated, but it will not work for what I am needing to do. I have tried if (entries.contains(checkDuplicates) == false) and if (!entries.equals(checkDuplicates)), and got the same results with those as well. I know it is something simple that I am missing or not understanding. Will someone help me out and point mes in the right direction please?
private void saveState() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Inserting entries to DB
// Reading all contacts
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries) {
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
}
String checkDuplicates = date + event + empID;
if (!entries.contains(checkDuplicates)) {
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
} else {
Toast.makeText(this,"Employee has already been scanned for this event.", Toast.LENGTH_SHORT).show();
}
}
Edit: I got it to work. Here is what I did:
private void saveState() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
eventDate = sdf.format(calendar.getTime());
String date = eventDate;
String event = eventTitle.getText().toString();
String empID = fiveAndTwo.getText().toString();
/**
* CRUD Operations
* */
// Reading all entries
Log.d("Reading: ", "Reading all entries..");
List<EventEntries> entries = db.getAllEntries();
for (EventEntries ee : entries) {
String log = "Id: " + ee.getID() + " Date: " + ee.getDate() + " Event Name: " + ee.getEvent() + " Emp ID: " + ee.getempID();
// Writing entries to log
Log.d("Entry: ", log);
}
EventEntries checkDuplicates = new EventEntries(date, event, empID);
if (!entries.contains(checkDuplicates)) {
Toast.makeText(this,"Successfully entered.", Toast.LENGTH_SHORT).show();
// Inserting entries to DB
Log.d("Insert: ", "Inserting ..");
db.addEntry(new EventEntries(date, event, empID));
String logEntry = "Date: " + date + " Event Name: " + event + " Emp ID: " + empID;
Log.d("Entered to DB:", logEntry);
} else {
empAlert();
}
}
And here is what I added to the EventEntries class:
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((_date == null) ? 0 : _date.hashCode());
result = prime * result + ((_empID == null) ? 0 : _empID.hashCode());
result = prime * result + ((_event == null) ? 0 : _event.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EventEntries other = (EventEntries) obj;
if (_date == null) {
if (other._date != null)
return false;
} else if (!_date.equals(other._date))
return false;
if (_empID == null) {
if (other._empID != null)
return false;
} else if (!_empID.equals(other._empID))
return false;
if (_event == null) {
if (other._event != null)
return false;
} else if (!_event.equals(other._event))
return false;
return true;
}
entries.contains(checkDuplicates) always returns false because entries is a list of EventEntries object and checkDuplicates is a String. You should call entries.contains using an EventEntries object as the parameter.
One more thing:
// assuming entries is not empty
EventEntries first = entries.get(0);
entries.contains(first); // --> this is TRUE
// create a dupe of "first"
EventEntries firstDupe = new EventEntries(.....);
entries.contains(firstDupe); // --> in *general* this is FALSE
In general, the second case is false. It can be true, if in the EventEntries class you override the equals method so that first.equals(firstDupe) would return true. Only then, the second example will return true.
Here's an example implementation:
public boolean equals(Object obj) {
if (obj instanceof EventEntries) {
EventEntries other = (EventEntries)obj;
if (date.equals(other.date) && event.equals(other.event)
&& empID == other.empID) {
return true;
}
}
return false;
}
(Adjust as necessary.)
Replace:
String checkDuplicates = date + event + empID;
with:
EventEntries checkDuplicates = new EventEntries(date, event, empID);
Related
I have a list of Buttons in RecycleView. I have implemented OnClickListener on every Button in list:
#Override
public void onClick(View v) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
// We can access the data within the views
Toast.makeText(context, position + " " + washLocations.get(position), Toast.LENGTH_SHORT).show();
Log.d("myTag", "This is my message");
Integer isFav = washLocations.get(position).getFav();
WashLocation w = washLocations.get(position);
Integer id = w.getId();
List<WashLocation> washLocation = dataBaseHelper.getWashLocation();
for(WashLocation wash : washLocation){
if(wash.getId().equals(w.getId())){
dataBaseHelper.updateWashLocation(w);
return;
}
}
w.setFav(isFav == 1 ? 0 : 1);
dataBaseHelper.addWashLocationToFav(w);
favorite.setText(w.getFav() == 1 ? "Usuń z ulubionych" : "Dodaj do ulubionych");
}
}
The case is whenever I want to compare wash Id from Wash object which has been already clicked I always get that id is null. But when I print every object from DB every wash object has their own unique ID.
While creating table I add autoincrement:
String CREATE_TABLE = "CREATE TABLE " + TABLE_WASH_LOCATION + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + COL_WASH_NAME + " TEXT,"
+ COL_WASH_CITY + " TEXT," + COL_WASH_STREET + " TEXT," + COL_LAT + " TEXT," + COL_LNG + " TEXT," + COL_FAV + " INTEGER" + ")";
db.execSQL(CREATE_TABLE);
Example:
UPDATE:
The list of washLocation in adapter is popualted:
washLocations = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mAdapter = new MyAdapter(washLocations, this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAsyncTask myAsyncTask = new MyAsyncTask();
try {
JSONObject jsonObject = myAsyncTask.execute(latitude, longitude, radius).get();
String lat;
String lng;
String name = "";
String city = "";
if (jsonObject.has("results")) {
JSONArray jsonArray = jsonObject.getJSONArray("results");
for (int n = 0; n < jsonArray.length(); n++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(n);
lat = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lat").toString();
lng = jsonObject1.getJSONObject("geometry").getJSONObject("location").get("lng").toString();
JSONObject oName = jsonArray.getJSONObject(n);
if (oName.has("name")) {
name = oName.getString("name");
}
JSONObject oVicinity = jsonArray.getJSONObject(n);
if (oVicinity.has("vicinity")) {
city = oVicinity.getString("vicinity");
}
WashLocation w = new WashLocation(name, lat, lng, 0, getCity(city), null);
washLocations.add(w);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
I'm not really sure that I understand what you are trying to do, i cleaned up your code a bit and added comments starting with a Q, can you answer those questions ?
#Override
public void onClick(View v) {
int position = getAdapterPosition(); // gets item position
if (position != RecyclerView.NO_POSITION) { // Check if an item was deleted, but the user clicked it before the UI removed it
WashLocation w = washLocations.get(position);
// We can access the data within the views
// Q: Does this show the right WashLocation ?
Toast.makeText(context, position + " " + w, Toast.LENGTH_SHORT).show();
Log.d("myTag", "This is my message");
Integer isFav = w.getFav();
Integer id = w.getId();
// Q: Will this list be different from washLocations ?
List<WashLocation> washLocation = dataBaseHelper.getWashLocation();
for(WashLocation wash : washLocation){
if(wash.getId().equals(id)){
// Q: Why can't you execute this line immediately ?
// Q: What does this line actually do ?
dataBaseHelper.updateWashLocation(w);
return;
}
}
w.setFav(isFav == 1 ? 0 : 1);
dataBaseHelper.addWashLocationToFav(w);
favorite.setText(w.getFav() == 1 ? "Usuń z ulubionych" : "Dodaj do ulubionych");
}
}
I am working on a code snippet where i am storing my json encoded data into a txt file,and using following method to separate all parts and adding them into database.
public boolean addAnswersFromJSONArray() {
boolean flag = false;
Answer answer = new Answer();
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "user_live.txt");
FileReader fr;
JsonReader reader;
try {
fr = new FileReader(file);
reader = new JsonReader(fr);
reader.beginArray();
reader.setLenient(true);
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("product_name")) {
answer.setProductName(reader.nextString());
} else if (name.equals("subject")) {
answer.setSubject(reader.nextString());
} else if (name.equals("month")) {
answer.setMonth(reader.nextString());
} else if (name.equals("year")) {
answer.setYear(reader.nextString());
} else if (name.equals("question")) {
answer.setQuestion(reader.nextString());
} else if (name.equals("answer")) {
answer.setAnswer(reader.nextString());
} else if (name.equals("question_no")) {
answer.setQuestion_no(reader.nextString());
} else if (name.equals("marks")) {
answer.setMarks(reader.nextString());
} else {
reader.skipValue();
}
}
answer.save(db);
reader.endObject();
flag = true;
}
reader.endArray();
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
file.delete();
db.close();
}
return flag;
}
and then i am retrieving each fields departments,subjects,month and year,questions,answers,question_no, but while retrieving marks i am getting only unique entries that is 10 and 5....Ideally the size of one set is 18 so i m getting ArrayIndexoutOfBounds Exception.
//database calling part
marks = db.getMarksList(department, subject, month_year);
database method is,
public String[] getMarksList(String department, String subject,
String month_year) {
String month = month_year.split("-")[0];
String year = month_year.split("-")[1];
String whereClause = DEPARTMENT + " = '" + department + "'" + " AND "
+ SUBJECT + " = '" + subject + "' AND " + MONTH + " = '"
+ month + "' AND " + YEAR + " = '" + year + "'";
System.out.println("questions: " + whereClause);
Cursor cursor = db.query(true, "ANSWERS", new String[] { "MARKS" },
whereClause, null, null, null, "DEPARTMENT", null);
String list[] = new String[cursor.getCount()];
int i = 0;
if (cursor != null && cursor.getCount() > 0) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
.moveToNext()) {
list[i] = new String(cursor.getString(0));
i++;
}
}
return list;
}
Can anyone help me to resolve this issue?? Why getting only unique value,I have checked my json result also each row contains marks.
i got the solution for this,
Changed database query and method as following,
public List<Answer> getMarksList(String department, String subject,
String month_year) {
List<Answer> list = new ArrayList<Answer>();
String month = month_year.split("-")[0];
String year = month_year.split("-")[1];
try {
String sql1 = "select all marks from " + TABLE_NAME
+ " where department = '" + department
+ "' AND subject = '" + subject + "' AND month = '" + month
+ "' AND year = '" + year + "';";
SQLiteDatabase db1 = this.getWritableDatabase();
Cursor cursor = db1.rawQuery(sql1, null);
if (cursor.moveToFirst()) {
do {
Answer a = new Answer();
a.setMarks(cursor.getString(0));
list.add(a);
} while (cursor.moveToNext());
}
} catch (Exception e) {
}
return list;
}
using "all" in query is retrieving all records.
I am using this code to show missed calls in my application while I am using Call class to define an object each time then store it in test how Can I print all objects in test list in my application?
List<Call> callList = new ArrayList<Call>();
while (c.moveToNext()) {
Call call = new Call();
call.setNumber(c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)));
/* if (c.getInt(c.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.OUTGOING_TYPE) {
call.setOut(true);
} else {
call.setOut(false);
}*/
call.setName(c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)));
call.setTime(c.getLong(c.getColumnIndex(CallLog.Calls.DATE)));
call.setDuration(c.getLong(c.getColumnIndex(CallLog.Calls.DURATION)));
if ( c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME))!= null)
{
continue;
}
callList.add(call);
}
c.close();
}
you can override toString() method on Call class like:
#Override
public String toString() {
return Name+" "+Time+" "+Duration;
}
and print your list in for statement like:
for (int i = 0 ; i < callList.size() ; i++)
Log.d("value is" , callList.get(i).toString());
You can just iterate through the list and print the elements :
List<Call> callList = new ArrayList<Call>();
for(Call call : callList){
System.out.print("Name - " +call.getName() +
"Number - "+call.getNumber() +
"Time - "+call.getTime() +
"Duration - "call.getDuration());
}
you must override toString in your Call class like this
#Override
public String toString() {
return "Call{" +
"Name=" + Name+
", Time='" + Time+ '\'' +
", Duration='" + Duration+ '\'' +
'}';
}
and then use this
System.out.println("callList: " +Arrays.toString(callList.toArray()));
I've got a long running task that I run onResume on my activity. The task involves querying a database then decrypting some data, then manually sorting it then updating the sort order in the database using a transaction.
This works fine when I run it from the Activities main UI thread, but when I execute the same task from within an AsyncTask I always get these errors:
I/SqliteDatabaseCpp(5166): sqlite returned: error code = 1, msg = no such table: Household, db=/mnt/sdcard/myDatabase.db
no such table: while compiling: no such table: Household: , while compiling: SELECT DISTINCT street FROM Household WHERE street IS NOT NULL AND LENGTH(LTRIM(RTRIM(street)))>0
I know that the database exists and that SQL statement is fine because it runs fine outside the AsyncTask. Is there something about access my database from within an AsyncTask that causes problems?
I'm getting errors on the "SELECT DISTINCT" raw query below.
private boolean update_street_sort_order() {
boolean returnValue = false;
DBUtilities objDbUtil = null;
Cursor cCases = null;
final String SORT_ATTRIBUTE = "street_sort_order";
final int STREET_INDEX = 0;
final int ENCRYPTED_STREET = 0;
final int DECRYPTED_STREET = 1;
try {
objDbUtil = DBUtilities.getInstance(this);
if (objDbUtil != null) { // Get list of cases
ArrayList<String[]> alStreet = new ArrayList<String[]>();
SQLiteDatabase sqlitedatabase = objDbUtil.getDatabase();
if (sqlitedatabase != null && sqlitedatabase.isOpen()) {
cCases = sqlitedatabase.rawQuery("SELECT DISTINCT street "
+ "FROM Household " + "WHERE street IS NOT NULL "
+ "AND LENGTH(LTRIM(RTRIM(street)))>0", null);
String _password = this.context.getPassword();
if (cCases != null && cCases.moveToFirst()) {
do { // Create list of en/decrypted streets
alStreet.add(new String[] {
cCases.getString(STREET_INDEX),
Crypto.decrypt(_password,
cCases.getString(STREET_INDEX)) });
} while (cCases.moveToNext());
}
if (cCases != null) {
cCases.close();
cCases = null;
}
int alStreet_length = alStreet.size();
if (alStreet_length > 0) {
Collections.sort(alStreet, new Comparator<String[]>() {
#Override
public int compare(String[] lhs, String[] rhs) {
return lhs[DECRYPTED_STREET]
.compareToIgnoreCase(rhs[DECRYPTED_STREET]);
}
}); // sort decrypted street using custom comparator
StringBuilder sql_transaction = new StringBuilder(
"BEGIN TRANSACTION;" + "UPDATE Household SET "
+ SORT_ATTRIBUTE + "=NULL;");
for (int i = 0; i < alStreet_length; i++) {
sql_transaction.append(String.format(
"UPDATE Household " + "SET "
+ SORT_ATTRIBUTE + "=%1$d "
+ "WHERE street=\"%2$s\";", i,
alStreet.get(i)[ENCRYPTED_STREET]));
}
sql_transaction.append("COMMIT;");
// execute transaction
sqlitedatabase.execSQL(sql_transaction.toString());
}
returnValue = true;
}
}
} catch (Exception e) {
Log.e(Utilities.getFullMethodName(e), e.getMessage());
} finally {
if (objDbUtil != null) { // release resources
objDbUtil.close();
objDbUtil = null;
}
}
return returnValue;
Sorry if I repeat my question but I have still had no clues of what to do and how to deal with the question.
My app is a dictionary. I assume that users will need to add words that they want to memorise to a Favourite list. Thus, I created a Favorite button that works on two phases:
short-click to save the currently-view word into the Favourite list;
and long-click to view the Favourite list so that users can click on any words to look them up again.
I go for using a SQlite database to store the favourite words but I wonder how I can do this task. Specifically, my questions are:
Should I use the current dictionary SQLite database or create a new SQLite database to favorite words?
In each case, what codes do I have to write to cope with the mentioned task?
Could anyone there kindly help?
Here is the dictionary code:
package mydict.app;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class DictionaryEngine {
static final private String SQL_TAG = "[MyAppName - DictionaryEngine]";
private SQLiteDatabase mDB = null;
private String mDBName;
private String mDBPath;
//private String mDBExtension;
public ArrayList<String> lstCurrentWord = null;
public ArrayList<String> lstCurrentContent = null;
//public ArrayAdapter<String> adapter = null;
public DictionaryEngine()
{
lstCurrentContent = new ArrayList<String>();
lstCurrentWord = new ArrayList<String>();
}
public DictionaryEngine(String basePath, String dbName, String dbExtension)
{
//mDBExtension = getResources().getString(R.string.dbExtension);
//mDBExtension = dbExtension;
lstCurrentContent = new ArrayList<String>();
lstCurrentWord = new ArrayList<String>();
this.setDatabaseFile(basePath, dbName, dbExtension);
}
public boolean setDatabaseFile(String basePath, String dbName, String dbExtension)
{
if (mDB != null)
{
if (mDB.isOpen() == true) // Database is already opened
{
if (basePath.equals(mDBPath) && dbName.equals(mDBName)) // the opened database has the same name and path -> do nothing
{
Log.i(SQL_TAG, "Database is already opened!");
return true;
}
else
{
mDB.close();
}
}
}
String fullDbPath="";
try
{
fullDbPath = basePath + dbName + "/" + dbName + dbExtension;
mDB = SQLiteDatabase.openDatabase(fullDbPath, null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
catch (SQLiteException ex)
{
ex.printStackTrace();
Log.i(SQL_TAG, "There is no valid dictionary database " + dbName +" at path " + basePath);
return false;
}
if (mDB == null)
{
return false;
}
this.mDBName = dbName;
this.mDBPath = basePath;
Log.i(SQL_TAG,"Database " + dbName + " is opened!");
return true;
}
public void getWordList(String word)
{
String query;
// encode input
String wordEncode = Utility.encodeContent(word);
if (word.equals("") || word == null)
{
query = "SELECT id,word FROM " + mDBName + " LIMIT 0,15" ;
}
else
{
query = "SELECT id,word FROM " + mDBName + " WHERE word >= '"+wordEncode+"' LIMIT 0,15";
}
//Log.i(SQL_TAG, "query = " + query);
Cursor result = mDB.rawQuery(query,null);
int indexWordColumn = result.getColumnIndex("Word");
int indexContentColumn = result.getColumnIndex("Content");
if (result != null)
{
int countRow=result.getCount();
Log.i(SQL_TAG, "countRow = " + countRow);
lstCurrentWord.clear();
lstCurrentContent.clear();
if (countRow >= 1)
{
result.moveToFirst();
String strWord = Utility.decodeContent(result.getString(indexWordColumn));
String strContent = Utility.decodeContent(result.getString(indexContentColumn));
lstCurrentWord.add(0,strWord);
lstCurrentContent.add(0,strContent);
int i = 0;
while (result.moveToNext())
{
strWord = Utility.decodeContent(result.getString(indexWordColumn));
strContent = Utility.decodeContent(result.getString(indexContentColumn));
lstCurrentWord.add(i,strWord);
lstCurrentContent.add(i,strContent);
i++;
}
}
result.close();
}
}
public Cursor getCursorWordList(String word)
{
String query;
// encode input
String wordEncode = Utility.encodeContent(word);
if (word.equals("") || word == null)
{
query = "SELECT id,word FROM " + mDBName + " LIMIT 0,15" ;
}
else
{
query = "SELECT id,content,word FROM " + mDBName + " WHERE word >= '"+wordEncode+"' LIMIT 0,15";
}
//Log.i(SQL_TAG, "query = " + query);
Cursor result = mDB.rawQuery(query,null);
return result;
}
public Cursor getCursorContentFromId(int wordId)
{
String query;
// encode input
if (wordId <= 0)
{
return null;
}
else
{
query = "SELECT id,content,word FROM " + mDBName + " WHERE Id = " + wordId ;
}
//Log.i(SQL_TAG, "query = " + query);
Cursor result = mDB.rawQuery(query,null);
return result;
}
public Cursor getCursorContentFromWord(String word)
{
String query;
// encode input
if (word == null || word.equals(""))
{
return null;
}
else
{
query = "SELECT id,content,word FROM " + mDBName + " WHERE word = '" + word + "' LIMIT 0,1";
}
//Log.i(SQL_TAG, "query = " + query);
Cursor result = mDB.rawQuery(query,null);
return result;
}
public void closeDatabase()
{
mDB.close();
}
public boolean isOpen()
{
return mDB.isOpen();
}
public boolean isReadOnly()
{
return mDB.isReadOnly();
}
}
And here is the code below the Favourite button to save to and load the Favourite list:
btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add code here to save the favourite, e.g. in the db.
Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
toast.show();
}
});
btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Open the favourite Activity, which in turn will fetch the saved favourites, to show them.
Intent intent = new Intent(getApplicationContext(), FavViewFavourite.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return false;
}
});
}
You need to have two tables
words
favorites
Words(id, word, meaning,...)
Favorites(id, word_id)
In the Favorites table have a foreign key that points the word from the Words Table.
I have only addressed the way you need to structure the table.
*EDITED*
words(id, name, meaning, timestamp)
favortie(id, word_id)