how to create contents in an activity dynamically - android

I am developing an android application, in which user can take a test and get his score.
So, I have created a database with 50 questions and I want to choose a question number randomly using Random class and retrieve that question from database and show it in an Activity. And by clicking "Next question" button in the activity, I want the same activity to be loaded with different question. How can I write the code for this?

Why do you want to load the same Activity again?
I'd create an activity (fragment), with the main design, and a function (call it reDisplay()), which displays the question, the answers, etc. In the onCreate(), I'd call this function, and every time, the user clicked on the "Next question" text, I'd save them answers, and call the reDisplay() function, to show an other questin.

Related

keep data retained of listview when switching between activities

i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.
the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.
i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them
thanks in advance and here is my app code for main activity and second activity and picture for output
sorry i add code as images becausethe site keep saying the code need indentation thank you
main activity[main out put][2]second activity[second activity out put][4]
You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.
You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.
Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.

Android app - delete item from list following an action in another activity

I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo

Android: refreshing a listview from a 'grandchild' activity

I have a setup as follows
Activity 1 uses a ListView and ListAdapters to display information
from list of objects of type A (by retrieving from the database the first time its called).
Upon clicking an item in the ListView in Activity 1, the control goes
to Activity 2, which again uses a ListView and ListAdapters to display
information from list of objects of type B.
There is a '+' button in Activity 2, which when tapped switches the
control to Activity 3. Here I can create an object of type B and save it to the database.
Now I use the setResult() in Activity 3 and onActivityResult() in Activity 2 to update the list in Activity 2.
So far so good. I can see the item of type B that I just created in Activity 2.
Now if I press the back button and go back to Activity 1, and tap on
the same item of type A then when I go to the Activity 2, the item that I had just created
does not show. However when I close the app, and open it again, and
follow the same path, I can see that item. (As the list was reloaded from the database)
So how do I update the list in Activity 1?
I hope I explained my question properly (apologize if not!). I don't want to put all the code here, since there is no issue with the code, unless my approach is wrong.
Put your code which loads the list from database in Activity.onResume() method. This way it should execute every time your activity is restored from invisible state (take a look at the docs on activity lifecycle for more info).
Also, you might want to implement loading from database using Loader. It monitors the data source for updates, thus keeping data up to date.

How to save android acvitiy history and retrieve at later time?

How do I save previous activity and have that update into a listview so that when a user launches the app at a later time he can click on a menu option called history which will bring a listview of the last few activities. Let's say the listview holds 5 activies and after the 5 activies it deletes the oldest activity saved and saves the new activity in it's place.
For example if someone wants to view history of an android activity that he viewed two days ago, he can simply click on a button called history and view his past 5 entries which will be displayed in a listview.
Any ideas?
You can't do it by default Android Activities method.
Just save state in onResume/onCreate (as file in filesystem or SharedPreference) and read it from yours History Activity.
Sorry for bad english)) Ask if don't understand something.

How to maintain the state of an activity at the time of clicking back button and coming back to the same activity

I have a question related to navigation between different activities and onPause and onRestore might solve the problem but how that I don't know.
The problem is
I have following screens in my application
HomeActivity - This is the welcome screen
CategoryActivity - Here there are three categories e.g. Animal, Plants and Fruits.
QuestionsActivity - Here each questions are available according to selection of Category. In each cateogry there are 50 questions.The application has multiple choice type of questions.
so the flow of application is when a user comes and selects a category, I populate an arraylist of 50 questions from my xml for that category, then display those questions one by one on the QuestionActivity screen.
Here, every time a user clicks on the category, I shuffle the arraylist
and show the count. i.e you finished 1 question, you finished 2 question etcs.
So this is something about my application and now problem:
Suppose a user is in QuestionActivty and he answered 5 questions and then click on back button of mobile so he is redirected to the CategoryActivity screen. Now from here, if he again clicks on the same Category, he is redirected to the QuestionActivity screen but his questions starts from 1st questions :(. Ideally, it should start with 6th question as he already aswered 5 questions.
Can some one please help me to solve this problem? If I persist the count, then it won't work for me as every time I am shuffling the arraylist (so the answered questions might repeat.)
What the ideal solution should be is to let android manage the state of QuestionActivity at the time of click of back button and resume the same state at the time of again selecting the same Category
This must be a common problem so if you already solved it, please help me out.
Thanks a lot in advance,
Ishan
Why not use StartActivityForResult(). Send back an int x = 5 back to CategoryActivity and then when you resume QuestionActivity you can send x back with it in the Bundle extras. SO then QuestionActivity knows to start form the 6th (x+1) question.
If the questions are randomly ordered you will have problems, since the 6th question might be the same as the first five. So instead of sending just an int x back, send an Integer Array of the question numbers that have already been answered.
I'd suggest looking into storing this data in either a SQLite DB or in a SharedPreferences file. This way you can record where your user has left off and present the next questions based on values within one of these storage mechanisms.
http://developer.android.com/guide/topics/data/data-storage.html

Categories

Resources