SQLite and Activity updates - android

I have a neat, working SQLite database on Android that records species observations. I am then using JSON (successfully) to verify observations entered against a set of parameters on a remote database. On verification, a flag in the database is updated to say that this process has been done. But, when I view my main activity (Text, the values for observations with flags after verification don't update.
My activity order is:
Main activity (obs = 0 from database)
Obs Entry activity
Main activity (obs = 1 with flag A from db)
Data management activity, verify button
Verify done activity, main button
Main activity (obs = 1 with flag A from db)
If I exit, or leave it for a while, then come back in to Main activity, the database is being polled correctly and obs = 1 with flag B from db.
So I know my database is right and the SQL is correct too. Could it be that I'm declaring my buttons at the wrong point for the Activity to correctly resume()?
My code:
final Button verifySightingsButton = (Button) findViewById(R.id.uploadprocess);
if (verifies == 0) {
verifySightingsButton.setTextColor(getResources().getColor(R.color.red));
} else {
verifySightingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// do stuff
}
});
}
where 'verifies' is the number of flag A records. The button is being called with the onCreate() method. Would it make any difference if I instantiated it as a class variable?
Thanks in advance - bugging me senseless!
UPDATE: This is the code (actually using XML, not JSON which is used for other queries) that handles the verification and update of flag:
// Get details for an observation within loop
ArrayList d = (ArrayList) allVerifyRows.get(i);
// Build the URL to verify the data
String aURL = buildVerifyURL(d);
// Parse the XML
aValid = ParseVerificationXML.verifyXMLdata(aURL);
db.open();
Long bouCode = new Long((String) speciesList.get((String) d.get(3)));
boolean insert = db.updateNote(
((Long) d.get(0)).longValue(),
((Long) d.get(1)).longValue(),
// and some other variables being updated
db.close();
and the code to check the database for records that are and are not verified:
NotesDbAdapter db = new NotesDbAdapter(this);
db.open();
Cursor c = db.fetchAllNotes();
species = new ArrayList<String>();
while (c.moveToNext()) {
String str1 = c.getString(2); // Date
species.add(str1);
}
c.close();
Cursor v = db.performNullCountForVerification();
if (v.moveToFirst()) {
stillToVerify = v.getInt(0); // Non-Verified records
readyForUpload = stillToVerify;
}
v.close();
stillToVerify = species.size() - stillToVerify;
db.close();

You might use the onResume method of your main activity to update the shown values. The onResume method is called every time the activity appears on the screen in contrast to onCreate, which is only called when the activity gets created.

Related

Temporarily store SQLite Database rows before write back to it

I have a scenario, whereby my App-A needs to read the Data from Database of App-B.
1) Modify the App-B's DB
2) Remove some rows of Data & save it.
3) While App-A is getting killed, restore App-B's data.
Now my concern is what's best way to save App-B's DB data so that it can easily be restored ?
I recommend you getting data from App B first and storing it into an Array of ItemData.class you have created. (Do this using your code in app A.)
ItemData.class :
public class ItemData{
public int itemId;
public String itemText;
public boolean itemBoolean;
}
And you can add new Item to your array as below:
ArrayList<ItemData> items= new ArrayList<>();
ItemData item = new ItemData();
item.itemId = //you should assign id from app b database
item.itemText = //you should assign text from app b database
item.itemBoolean = //you should assign boolean from app b database
items.add(item);
Then you can access all the data from app be and modify them meanwhile they are available. This will be much faster and data will not get lost from your array.
Now with a for loop you can restore all data and save it on another database:
for (int i = 0; i < items.size(); i++) {
int restoredID = items.get(i).itemId;
String restoredString = items.get(i).itemText;
boolean restoredBoolean = items.get(i).itemBoolean;
}
I hope this helps.

Android: Can't update UI after modifying database

