Re-load Android activity data
I am writing an Android app, part of which will be a survey involving multiple pages of checkbox question and answers. I have created an activity to displa开发者_JAVA技巧y the question and options (from the DB) and what I want to do now is when i press the "Next" button it should just reload the current activity with the next question set from the database.
(the activity starts with survey.getNextQuestion()
- so its just a case of refreshing the activity so it updates)
Im sure this is a simple thing to do -any ideas?
Thanks
Typically you would start a new instance of the activity for the new question. That way if the user hits back, it has the logical behavior of taking them back a page.
It is possible to update the same activity, but it is more complicated.
To open a new activity:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("nextQuestion", x);
startActivity(intent);
Then in your onCreate method, you can pull the next question from the database.
Im sure this is a simple thing to do -any ideas?
It is pretty straight forward, yes. Mayra's answer is perhaps the better one, however here is an approach that will achieve the functionality you specified.
You can use findViewByID(int)
to identify the View
objects in your layout that need to be updated, and assign the result to an attribute in your onCreate
to allow you to access it later.
E.g.
aView = (View) findViewById(R.id.aview);
Your survey.getNextQuestion()
can obviously be used to get the next question.
The question can then be placed into the UI by manipulating the View
s you obtained in onCreate
.
E.g.
aView.setText("your question/answer");
Depending on the number of answers you may need to programatically create/remove checkbox Views from your layout
E.g.
ViewGroup yourViewGroup = findViewById(R.id.yourviewgroup);
View yourView = new View();
//Configure yourView how you want
yourViewGroup.addView(yourView);
youViewGroup.removeView(yourView);
All of this functionality can be contained in a function that is called by the onCreate
method and when the next button is pressed.
Don't forget to store the result of the previous question before refreshing ;)
精彩评论