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.
Related
I'm kind of new to android java so I started to create a Music Player app just to learn and practice. The main thing was to control the same MediaPlayer from 2 different activities (In MainActivity there's a listview with the music files and PlayerActivity has the MediaPlayer). I tried to use public voids like:
PlayerActivity pa = new PlayerActivity();
//button onClick
pa.songPlay();
and it kind of worked at first but then a lot of errors occurred when using contexts and MediaPlayer.create(), so I started to look for another way to do this but found none so far. Is there a way to make 2 different layouts with a shared element, and with the same class?
For a better explanation:
MainActivity:
PlayerActivity:
And fuse them like:
Okay so I'm answering my own question because I didn't know that FrameLayouts exist, but since I found them I can put two different layouts into the same space and just set the visibility to VISIBLE or GONE whenever I want to switch between them
My task is to develop a list of different type of questions, a survey. It could include types like Integer-Answer-Question, Long-Text-Answer-Question, and so on.
Why is it needed to be a list? Because for the people using the app is way better to scrolldown answering each question rather than swiping to right, or doing another movement.
So I was face to face with the dilemma of using a ListView or a RecyclerView. My research gave the final outcome of using a recyclerView and having a viewHolder for the different types of questions that I have.
The struggle came when I realized that there is a type of question that has dependency related to it; how so? if you select one option then you have to "show" some questions, and if you deselected this option then you have to "hide" it again.
The thing is that I need to know the reference of each question to their viewHolder in order to "show" o "hide" each of them, but if the recycler is recycling viewHolders then it could create a mess on my logic.
My punctual questions are: Am I using the correct component with the RecyclerView?, is there any way to access a viewHolder with a unique reference, like and id or something?.
if you need me to show some code, I'd do it happily.
Valuable information:
if you are interest in how notifyDataSetChanged() works you can access to this link for further and detail information.
Minas mina's approach was the correct one!
If I understand correctly, you need to hide some types of questions when the user selects an option.
Your understanding of what the view holders are supposed to do is not quite right. The view holders cache a bunch of views that you later use in onBindViewHolder() to fill-in data from your model objects.
The actual model objects should be in your adapter. In your case, something like
List<Question> questions
In onBindViewHolder(), you fill-in the fields of the View holder with the data from a Question object.
As for your question, what you can do is to set a flag in your recyclerView adapter, e.g. hideQuestionsOfTypeA to true and then call notifyDatasetChanged() on the adapter.
When binding objects, check if that flag is true and if yes, set the visibility to GONE to the views that need to be hidden.
I'm building an app for educational purposes, with a lot of questions and challenges to the user to solve. But, it would be a HUGE amount of screens if I create a lot of questions and one xml file and activity for each one, as I want to have different kinds of questions, where the user should be able to write the code, select a block of code or select the correct answer about theory and stuff. Is there a way to use the same model of screen(xml file) for one specific kind of question? Like, using one model to all the screens and questions where the user should select the correct answer, and another model to another kind of question..
p.s: Yes, i'm kind newbie in Android
Thanks! :)
I would recommend using FragmentStatePagerAdapter
You only needs one layout and every time a fragment looses focus, it is destroyed automatically.
It is the same as adapters. You just create the visible views and 2 more. In this case you will have the visible question and one on the left and one on the right.
Let me know if you need any help.
You can define all kinds of questions, such as checkboxes, radioButtons, Edittexts and etc. in a xml file and, if necessary, visible or hide them.
Use Fragments to display questions in your activity.
In short, Fragments are mini-Activities with their own layouts, so each different type of question should have a corresponding Fragment. That should be exactly what you need.
For example, you can create a fragment_radiobox and a fragment_codesnippet in your project. In first one you will include a TextView for your question and a few checkboxes for an answer, and in the second one you'd have an EditText field to write some code. You can call any of those two fragments in your main activity depending on which question you want to display.
I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.
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 ;)