java - Append TextView of another activity through main -
i'm trying create game log, show happened on each round of game. log in activity want updated whatever player does. player presses buttons things. it's code post, have made new bundle, , intent.
in mainactivity.
bundle bundle = new bundle(); intent gamelogswitch = new intent(getapplicationcontext(),gamelog.class);
i trying put send other activity don't know if can put variables in it. otherwise works simple words such ("key","it works")
gamelogswitch.putextra("break","---break---"+"\n"+player1name+": "+greenresult.gettext()+"("+grtop1+","+grtop2+","+grtop3+")"+"\n"+player2name+": "+redresult.gettext()+"("+rdtop1+","+rdtop2+","+rdtop3+")");
and of course have when gamelog button pressed
startactivity(gamelogswitch);
now in gamelog.class 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 couple of questions. first, why append work once string when start activity i.e when go , press same button again won't write same thing again underneath?
secondly, doesn't seem work "break" key, have fact there variables in text i'm sending? works simple text gamelogswitch.putextra("key","it works");
there must easier way this! thank you.
update 1: answer drv seem work, when try gamelogtext.append(appconstants.log)
replaces in textview no matter how many times press button. think activity resetting each time start again. way around this?
make global string in constants class , use wherever want:
public class appconstants{ public static string log=""; }
edit string in class sending in intent:
appconstants.log="---break---"+"\n"+player1name+": "+greenresult.gettext()+"("+grtop1+","+grtop2+","+grtop3+")"+"\n"+player2name+": "+redresult.gettext()+"("+rdtop1+","+rdtop2+","+rdtop3+")";
and use in 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 log here using appconstants.log } }
Comments
Post a Comment