Flipping different data in the same activity when button is clicked - android

This is my first question to stackoverflow, so please do not see my acceptance rate.
What I am trying to create is a quiz in android. Here I am having the same layout where I have a question and 3 answers. I have a next button at the bottom. When I click on that the same layout should get loaded with new question and options for it.
I am thinking of how to do it. I saw something called view flipper, but can anyone tell me the exact solution for this?
Here is my code
public class TriviaQuiz extends Activity {
Trivia trivias = TourDescription.currentTour.getTriviaArray().get(0);
Here trvias is an object which contains all the data.
}
// send the trivias to the activity where I set the question and options
public void playquiz(View v) {
Intent quizIntent = new Intent(TriviaQuiz.this, Quiz.class);
Bundle bundle = new Bundle();
bundle.putSerializable("trivia", trivias);
quizIntent.putExtras(bundle);
startActivity(quizIntent);
}
public class Quiz extends Activity
]
In this last activity I have the layout and the next button. Hope it is clear now.
Note:Here am trying to update the array value at get(0)
Please help me a way for this.

You can just do it like this, call a method with the array index and then inside the method set the values as needed. Next when tap on the button,update the value of array index in the required manner.

You should update values on the button click for the next question. Do you see any problem with this approach?

Related

I have around 1000 different activities in my Android App. How can I jump to a random activity?

Intent myIntent = new Intent(this,Q1.class);
In my app I have 1000 different activities namely Q1,Q2,Q3....Q1000.
I have a button on each activity and I want when a user clicks on it, he should land on some random activity.
How to achieve this ?
Although this is terrible, I'll still show a possible way to do this. But just remember, this is TERRIBLE.
You can store all the activity classes in an ArrayList.
ArrayList<Class<Activity>> activities = new ArrayList<> ();
And then you add all the activities into the ArrayList. Yeah, I know, this part is tedious. For example,
activities.add (Activity1.class);
And then you create a Random called rand and use that to access an element in the list:
list.get (rand.nextInt (list.size()));
Here is another "better" way to do it, but it's still kinda bad. I strongly advise you to store teh questions in a database. Anyway, here's the better-but-still-bad method.
You create a question class:
public class Question {
//here you can put correctAnswer, questionText etc
}
After that, you make an ArrayList of Questions i.e. ArrayList<Question>.
ArrayList<Question> questions = new ArrayList<> ();
And still, you need to add 1000 questions to the array list. Then you can just use a Random to access it.
When you want to display a question in one activity, you can just putExtra in Intent before starting the activity. putExtra basically passes some "parameter" thingys to the activity. Then in the activity, you can just get the "Extra" and use the Question object to display. e.g. set the text of a TextView to the question text.
this can be also used:
try {
Random rand=new Random();
Intent i = new Intent(this, Class.forName("test.hu.test.Q"+rand.nextInt(1000)+"")); // get activity's class by reflection
startActivity(i);
}catch (Exception e)
{
}
But i also suggest to use DB.

Application with different requests sent to a database (SQL) depending on buttons clicked

