deathskiller deathskiller - 5 months ago 20
Java Question

Append TextView of another activity through main

I'm trying to create a game log, to show what happened on each round of the game. The log is in another activity but I want to be updated constantly with whatever the player does. A player just presses buttons that do certain things. It's too much code to post, but I have made a new bundle, and an intent.

In MainActivity.

Bundle bundle = new Bundle();
Intent GameLogSwitch = new Intent(getApplicationContext(),GameLog.class);


I am trying to put this to send to the other activity but I don't know if you can put variables in it. otherwise it works with simple words such as ("key","It works")

GameLogSwitch.putExtra("Break","---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")");


and then of course I have this when the gamelog button is pressed

startActivity(GameLogSwitch);


Now in Gamelog.class i have this.

package com.example.adam.snookerproject;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class GameLog extends AppCompatActivity {
private TextView GameLogText;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
GameLogText = (TextView) findViewById(R.id.GameLogText);
Intent GameLogSwitch = getIntent();
String myString = GameLogSwitch.getStringExtra("Break");
GameLogText.append(myString);
}
}


I have a couple of questions. First, why does append only work once with my string when I start the activity i.e when I go back and press the same button again it won't write the same thing again underneath?

Secondly, it doesn't seem to work for my "Break" key, does this have to do with the fact that there are variables in the text I'm sending? It only works for simple text like
GameLogSwitch.putExtra("key","It works");


There must be an easier way to do this! Thank you.

UPDATE 1: The answer from Drv does seem to work, but when I try to do
GameLogText.append(AppConstants.log)
it just replaces everything in the textview no matter how many times I press the button. I think the activity is just resetting each time I start it again. Any way around this?

Drv Drv
Answer

Make a global string in Constants class and use it wherever you want:

  public class AppConstants{

        public static String log="";
  }

Edit the string in the class where you are sending it in intent:

  AppConstants.log="---BREAK---"+"\n"+Player1Name+": "+GreenResult.getText()+"("+GrTop1+","+GrTop2+","+GrTop3+")"+"\n"+Player2Name+": "+RedResult.getText()+"("+RdTop1+","+RdTop2+","+RdTop3+")";

And use it in your class as:

  package com.example.adam.snookerproject;

  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.TextView;

  public class GameLog extends AppCompatActivity {
    private TextView GameLogText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_game_log);

       GameLogText = (TextView) findViewById(R.id.GameLogText);

       //get your log here using AppConstants.log
   }
  }
Comments