I have an class Voice, which extends Activity, and contains a counter. When the user answers correctly, the counter adds one via counter++;
public class Voice extends Activity implements OnClickListener{
ListView lv;
static final int check = 111;
int counter_score;
TextView txView;
MediaPlayer ourSong;
ImageView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letter_a);
initialize();
}
private void initialize() {
lv = (ListView)findViewById(R.id.lvVoiceReturn);
Button b = (Button)findViewById(R.id.imageButtonSelector);
txView = (TextView)findViewById(R.id.counter);
b.setOnClickListener(this);
counter_score=0;
}
This score, is bundled and passed on to the next activity "What" within a string "your score is 1".
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv.setAdapter( new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
if(results.contains("hey") || results.contains("a") || results.contains("ay")) {
//toast referenced to xml the after 400ms
counter_score++;
txView.setText("Your Score is" + " " + counter_score);
AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
dialogBuilder.setTitle("AWSOME");
dialogBuilder.setMessage("¡Your current score is" + counter_score);
dialogBuilder.setIcon(R.drawable.ic_mark);
dialogBuilder.show();
ourSong = MediaPlayer.create(Voice.this, R.raw.rightsound2);
ourSong.start();
Thread timer = new Thread() {
public void run(){
try {
sleep(2500);
}catch (InterruptedException e){
e.printStackTrace();
} finally {
String score = txView.getText().toString();
Bundle keeper = new Bundle();
keeper.putString("key", score);
Intent putScore = new Intent(Voice.this, What.class);
putScore.putExtras(keeper);
startActivity(putScore);
}
}
};
timer.start();
}
}
The next Activity, What, gets this Bundle and displays it fine using setText(gotScore)
public class What extends Activity implements OnClickListener {
ListView lv;
static final int check = 111;
private int counter_score;
TextView txView;
MediaPlayer ourSong;
ImageView display;
String gotScore;
String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5",
"example6"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letter_b);
initialize();
Bundle gotKeeper = getIntent().getExtras();
gotScore = gotKeeper.getString("key");
txView.setText(gotScore);
}
private void initialize() {
// TODO Auto-generated method stub
lv = (ListView)findViewById(R.id.lvVoiceReturn);
Button b = (Button)findViewById(R.id.imageButtonSelector);
txView = (TextView)findViewById(R.id.counter);
b.setOnClickListener(this);
..this is when things go bad :(
On What I have another question tied to a counter as well. When the user answers correctly the counter adds one via counter++; and it does. However, it changes the txview string to "your score is 1". I can't get it to add 1 to the counter result passed from the previous activity within the string, so that the counter on What reads "your score is 2". This gets passes to the next activity in Bundle keeper, which holds the aggregate score.
I've read a few tutorials on passing an int verses a string, but some of the code they use like getInt is not recognized. I'm stumped.
What you're bundling and passing to the What activity is not the counter but the string "Your score is 1". If you want to increment that number in the next activity then you should be sending just the integer value and constructing whatever string you need there instead.
I ve read a few tuts on passing an int vs a string..but some of the code they use like getInt is not recognized..anywho Im stumped..
I'm not too sure I know what you mean by getInt() is not recognized. In any case, make things easier for yourself when passing counter from one activity to another. If it is an int and you plan on manipulating like an int in the receiving activity then add it to the bundle as an int. For example:
Bundle keeper = new Bundle();
keeper.putInt("key", counter_score);
And retrieve it from the bundle with:
Bundle gotKeeper = getIntent().getExtras();
int score = gotKeeper.getInt("key");
What if you make a "global" class to be shared across the different activities, and use it to keep the variables used "in sync"?
For example - Globals.java:
public class Globals {
public int counter_score;
}
And then reference that variable using Globals.counter_score
You can of course also use that shared class for other variables and functions as well - for example common operations.
Update
As the commenters pointed out, this method isn't particularily good - I forgot that the code is simply referenced, and doesn't "live" on its own to keep information for the other activities (thanks for correcting me on that one, I'm still learning...)
Something that COULD work better, though, is to pass the current state of the counter_score variable in the intent when you launch your second activity - for example:
IntentToLaunchTheOtherActivity( counter_score );
And then maybe pass the variable back to the previous activity if it's changed afterwards...
I got it work. Essentially I needed to what what TJ Third suggested converting keeper.putString("key", counter_score); to keeper.putInt("key", counter_score);, I also needed to convert the bundle being received to an int within the "What" activity. Within "What" activity I renamed int counter_score; and int gotKeeper;(this was String gotKeeper) then instead of calling counter_score =0; now that the bundle passed is an int,I called counter_score = gotKeeper; under initialize(); so the counter score equals the result generated from the previous activity "Voice".
Now when the user answers correctly, counter++; adds one to the existing counter_score and bundles it and send it to the next activity, and rinse a repeat.
static final int check = 111;
int counter_score;
TextView txView;
MediaPlayer ourSong;
ImageView display;
int gotKeeper;
String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5",
"example6"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.letter_b);
initialize();
Bundle gotKeeper = getIntent().getExtras();
gotKeeper = gotScore.getInt("key");
counter_score = gotKeeper;
Again thnx to everyone for your suggestions and insight.Huge help to a newbie.
Related
How do I assign user input (from a TextView) into a variable then call that variable in another class?
From my MainActivity, I have the followingn where user input is taken:
Button confirm;
EditText inputField;
String typedChar;
char[] cars = typedChar.toCharArray();
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
confirm = (Button)findViewById(R.id.btConfirmInput);
inputField = (EditText) findViewById(R.id.etInputChars);
confirm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
typedChar = inputField.getText().toString();
}
}
);
I'm trying to store the input and convert it to char
String typedChar;
char[] cars = typedChar.toCharArray();
Now I want to use cars in another class in the following method which print to a custom view:
private void drawText() {
for (int i = 0; i < txtPosByColumn.length; i++) {
canvas.drawText("" + cars[RANDOM.nextInt(cars.length)], i * fontSize, txtPosByColumn[i] * fontSize, paintTxt);
if (txtPosByColumn[i] * fontSize > height && Math.random() > 0.975) {
txtPosByColumn[i] = 0;
}
txtPosByColumn[i]++;
}
I'm however able to assign hardcoded value to cars like bellow:
private char[] chars = "010101".toCharArray();
but I want it come from user input
Anyone please kindly advice, guide. I know I'm doing things wrong but can't figure out...
PS: Noob here
You put your variable in an Intent like this:
confirm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
typedChar = inputField.getText().toString();
char[] chars = typedChar.toCharArray();
Intent intent = new Intent(MyCurrentActivity.this, MySecondActivity.class);
intent.putExtra("somethingWithARelevantName", chars);
startActivity(intent);
}
}
);
And you get it in your second activity like this:
Intent intent = getIntent();
char[] chars = intent.getExtras().getCharArray("somethingWithARelevantName");
edit: if want your variable in a class that is not an activity, you can pass it in the constructor:
class MyClass{
char[] chars;
MyClass(char[] chars){
this.chars = chars;
}
}
You should specified what is the type of that other class.
If it is a simple Java class you can pass it as a field to your drawText(char[] array);
If however you are dealing with activities the :
In your first activity, before launching the other activity, use Extra intent to send data between activities as the answer show before.
I am developing a Quizz App in which an activity shows question and options from SQLite and on selecting option, another activity is showing result for 2000 ms(it has a timer)and then it calls First Activity via an Intent.
So, Most of the interaction is between 2 activities. But each time my MainActivity is called, it re-initializes all the variables again and again.
I am opening my database connection in onCreate() and also keeping a counter (that can count how many questions have been asked yet) whose value is not retained after the intent from Second Activity. I am worried on how to solve this.
I am a bit confused about the life cycle that is followed. Whether the call to First Activity from Second one starting with onCreate() or it's also initializing the instance variables again.
This is onCreate() method I wrote:
public class MainActivity extends Activity {
protected static final int SCORE_INCREMENT = 5;
TextView question;
Button score, opt1, opt2, opt3;
MyDatabaseManager dbManager;
QuizManager quizManager;
private int quiz_counter =1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbManager = new MyDatabaseManager(getApplicationContext());
dbManager.open();
quizManager = new QuizManager(MainActivity.this, dbManager);
Toast.makeText(MainActivity.this, "Asking The First Question", 0).show();
askQuestion();
}
}
Is there any difference between the above written code and the one I am writing now... if the activity is called again via an Intent
public class MainActivity extends Activity {
protected static final int SCORE_INCREMENT = 5;
TextView question;
Button score, opt1, opt2, opt3;
MyDatabaseManager dbManager = new MyDatabaseManager(getApplicationContext());
QuizManager quizManager = new QuizManager(this, dbManager);
private int quiz_counter =1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "Asking The First Question", 0).show();
askQuestion();
}
}
This might be a silly question. But it's a bit confusing for me. Suggestions are welcome.
If you have variables that you want to maintain between changing activities, then you should either
Store them in SharedPreferences
or
Pass them between the Activites in the Intents (see Starting another activity)
I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );
public class MainActivity extends Activity implements OnClickListener, AdapterView.OnItemSelectedListener, OnCheckedChangeListener{
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
//Declare Variables to be used
final Bundle bundle = new Bundle();
final CheckBox checkbox01 = (CheckBox) findViewById(R.id.CheckBox01);
//All if statements for builds
final Button findbuilds = (Button) findViewById(R.id.btn_FindBuildsTab1);{
findbuilds.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if ((v==findbuilds)&&(checkbox01.getText()==("Ahri-AP"))&&((checkbox01).isChecked())){
final String charactername = "Ahri-AP";
}
if ((checkbox01).isChecked(){
//All Bundles are placed here
bundle.putString("CharacterName Value Key", charactername);
//Start Tabhost Activity and pass bundle(s)
Intent nextActivity = new Intent(v.getContext(),
nextActivity.putExtras(bundle);
startActivity(nextActivity); TabHostActivity.class);
}
Basically, what i am trying to do is create the string for the character's name in the first if-statement, and then bundle it in another if-statement. The reason i need to bundle the string(s) in a different if-statement is to avoid reaching the bytes limit of a method. I have a total of 64 different Strings that need to be bundled for each if-statement, and i have a total of 126 if-statements that need to happen. The problem is of course that the String variable is never found or resolved with the bundle in the 2nd if-statement. Is there a way to do this? Or maybe a more efficient way? Thanks in advance for the help! :)
Just pull the definition of your variable outside of the if statement. eg:
final String charactername
if() {
charactername = ...
}
if() {
bundle stuff with charactername
}
I want to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.
I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
EditText BMI_weight;
public String weight;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_main);
Button submit =(Button)findViewById(R.id.BMI_submit);
BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
weight = BMI_weight.getText().toString();
// create a bundle
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("wt", weight);
// add bundle to the intent
Intent intent = new Intent(v.getContext(), BMI_Result.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
);
Second Activity :
public class BMI_Result extends Activity
{
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//get the bundle
Bundle bundle = getIntent().getExtras();
// extract the data
String weight = bundle.getString("wt");
Weight.setText(weight);
}
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");