I still am a beginner in Android development and will try to make my question as clear as possible with a schema of what I have in my mind.
Purpose of this application:
- I want the user to have the choice between a few buttons and when clicking on any of them, it would open a list view with different content according to the button.
ex : if you click on "Category_1" button, only elements with a fitting id will appear in the listview.
So far, I have :
- defined my "handler" class (extends SQLiteOpenHelper) : name/path of DB, definition of CRUD, .getReadableDatabase, etc.
- define a class for my table, in my case "Restaurants.java" with getters/setters and constructor.
- defined my MainActivity with empty listeners for my button.
- defined my "DatabaseAdapter.java" in which I want to define the methods/sql requests which will communicate with the database and get the information I want from it.
- defined my ListViewActivity with nothing to display so far.
Here is a schema of what I want with the idea of how to make it to try to optimize my application :
To sum up:
- I want a listener for each button setting a variable to a certain value (for example: "if you click on 1 then set the value of A to 1") and opening the ListViewActivity.
- There would be a method defined in "...Adapter.java" sending a request to the database and having the variable A defined earlier as an input.
- And then, when clicking on the button, the ListViewActivity will open and call the method from "..Adapter.java", and finally display the results.
So, here are my questions :
- First of all, is the design optimized enough to allow my application to run fast? I think it should as all the button open only one activity and there is only one method defined for all buttons.
- I have a hard time defining the method in "...Adapter.java" which will be called from my ListViewAcitivity. The input should be the variable obtained when clicking on the button but I don't really know how to get a variable in one activity, open a second activity where to display results by using the variable in a third activity... :s
Is it fine to set a variable to a certain value when we click on a button and use this variable in another class as an input for a method?
public findNameInTable(int A){
string sql = " select NAME from MY_TABLE where CAT1 = " + A;
c = database.rawQuery(sql, null); }
Thanks in advance for any indications, suggestions or links which could help me to make my application come true, and sorry if some questions really sounds newbie, I am starting !
Have a good day !
Part 1: The best way I have found to pass variables to other activities is with a putExtra(String, variable);. Say you change the variable name on a button press, you can then call:
YourNewActivityClassName var = new YourNewActivityClassName();
Intent i = new Intent(context, YourNewActivityClassName.class);
i.putExtra("name", name);
startActivity(i);
Then in the activity you just created, you can call this in the onCreate method:
Intent i = getIntent();
final String name = i.getStringExtra("name");
Of course this is assuming the variable was defined as a String before the putExtra was called.
If you want to use other variable types, there are different get***Extra commands you can call like getIntExtra(int, defaultval) but the putExtra will still be used to send it.
Part 2: For calling a method with a variable assigned in a button click, I have found the best way to do this is with a "holder class" this holder can be defined as a final, and a button press assigns a value to one of it's slots. Here is my holder for Integers:
public class holder {
int to;
public void setTo(int to){
this.to = to;
}
public int getTo(){
return to;
}
}
I instantiate my class as final within my on create final holder hold = new holder();
then call my hold.setTo(int); within a list click listener. When I want to get the data, I simply call hold.getTo(); and I have my integer.
Heres a similar post: Pass value outside of public void onClick
Hope this helps!
Mike

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

android multiple choice using images

I have 10 images in an array which are answers to 10 questions which are also images in another array.
I have 10 image buttons coming up on my relative layout loaded with the 10 answer images. The answer array is answers[] and the questions array is questions[]. They correspond to each other, meaning that answers[1] is the answer to questions[1], and answers[4] is the answer to questions[4], and so on. I have a simple "for" statement which is for(int i=0; i<=9; i++).
I want the 10 questions to come up one at a time, and let the user answer. I need the program to pause and let the user click the answer to the question. With that "for" statement, the first question which is questions[0] will pop up in imageview. Using if statements, I want to say "if the imagebutton with images[0] is clicked then I want an image to display that will say correct and do some other stuff, else I want an image to say incorrect and do some other stuff" and then I want it to pause until the user just clicks anywhere on the screen.
Can anyone help me with the pausing in the "for" statement to let the user answer, and the "if" statement where if the correct answer is picked I can display an image that says correct etc, and then pause again until the user just clicks anywhere on the screen?? Thanks!
By the way, I need those question images to pop up in the same imageview each time obviously, just changing the image.
You can't really control the program flow the way you want to with a for loop. The user interface in Android is event-driven, so the only way to move from question to question is by reacting to the event that is generated when the user clicks a button. You do that by finding the button and attaching an event handler for the click event, called onClick, which is a part of the View.OnClickListener interface. You can define a variable on your activity class to store which question you're on and use that to step your way through as the user answers questions. Your activity might look something like this:
public class QuestionActivity extends Activity implements View.OnClickListener {
public int currentQuestion = 0;
Image[] answers;
Image[] questions;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load questions and answers
setContentView(R.layout.mylayout);
final ImageButton button = (ImageButton)findViewById(R.id.mybutton);
button.setOnClickListener(this);
}
public void onClick(View view)
{
if (/* the answer is correct */) {
// happiness: show images, do what needs doing
} else {
// sadness: show other images, etc
}
currentQuestion++;
}
}

Problem with list activity

I have implmented pagination and it display 5 records per page. Now suppose I am on page 3 and click 3'rd element then 3'rd element of page-1 is selected.
I am not able to figure out problem as I always create new list object while setting data.
I used below code
temp = new ArrayList();
this.someListAdapter = new SomeListAdapter(this, R.layout.row_facet,temp);
setListAdapter(this.someListAdapter );
Below is signature of SomeListAdapter class.
public class SomeListAdapter extends ArrayAdapter<VoNeighborhood> {
}
Please help....
There really aren't enough details here about how you do pagination with your ListView.
So I might guess you're overriding onListItemClick and using the position variable it sends you, but you then don't take into account the page you're on?
Alternatively, just don't use pagination as you have an infinite canvas to scroll your list within — I don't think I've recall seeing an Android app so far that uses pagination!

Categories

Resources