my project involves the following:
1) An EditText view (inputText) in which the user is supposed to type a name
2) A Button that, when pressed, creates a Person object whose name is in inputText and saves that object to the realm database. Then it refreshes the textLog to include the new Person's name.
3) A 'TextView' (textLog) that shows a list of all the names of the Person objects in the realm database.
My problem is that clicking on the button refreshes the text log before it saves the person object to the database. This means the new person's name doesn't show up until I click the button again to create a newer Person object. I want the UI to refresh after the object is saved to the database, so it is always up to date. The following code is from my MainActivity class. Earlier I had done Handler handler = new Handler(Looper.getMainLooper());.
// called when button is clicked
private void submit(View view)
{
final String input = inputText.getText()
.toString()
.trim();
// asynchronous transaction
realm.executeTransactionAsync(realm -> {
realm.copyToRealm(new Person(input));
handler.post(this::refresh);
});
}
private void refresh()
{
textLog.setText(realm.where(Person.class)
.findAll()
.stream()
.map(Person::getName)
.collect(Collectors.joining("\n")));
}
Add Realm.Transaction.OnSuccess() and Realm.Transaction.OnError() callbacks to your async transaction. When those methods are called, you know the transaction is complete and you can refresh your UI.
It looks like you have a race condition. You do realm.executeTransactionAsync and then immediately do handler.post(this::refresh); - there's no guarantee that they'll execute in the order you want them to.

How do I access the same database from different activities?

I am writing an app and I want to store the high scores. I have it working to show the high score on the end activity. However, I want to have a highscores activity to show all the highscores. I am doing this by, in the highscores activity, calling the end activity to return the high score, so the database doesn't change. After running a debug, I saw that it got to the databasehandler but got caught on getReadableDatabase(), saying that it was unable to invoke the method on a null object reference.
This is my highscores method(I didn't include the whole thing, and the ifs are because there are different difficulties of the game)
public class highscores extends Activity {
TextView mode, score;
Button right, back;
int modenum;
end get=new end();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_highscores);
mode =(TextView)findViewById(R.id.scorebar);
score =(TextView)findViewById(R.id.score);
right =(Button)findViewById(R.id.button14);
back =(Button)findViewById(R.id.highscores);
mode.setText("Easy");
score.setText(get.datatostring(1));
modenum =1;
}
public void right(View view){
if(modenum==1) {
modenum = 2;
mode.setText("Hard");
score.setText(get.datatostring(2));
}else if(modenum==2) {
modenum = 3;
mode.setText("X-Mode");
score.setText(get.datatostring(3));
}else {
modenum = 1;
mode.setText("Easy");
score.setText(get.datatostring(1));
}
}
This is in the end method
public String datatostring(int difficulty){
MyDBHandler db = new MyDBHandler(this, null,null,1);
return db.datatostring(difficulty);
}
And this is in the databasehandler
public String datatostring(int difficulty){
SQLiteDatabase db = getReadableDatabase();
String dbString = "";
String c;
if(difficulty==1)
c = COLUMN_SCORE;
else if(difficulty==2)
c = COLUMN_HARD;
else
c = COLUMN_X;
String query = "SELECT "+c+" FROM "+TABLE_SCORES;
Cursor cursor = db.rawQuery(query,null);
cursor.moveToFirst();
if(!cursor.isAfterLast()){
int index = cursor.getColumnIndex(c);
String value = cursor.getString(index);
if(value!=null){
dbString += cursor.getString(cursor.getColumnIndex(c));
}
}
db.close();
cursor.close();
return dbString;
}
I suggest to use a SQLiteOpenHelper to handle this.
You should see this example:
Android Sqlite DB
Hope that helps, i personally works in this way.
The other option is to use som ORM, i recommend to use GreenDao, its good, and lets handle easy way this kind of actions.
Regards.
Follow this tutorial to create a concurrent and scalable database
Remember :
You should always make database queries through one instance (Singleton) of your database adapter else you will face a lot of issues when accessing your database concurrently from different classes.
Use SQLiteOpenHelper class for accessing your database. As it gives you many useful functions eg. upgrading user's database when you publish app updates with schema changes.
The short answer is yes.
But as you see you have no clear concept of the connection.
Bd in the android is SQLite (recommendation) Always be on the same route, you need to create a class that allows you to manage and connect to the database. The class will be SQLiteOpenHelper
Check THIS
You can connect one or more times to the database, since activity A, B or activity you want.
The important thing is to define the handler to connect, close, make request to the database.

