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

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.

Related

Is there a way to create pages/activities or something similar based on a users input?

I want to ask a user how many times a week they go to the gym, and depending on their input i want to display "x" amount of activites one after the other.
Example: User inputs 4 days a week. following that, the next activity will be a page for day one, then they click a button, then day 2, then click a button, then day 3 and so on.
One way I thought of doing this is creating 7 activities for the 7 days of the week, but id like to find a better way.
Another way which I'm not sure if its possible, is to create a sort of recursive Activity.
Example: User inputs 4 days a week. following that, the next activity will be a page for day one, then they click a button which opens up the same activity but with all the data they put in previously saved in a DB, and the inputs for day 1 has been cleared, so it becomes day 2.
if any one has any knowledge on the above scenario if you have done something similar or know if android studio has a better way to do this, any input will be appreciated, still a beginner using android studio, Thanks in advance.
Can't you create just different views in Android Studio ? Creating 7 different views would be the easy way, but if you want to do it more elegantly you could create one "template view". So you create a file/view with some placeholders like "Activity" and "Day_X". Then for every page the user can get to, you open up the same view (page, mask) but fill it with different data.
Then you can just based on the user input code something like:
"Create me NUM_DAYS_USER_INPUT times the activity template mask", once with the data for activity one, once with the data for activity too and so on...
You just need to pass the correct data for the single views to the masks.

Saving value from 2nd activity to 1st without losing the updated data

This app has 2 activities. The main activity shows 4 images as image buttons. When user gets into the form page, user can edit the information of the images. However the info does not save when I edit another image and then return to the first image.
Any help to this would be very appreciated !
If you just want to retrieve an updated data from an activity than easiest solution is to create any class to save constants and update that constants while editing your data and when you want an updated data just call that constant variables from that class.

Load state of particular recyclerview item

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

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!

Android application design help, How to control which activity shows

Hello I require some advice on how to approach this problem,
bear with me as I am still new to android.
I have an activity that opens on application start and requires some information from the user, but next time the user opens the app, i want the application to open a different activity that will display the various information.
Kind of like facebooks app, where when you first run it, you have to login, and only then next time you run the app you are guided straight to the feed.
Any ideas how one could do this efficiently?
UPDATE: Ive stored the information via shared preferences and am now using a controller activity that decides which step to take.
So, Controller activity runs on start up, and decides whether to show a log in screen or whether to go straight to the information. But now im encountering a problem where i end up opening a blank activity (the controller) and then another ontop of that ( the decided activtiy). I dont want the blank activity to show, so its kinda of like a background process, any ideas?
Ideally you would have a main activity like a controller. Use a SharedPreference object to keep track of whether the user is logged in or not. So back in your main activity, read this value and if it is set go to your news feed activity else show a login screen activity. (as you do the check and redirection, you can show a progress dialog)
links for SharedPreferences
MobTuts
Android Developer

Categories