Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i pass more than one varible from a java activity to a javaclass?

I'm able to pass a string from a java activity to a class of librarie, but i can't figurate put how to send more than one i need to pass three here is the code:

OFActivity.java

package cc.openframeworks.androidMultiOFActivitiesExample;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import cc.openframeworks.OFAndroid;
import cc.openframeworks.androidMultiOFActivitiesExample.R.id;


public class OFActivityA extends cc.openframeworks.OFActivity{

   @Override
    public void onCreate(Bundle savedInstanceState)
    { 
        super.onCreate(savedInstanceState);

    }


    OFAndroid of = new OFAndroid("jala");




}

OFAndroid.java

public class OFAndroid {


   private static String text ;

   public OFAndroid(String text) {
      this.text = text ;
   }


   public void useText() {
      Log.e("TAG" , this.text);
   }

like image 898
545 55 Avatar asked Jan 20 '26 18:01

545 55


1 Answers

Try using a) Array, b) List, or c) Bundle

Array

OFActivity.java

public class OFActivityA extends cc.openframeworks.OFActivity{

    // onCreate()

    OFAndroid of = new OFAndroid(new String[]{"jala", "foo", "bar"});
}

OFAndroid.java

public class OFAndroid {
   private String[] textArray ;

   public OFAndroid(String[] textArray) {
      this.textArray= textArray;
   }


   public void useText() {
      for (String text : textArray) {
          Log.e("TAG" , text);
      }
   }

List

OFActivity.java

public class OFActivityA extends cc.openframeworks.OFActivity{

    // onCreate()

    List<String> listOfString = new ArrayList<>();
    listOfString.add("jala");
    listOfString.add("foo");
    listOfString.add("bar");
    OFAndroid of = new OFAndroid(listOfString);
}

OFAndroid.java

public class OFAndroid {
   private List<String> listOfString;

   public OFAndroid(List<String> listOfString) {
      this.listOfString = listOfString;
   }


   public void useText() {
      for (String text : listOfString) {
          Log.e("TAG" , text);
      }
   }

Bundle

OFActivity.java

public class OFActivityA extends cc.openframeworks.OFActivity{

    // onCreate()

    Bundle bundle = new Bundle();
    bundle.putString("keyOfString1", "jala");
    bundle.putString("keyOfString2", "foo");
    bundle.putString("keyOfString3", "bar");    
    OFAndroid of = new OFAndroid(bundle);
}

OFAndroid.java

public class OFAndroid {
   private Bundle bundle ;

   public OFAndroid(Bundle bundle) {
      this.bundle = bundle;
   }

   public void useText() {
      Log.e("TAG", bundle.getString("keyOfString1")); // jala
      Log.e("TAG", bundle.getString("keyOfString2")); // foo
      Log.e("TAG", bundle.getString("keyOfString3")); // bar
   }

By the way, is there any reason to use static variable in OFAndroid.java? It'll easily cause memory leak problems if you don't control the life cycle and reference of static Objects carefully.

like image 122
yyp Avatar answered Jan 22 '26 10:01

yyp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!