Load state of particular recyclerview item - java

so I have recyclerview populated with data from database. Each row contains two buttons one for play and second for stop time. So what I'm trying is to save state of buttons inside recyclerview. And I used
PreferenceManager.getDefaultSharedPreferences(context).edit().
putInt(Constants.numberOfbutton, getAdapterPosition()).apply();
to save particular position. I have inserted that line of code inside button click and I checked it's saving correct position. What I'm trying to achieve is, when user enters the app after closing if he lefts any of the buttons in play mode to restore that state and continue timer. But problem is when user enters the app all buttons are running. I'm using chronometer for presentation of the time. I think here is the problem in this method:
private void startTime(int position ) {
Data data = datas.get(position);
chronometer.setBase(SystemClock.elapsedRealtime() + Long.parseLong(data.getTime()));
chronometer.start();
}
UPDATE EXPLANATION:
So I can load from database for each item of recyclerview it's time, date, name etc. But I can't figure it out how to play chronometer of just particular item when user enters the app again. NOTE AGAIN: I'm performing correct saving getAdapterAtPosition and loading data from PreferenceManager. I've checked that. Even I tried putting the number of one of rows but same issue appears.
Any help very appreciated. Thanks.

I have found the answer. I save the boolean in database actually I used 1 or 0 than parse that to true or false acordingly is button play pressed or not. Thank you all for your comments and trying to help.

Try to use handler or alarm service instead of chronometer

Related

How to check if an app is being opened - Android Studio

I'm making a game and am saving data for the player using sharedPreferences. The way I have it set up right now, every time the main application (the first page) is loaded, the old data it held before is loaded. So, imagine a player has 100$, and they exit the app. Upon opening the app again, this data will be loaded and everything seems fine.
The problem, however, is that if a player's money is changed in ANOTHER activity, the way I have it set up right now is that any time a player navigates back to the main activity, the data is loaded. So if the player has $100 on the main activity, this info is saved every few seconds on the main activity. If the player spends 50$ on a second activity, when they return to the main activity, since the last saved data the main activity has is $100, it will load the $100.
This is a problem, and a way to fix it is to ONLY LOAD THE DATA WHEN THE APP IS OPENED. So like I don't want to load the data every time the player navigates to the main activity, but only when they open the app. I need a simple if() boolean statement to do this, but I'm not sure what the statement I need is.
Thanks!
Store a boolean in sharedPreferences and check if it exists or if it's false/true in the create of the activity, such as storing a value for "hasLoaded" with a value of either true or false. check this value in onCreate and then do your logic accordingly.
OR alternatively,
create a static variable in your mainActivity for hasLoaded, then in onCreate, do everything you want to do and then change it to true.
static boolean hasLoaded = false;
if(!hasLoaded){
//all your logic here
hasLoaded = true;
}
note:
storing it in sharedPrefs will ensure that your initialization will only happen ONCE for all usages of the app, while storing it in a static variable will make sure it only happens once PER RUN of the app

how to get data from one activty to another several times Android

i tried to make an basketball game with 2 activities.
one is the opening screen with a play button and when i press on play i build a pop up window that will give the user some inputs like player name , percentage form field and more some data. how can i do that after the user enter all the details at the pop up screen and press enter it will send the data to the arraylist in the MainActivity and its will pop up again (i need that the pop up will open 6 times - each time for each player. 6 players at all). can someone plz tell me because intent is not good here from the pop up. thanks
You will need to save the data somewhere preferably , you can use an SqLiteDatabase which saves the player information having playerId as a Primary field.
I think no need to save all data in the database or shared preferences!
You can use a static class to store data or use EventBus to interact with your classes.

Method for "add to list" - Android application?? what method is suitable?

So i'm planning to develop an android application. In one activity, if i were to click a button of add, then it will prompt a user input and store the data. After that, thare would be another "list of item" button, where users can see which item they added and the information they input earlier.
I was thinking of using if.. else statement to check whether the button is click by setting a variable of default status=0 and change to 1 when clicked.
But how am i going to make sure that the functions won get messed up? is there any better suggestion for this? Im a beginner who just started to explore android studio.
Okay, so it is very simple and logical, and here is my best explanation. It's a rather long explanation, but I made it as thorough as possible...If you need any more help, just let me know:
You don't need to do any of that status=0 stuff. Just have an onclick listener:
(androidfromhome.com)
And in the listener, you want to go to another screen with a text input. So, just have an intent in the onClick which takes user to another screen:
(www.devcfgc.com)
Okay, so now you got your second screen right? You're going to want a textview (Change this in the xml) in the middle of the screen, where the user can enter data:
String stuff = TextView.getText();
Note, you will need to initialize the textview:
(androidfromhome.com)
So, now you got the text that the user entered. Just save it in SharedPreferences, and that's it, you have it saved!
http://developer.android.com/reference/android/content/SharedPreferences.html
Read about SharedPreferences in the link above.
So now, you have it saved. If you are going to get a lot of data, you can also save it in a database, which is more advanced stuff, that I don't know how to do. Now, display the saved data on the screen (A new screen, again, using an intent) using the method:
setText();
on your textView object for the next activity. That's it!

Multiple MainActivities start on startActivityForResult() in Android

guys,
I've been working on a native Android application for some time and now I am at the end of it there is still a problem I need help with.
My project at github
There is the following problem - when user starts the application for the first time the app will ask him "When does your diet cycle starts?" with a datepicker popup. I am using SharedPreferences to store the result user has picked from the popup. I have separate DatePickerActivity from the MainActivity one that takes care of this datepicker that I start with startActivityForResult(). The DatePickerActivity passes the result to the MainActivity using an Intent.
When I debug the app I see finish() in DatePickerActivity is started twice and the MainActivity is started more than one this cause the datepicker popup to show once again.
Once the start date is set in the system there is no problem with these activities and application works fine.
Then comes the moment when user wants to reset the date - using the basket icon with text like "Изчисти" and the datepicker once again appear twice.
I hope I've been clear enough with my explanation and I am looking fowrard to hearings from you.
Best Regards,
Mihail
I managed to find a work around my problem. I guess I caused it because I need my main layout redraw after user picks a date from the picker and I doing
finish();
startActivity(getIntent());
Here should be the reason for multiple MainActivity instances in the app. Second thing I guest is that committing SharedPreferences to the OS is async and by the time I check for selected date it has not been written yet.
I solved the problem by saving picked date in static instance and when I redraw the main layout I already have the result.
Thank to those who lost some of their time trying to fix my problem!

Updating an Android SQLite Database on Field Change

I'm writing an android application and on one of my pages, I have a text field, a spinner, and a date picker that need to be saved/updated in a SQLite database. All three have default values on creation, and the database needs to update any time a field is changed (there's no 'Save' button or anything like that). I've looked into using an onFocusChangeListener() for this, but it seems like the application only changes focus when using text fields, so it wouldn't work with a spinner or datepicker. I've also tried splitting the components into fragments and overwriting the onPause() method with update functionalities thinking that the fragments paused when the user moved on to the next one but I think that it still only checks when the user leaves the screen. Any help/advice would be greatly appreciated. Thanks!
For Spinner you can try OnItemSelectedListener, for Datepicker, try OnDateChangedListener

Categories