insert in database with multiple activities

I have a problem, I created database for all activities, and in each activity I should insert information to database, so for the first activity , the insert is done , for the second activity I update the row with new insertion to complete all information and so on, my problem is that I don't know how to refer to the last row, I mean what should I do that make the update for the second activity occurs to the last row that has been insert in the first activity, do you have any suggestions ???
Well you can just use the primary key. When you insert something into the database you get as a return value the primary key. You can add this to the Intent that opens the other Activity and that way refer back to the row you previously inserted.
Edit:
I don't know if you are working with and SQLiteDatabase Object or with a ContentProvider, but in any case the code would be pretty much the same. In this example I will work directly with an SQLiteDatabase Object, even though using ContentProviders is in most cases the better alternative.
In your first Activity:
// When you perform an insert you get the id of the row which was just inserted.
long id = sqliteDatabase.insert("some_table", null, contentValues);
// The id will be -1 if an error occured
if(id >= 0) {
...
}
...
// When you start your second Activity you can add the id to the Intent
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
// The String is a tag with which you can later retrieve the id from the Intent.
// Note that you should never hardcode String like that, use constants in your app.
intent.putExtra("rowId", id);
In the onCreate Method of your second Activity you can retrieve the id:
#Override
protected void onCreate (Bundle savedInstanceState) {
// Check if the Activity has been created for the first time
if(savedInstanceState == null) {
// Retrieve the Intent with which the Activity was started
Intent intent = getIntent();
long id = intent.getLongExtra ("rowId", -1);
// If id is valid proceed
if(id >= 0) {
Cursor cursor = sqliteDatabase.query("some_table", columns, "_id = ?",
new String[] { String.valueOf(id) }, null, null, null, null);
// Check if Cursor is valid and if yes read your data.
if(cursor != null) {
...
}
}
}
}
The best way to do this would be to add a column to your database which will hold the time the row was inserted. Then when you need the latest row, query for the one with the most current time. An example SQL string would be:
SELECT * FROM my_table WHERE 1 ORDER BY time_stamp LIMIT 1

updating a row of data sqlite android

I am trying to update my database. It is not really updating. I think it is due to my update command
my main activity is a listview activity. I have a button on the action bar that basically starts a new activity with two editboxes. So once I click that button, I create a new database item. It is originally set to two empty strings. On my second activity, I have a save button that is supposed to save my changes into the textboxes
so after my main activity takes me to my second activity, the code is as follows
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
// if new then create new db, else retrieve old database, right now
// assume everything is new
db = new DBHandler(this);
nowAdd = new UENote("", "");
db.addNote(nowAdd);
}
i update my nowAdd contact strings through this function
public void updateColumnData() {
// gets the strings from edittext
EditText textTitle = (EditText) findViewById(R.id.editText1);
EditText textSubject = (EditText) findViewById(R.id.editText2);
// converts the string
String titleString = textTitle.getText().toString();
String subjectString = textSubject.getText().toString();
nowAdd.setSubject(subjectString);
nowAdd.setTitle(titleString);
db.updateNote(nowAdd);
}
the update note function is as follows
public int updateNote(UENote note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, note.getTitle());
values.put(KEY_SUBJECT, note.getSubject());
// updating row
return db.update(TABLE1, values, KEY_ID + " = ? ",
new String[] { String.valueOf(note.getID()) });
}
Make sure that the id you are passing in the db.update() is really the same one that you want to update. From the code snippet, it's not obvious how you are getting the value of note.getID(). Are you making query for empty record to get the id?
Assuming that KEY_ID is an auto incremented primary key, I would then debug getID() to check if it returns the same id of the record you wanted to update.

Categories

Resources