Re-load Android activity data - java

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 display 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 Views 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 ;)

Related

Converting my current project to fragments. Lost with my project

I am absolutely lost with my project and I need someone to point me to the right direction because right now im just looking at my code and I cant understand where to start.
MY app is all about answering questions. A player has to answer if the question is right or wrong. If he answers the right amount of questions he unlocks a new level. So until today I just thought I will need to create a new activity on every level, create questions in that activity and let it run. But just today I realized that having 100 activities of different levels is just stupid.
So I need to make my app work with fragments. The only thing i need is to make it that on each different level my app would select specific questions from the question list and display them with their values.
Right now i create questions in level activity. For example: Level1Activity
//Creating questions. (Question, boolean, answer).
final Question first = new Question("Do i understand this code?", true, "Only Jesus knows");
final Question second = new Question("Why dont i understand this code?", false, "Im not Jesus");
final Question third = new Question("Why I am not Jesus?", true, "2fat.");
//Creating Lists for questions and boolean values.
final ArrayList<Question> questions = new ArrayList<Question>();
final ArrayList<Boolean> type = new ArrayList<Boolean>();
//Adding questions to the question list
questions.add(first);
questions.add(second);
questions.add(third);
// Adding boleans to the boolean list
type.add(first.correctAnswer);
type.add(second.correctAnswer);
type.add(third.correctAnswer);
So what do i need to do so that I would not need to create a new activity with new questions on each level. Ho to make one activity and display them with fragments for all levels?
Probably you do not need fragments either. You can have multiple layouts (used via setContentView(int id)), or even better, have one single layout with several instances of TextView on it, and set the text in these text views programmatically.
By the way, you can startActivity() and invoke finish() on the current activity (I am not saying you should do namely this, but you can).
You must start using the same fragment in all levels, and load dynamically the questions. Using one activity for each level is a really bad idea.
First, try to create a fragment, inflate it and use it, and model ONE level. Perhaps the (parent/holder) activity can control the current level, and can pass the questions to the fragment accordingly, and save attributes such the score or the game state.
Note that from the MVC (Model-View-Controller) viewpoint an Activity is a Controller and it is wrong to keep Model data in the Controller. The View hierarchy (the View) and the Activity (the Controller) are re-created when the screen turns. Better use a separate class, probably, a singleton, to keep the data that must survive screen turns.

Best way to create a lot of new layouts

I'm currently making a local news app and the main layout has 10 image buttons (more to be added in the future) and I was wondering what would be the best way to get each of these to open a separate layout with an individual text view without making 10 seperate classes and maybe even without making 10 separate layouts. Right now my MainActivty class handles the first button from the layout main_activity which opens a new layout named issue.XML.
Thanks in advanced.
You can create a layout called "newsLayout" that has just one text view and sets it from intent and make the onClick listener of the buttons to start a new activity and passing the desired text (the news) to the intent
Here is some code to help:
newsLayout.java
TextView text = findViewById(R.id.text);
text.setText(getIntent().getCharArrayExtra("TEXT"));
mainLayout.java
public void openNews(View view)
{
Intent intent = new Intent(this, newsLayout.class);
intent.putExtra("TEXT", newsText);
startActivity(intent);
}
Where 'newsText' is the text you want to be shown
I may have wrote some lines wrong because im answering from mobile and i dont remember the exact words, if there's anything you dont understand tell me :)

Java to XML Android

