Passing String Array Between Two Class in Android Application

Passing String array between two class in android application

If you are trying to send a String-array from one Activity to another this can be done in the Intent.

In ClassA:

Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);

In ClassB:

public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}

Passing string array between android activities

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);


Hope this will help you.

In order to read:

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

Passing a String Array Between Java Classes Android App

You're creating a new accelerometer class, which is completely uninitialized since there is no constructor, then you access its member. Of course it'll be null.

Not sure how your two classes are related, but if the activity is called by the service, then you need to pass the string through the intent (through an extra, for example).

Side note: Class names should always start with a capital letter. Method/variable names should have camel case, i.e. "updateArray". Also, you can format your code here by selecting it and pressing CTRL+K.

Passing string array to another activity

You have to do the following:

Intent i = new Intent("Activity")
i.putExtra(key, ArrayName)

Intent i = getIntent();
String[] array = i.getExtras().getString(keyname);

and that's it.

Android - Passing an Array of Classes Between Activities

Since we're dealing with a Map, you should go with:

Object[] array = (Object[]) extras.get("classes"); // you cannot cast to Class[] !

and then:

Class a = (Class) array[0]; // and etc.

Android: Passing String Array over Multiple Activities

You put the code below in a file named data, in your code you then use just it by calling data.array

public class data {
public String[] array = new String[1];
}

But going with just passing through a String[] you shouldn't need bundle.

simply

intent.putExtra("stringArray".String[]);

and get it with

this.getIntent().getStringArrayExtra("stringArray")

pass array between 2 activity with intent

Best solution I can suggest is define your array as

public static int[] xyz=new int[]{1,2,3,4};

And now that static int array can be called or used from any activity provided if they are in the same package.
Here for your example in GraphCurrentActivity,
Add this import statement:

import static yourpackagename.BluetoothLeService.xyz;

//And these lines to read that array:
int[] arraydata;
arraydata = xyz;

And let me know if this was wrong.

Java, trying to pass string array from one class to another

Beside other answers, if you want to pass data from Fragment's "likeTitles" variable to Activity's myStringArr variable on constructor method, you have to do like this:

class MyFragment extends Fragment{
public String likeTitles[] = {"mazda","whynot","OHYEAAAaa","ok","1983198312893"};
public MainFragment(String[] s1){
for(int i = 0; i < likeTitles.length; i++)
s1[i] = likeTitles[i];
}
}


Related Topics



Leave a reply



Submit