Flash AS3 Pass my var data to another frame - java

I'm using AS3 / AIR 3.2 for Android.
I'm having a trouble about passing my variable data to another frame. I read some forums about this but I'm only new this so I don't have yet any idea.
I have an input text and button in my frame 1 where the user will input a name then the data entered will be save. (I used SharedObject) but all the data inputted will appear on frame 2.
While my frame 2 is a dynamic text where all the data will appear.
This is the code for my frame 1
import flash.net.SharedObject;
var myName:String;
myResult.text = "";
var mySO:SharedObject = SharedObject.getLocal("test");
if (mySO1.data.myName != null){
myResult.text = mySO1.data.myName;
}
else {
myResult.text = "No Name";
}
submit_btn.addEventListener(MouseEvent.CLICK, gotomyNextFrame);
function gotomyNextFrame(event:MouseEvent):void
{
nextFrame();
myName = myInputName.text;
trace(myName);
myResult.text = myName;
mySO.data.myResult = myInputName.text;
mySO.flush();
trace(mySO.data.myResult);
}
Error: Error #1009: Cannot access a property or method of a null object reference. I think this is because I'm wrong in passing of data into frame.
Attempt: I tried show the output on the same frame and I didn't encounter any error.

Your SharedObject var is mySO and not mySO1, and to share data between frames, you can use a variable like this :
frame 1 :
...
var shared_data:String = txt_input.text
nextFrame()
...
frame 2 :
// get shared_data and use it as you like
another_input.text = shared_data
shared_object.data.current_name = shared_data
...
Edit :
/* frame 01 */
// shared_data should be declared here to be a global var not inside a function
var shared_data:String
submit_btn.addEventListener(MouseEvent.CLICK, gotomyNextFrame)
function gotomyNextFrame(event:MouseEvent):void {
// here you should just assign a value to shared_data var
shared_data = yourName.text
nextFrame()
}
/* frame 2 */
stop()
import flash.net.SharedObject
// if you redefine shared_data var here you will lost it's value and you will get a null value
// var shared_data:String
var mySO:SharedObject = SharedObject.getLocal("test1")
myResult.text = shared_data
// here your SharedObject object is named mySO and not SharedObject
//SharedObject.data.mySO = shared_data
mySO.data.yourName = shared_data

Related

Getting Input From Generated EditText Android Kotlin