New to Android Development and was wondering if there was some way of taking the users input to create an activity? For example say the user is going through the process of setting up a profile of themself. One of the questions is "how many pets do you have?". The user inputs "4" and then clicks the 'Next' button (which opens the next activity).
How would I take the users input of "4" to create four editText objects in the next activity so that the user can now input the name's of his/her's pets?
I'm an ok-ish programmer (have never touched XML before though) so you don't need to go into much detail I just don't know how I would access this variable to create the four editText objects. From what I've found out you can't add strings to the resources at run-time nor can even edit/append files in resources.
I was thinking of writing an XML file from java and making the activity (written in XML) read the XML file if that's even possible? Can XML files read XML files?
this has nothing to do with xml
the user's input will be stored in a different data type like a string. If you need to load a new activity, one thing you can do is pass information in an android Bundle type.
you start new activities with intent data types. the intent data type has a putExtra method, where you can put variables in.
after you startActivity(yourIntent) the new activity can call Bundle and that has a method called getExtra associated with getIntent()
these should guide you at least
Intents.
String 1 = et.getText().toString();
String 2 = et.getText().toString();
String 3 = et.getText().toString();
String 4 = et.getText().toString();
Intent i = new Intent(this, newclass.class).
i.putExtras("key1", 1);
// and so on.
// Then you get the data by.
bundle extras = i.getExtras();
string temps1 = extras.getString("key1", null);
More info here: http://developer.android.com/reference/android/content/Intent.html
What you're asking about is not really the creation of activities, but rather the creation of layouts. As you're already aware, Android allows you to define a layout using xml and Activities have a simple method to call to set their content view based on that xml. Layouts are not required to be defined in xml though; they can be programatically created.
When you start an activity you create an Intent for it, and you are able to add "extras" to this intent; by doing that you can pass parameters into your activity and it can use those as hints for how to build the layout.
Do some research on programatic layouts (http://mylifewithandroid.blogspot.com/2007/12/xml-and-programmatic-layout.html might help) and you will probably figure out what you need to know.
You can pass data (in your case user input) with the help of intent.
You can send data by using putExtra. Here is good tutorial for you.
For dynamic layout you can inflate your current layout and add controls to your current view. For dynamic layout this is good tutorial to refer.
I hope this two two tutorial will help you.

How to open an activity twice but with different content?

I'm developing a chat app, but I have a problem.
I've a list with the contacts and when I select one contact I'm starting a new activity
Intent i = new Intent(this, MessageScreen.class);
startActivity(i);
but, when I choose another contact to talk I will use the same activity.
but it always open with the last contact screen and the variables still with the old values.
I would like to make something similar to google talk, where you can start to talk with another contact, and all the messages use the same screen, and you can change between the chats fast with no need to reconstruct the screen, reload the messages, etc..
Anyone have any idea of how to implement this?
Sliding between activities isn't a common feature, it sounds like there is one second activity that has a ViewPager that is populated with multiple chats. When starting this activity, they're probably adding the Reorder to front flag to the intent and have overridden onNewIntent to add a new view to the pager.
Try something like*:
i.PutExtra ("key", value);
before starting the activity (e.g. store the user name) and then read your value from the activity and adjust (e.g. UI) it based on the value
note: syntax could differ a bit since I do my Android stuff from C#

Android moving 'windows'

I'm new to android and I'm need a little help understanding how to move from one window to another. I know i use setContentView(R.layout.main) to load an xml layout file, but how do i swap to another layout file? I assume i would use an onClick method on a button and change setContentView(R.layout.other_layout), but would doing all this inside my main activity make my code cluttered? I could end up having 10000+ LOC easily. Can someone explain the correct way to do this please. Thanks
Intents allow us to call another activity from our present activity. For example our current activity is Act1 and we want to move to another activity, Act2. this can be done as:
Intent i = new Intent(Act1.this, Act2.class);
startactivity(i);
Refer http://developer.android.com/guide/topics/intents/intents-filters.html for more information on intents and activity.
Another option is to call setContentView() 2nd time to change the layout.
You use Intents to launch other Activities.
In your current Activity (i.e. window), you can do the following code to launch a new Activity
Intent i = new Intent(this, NewActivityName.class);
startActivity(i);
You should create Activities. Activity is equivalent to window/frame concept on desktop. Each activity should have a goal towards user interaction, ie. taking inputs and showing output. In your case create two activies, and both of them should have their own layour XML and a call to setContentView() within onCreate().
On button click use startActivity() to invoke a new one. Keep in mind that these activities are stacked top of one another.
A visible screen in Android is represented by an Activity. So instead of loading a different layout file into the same activity, you simple create a new activity with it's own layout and java file.
In order to call this second activity from the first one, or to communicate between activities in general android uses so called Intents.
Just look that chapter up in Android's Dev-Gui.

Categories