I'm making a random word generator and I'm having problems getting the input from the generated editText in Kotlin. I have found a few solutions in java and I can see how they work but I'm having trouble putting it into Kotlin.
I've set it up the so the EditTexts are generated by a while loop and the Id is stored in an array call "arraylist". I then wanted to use the Id in the array to obtain the "text" from each editText and put them into the "Strings" variable. I think in java you'd use "string[i]" so the variable becomes string1, string2 etc. I can't get this to work. I've tried printing the array and its blank so I don't think I'm getting the id correctly.
There are a few logic issues with code such as there already being an input that I'm using for formatting and arrays starting at 0 and such that I'll sort out later.
Thanks
Jake
class WordList : AppCompatActivity() {
#RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_word_list)
//Get Linear layout as variable
val linearLayout = findViewById(R.id.InfoLayout) as LinearLayout
val Test = findViewById(R.id.WordsInput) as EditText
val RandomiseButton = findViewById<Button>(R.id.RandomiseInputs) as Button
var Value = "Hello" as String
var editText = EditText (this)
var List = arrayListOf<String>()
var arraylist = ArrayList<Int>()
val strings = ArrayList<String>()
//Get Inputs from Previous page
var Choice = intent.getIntExtra("Amount", 0)
/*To Do
Get Inputs From Created Inputs
Randomise
Print output
*/
//Add new input
if (Choice >= 2) {
//Create Var for Edit
var Number = 2
//While loop to create multiple EditText fields
while (Number <= Choice) {
editText = EditText (this)
editText.hint = "Input " + Number
editText.setId(Number)
//Use Appearance To change things you can't set using style.xml
editText.setTextAppearance(R.style.TextHintFont)
editText.setTextColor(Color.parseColor("#E321C2"))
editText.setHintTextColor(Color.parseColor("#E321C2"))
editText.setEms(10)
//Set Edit
linearLayout.addView(editText)
arraylist.add(editText.id.toInt())
Number++
}
}
RandomiseButton.setOnClickListener {
var Random = (0..Choice).random()
var i = 0
while (i <= arraylist.size) {
strings.add(arraylist.get(i).text.toString())
i++
}
var OutputW = strings.get(Random).toString()
//Value = editText.text.toString()
var intent = Intent (this#WordList,WordsOutput::class.java)
intent.putExtra("RandomOut",OutputW)
startActivity(intent)
}
}
}
So I just worked it out
RandomiseButton.setOnClickListener {
var Random = (0..Choice).random()
var OutputW = linearLayout.getChildAt(Random) as EditText
var another = OutputW.text.toString()
var intent = Intent (this#WordList,WordsOutput::class.java)
intent.putExtra("RandomOut",another)
startActivity(intent)
}
I used the getChildAt to just randomly select a field.
more info here
https://www.i-programmer.info/programming/android/11415-android-programming-in-kotlin-layouts-and-autonaming-components.html?start=1
Only took me 3 days hahaha

How to generate field and initialize value

How to initialize a field that is generated. Or in the code example below, where can the AssignExpr object be added for the code to work?
private void addConfigField(ClassOrInterfaceDeclaration clazz) {
var className = "BlaConfig";
var configField = clazz.addField(className, "blaConfig", Modifier.PRIVATE);
var configFieldExpr = new NameExpr("blaConfig");
var newConfigObj = new ObjectCreationExpr(null, JavaParser.parseClassOrInterfaceType(className), new NodeList<>());
var assign = new AssignExpr(configFieldExpr, newConfigObj, Operator.ASSIGN);
}
Using com.github.javaparser:javaparser-core:3.2.4
You can get the variable declared in the 'configField'. That variable may be initialized.
configField.getVariable(0).setInitializer(/* Your code */);

How do I handle Java NoSuchFieldException?

I am trying to loop through request body keys and set values of an object when the key and an object field correlate like so:
User user = new User((String) super.session.get("id"));
Class<?> c = user.getClass();
for(String key : super.body.keySet()){
Field f = c.getField(key);
if(f != null){
if(key.equals("avatar")){
uploadFile();
}
f.set(user, super.body.get(key));
}
}
user.update();
However, I keep getting the NoSuchFieldException and my code stops working. How can I deal with the possibility of a field not existing?

Realm & Kotlin access field in variable

I am having the following situation:
I am getting some data via Retrofit2 & GSON that I dont want to save completely in the realm database. But i need to access the model later on.
Well here is the model:
open class Notification() : RealmObject() {
#PrimaryKey var pushNotificationId: Long = -1
var date: Date = Date()
var apsRaw: String = ""
#Ignore var aps: Aps? = null
get() = field ?: Gson.getInstance().fromJson(apsRaw, Aps::class.java)
private set
with
open class Aps(var message: String = "",
var category: String = "")
What i want to achieve is, to save the apsRaw string in realm only (to avoid having another table 'Aps') but during runtime i want to use the Aps instance for convenience reasons. So when loading it from realm i would like to initialize it if it has not been initialized before.
This way i am calling the Gson converter every time because the access to realm via field always returns null
Any suggestions?
How about
#Ignore var aps: Aps? = null
get() {
if(field == null && "" != appsRaw) {
field = Gson.getInstance().fromJson(apsRaw, Aps::class.java)
}
return field
}
private set

How to change the value of a variable once per day in Java/javascript

I'm trying to make an application/script for my school which displays the day's schedule. The catch with this is that my school runs on an 8 day cycle, so that complicates things. I have a a variable called cycleDay , but how would I go about updating this just once per day, and not more than that? If there's another way you can think of doing this, please let me know.
Thanks!
You can use, say, the getTime() function of the Date object, which returns the current time in milliseconds after January 1, 1970 (source: http://www.w3schools.com/jsref/jsref_gettime.asp), and save that value (e.g. in var lastUpdateTime). Then periodically check if the difference between the current time and that saved time is more than a day. If so, update cycleDay, and also update lastUpdateTime to the time when you updated.
For instance, initialize with:
var lastUpdateTime = new Date().getTime();
Somewhere else in your code:
var currentTime = new Date().getTime();
if(currentTime - lastUpdateTime >= 24*60*60*1000) // number of milliseconds in a day
{
// update cycleDay
lastUpdateTime = currentTime;
// ...
}
private Date lastUpdate = now()-1;
private myDailyChahgedValue;
Integer getMyDailyChangedValue() {
if (lastUpdate <> now()) {
myDailyChahgedValue ++;
}
return value;
}
Please note it is an a draft of code what shows main idea
Use Date and document.cookies to update your variable. Also i used two utility methods to manipulate document.cookies
var today = parseInt(new Date().getTime()/(1000*3600*24))
var cookies = getCookies();
if(cookies["last_updated"] && cookies["last_updated"]<today)
{
/* update variable */
setCookie("last_updated", today, 1);
}
/* utility methods starts, adding utility methods to simplify setting and getting cookies */
function setCookie(name, value, daysToLive) {
var cookie = name + "=" + encodeURIComponent(value);
if (typeof daysToLive === "number")
cookie += "; max-age=" + (daysToLive*60*60*24);
document.cookie = cookie;
}
function getCookies() {
var cookies = {}; // The object we will return
var all = document.cookie; // Get all cookies in one big string
if (all === "") // If the property is the empty string
return cookies; // return an empty object
var list = all.split("; "); // Split into individual name=value pairs
for(var i = 0; i < list.length; i++) { // For each cookie
var cookie = list[i];
var p = cookie.indexOf("="); // Find the first = sign
var name = cookie.substring(0,p); // Get cookie name
var value = cookie.substring(p+1); // Get cookie value
value = decodeURIComponent(value); // Decode the value
cookies[name] = value; // Store name and value in object
}
return cookies;
}
/* utility methods ends */

Categories

Resources