0

I'm creating an android app in android studio and I need to pass a string array from one class to another. It doesn't seem to work thought, and I can't find what's actually wrong. I tried to do it with the constructor.

I will omit unnecessary code.

class MyFragment extends Fragment{


   //I create a string array to pass on the other class

    public String likeTitles[] ={"mazda","whynot","OHYEAAAaa","ok","1983198312893"};


    public MainFragment(String s1[]){

            s1= likeTitles;
        }
}


public class List extends ListActivity{

    String myStringArr[]=new String[5];

    MainFragment myFrag = new MainFragment(myStringArr);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, myStingArr);
            getListView().setAdapter(adapter);
    }

I tried to use a string array from withing the class and it worked fine with my adapter, but this doesn't work.

Tasos Anesiadis
  • 1,134
  • 1
  • 12
  • 37
  • That's not how you pass data between Fragments and Activities. Just do a simple search here for "pass data between activities" and you'll find everything you need to know. Flagging as dupe of http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments – NoChinDeluxe Dec 10 '15 at 17:11
  • Possible duplicate of [How to pass data between fragments](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) – NoChinDeluxe Dec 10 '15 at 17:12

3 Answers3

0

You should use a static initializer to pass data to a Fragment:

class MyFragment extends Fragment {
    public static MyFragment newInstance(String[] array) {
        MyFragment fragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArray("yourArray", array);
        fragment.setArguments(bundle);
        return fragment;
    }
}

And here is how you should call it from your Activity:

public class List extends ListActivity {
    ... onCreate(...) {
        String myStringArr[]=new String[5];

        MyFragment myFrag = MyFragment.newInstance(myStringArr);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, myStingArr);
        getListView().setAdapter(adapter);
    }
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

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];
    }
}
Christian
  • 7,062
  • 9
  • 53
  • 79
  • This acually worked. I had no idea about that, thanks. – Tasos Anesiadis Dec 11 '15 at 13:16
  • It's very important to understand what you're doing (LOL)! Try to think String[] is a Bus and every position [i] is a seat in the Bus. What the function does is copy each person at seats from "likeTitles bus" to "s1 bus", got it? In your question, you're trying to copy "the bus" itself, and JAVA does not allow that, because the "s1" var is passed by value, not but reference. See this question: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Christian Dec 14 '15 at 11:43
0

create one class with static String array variable

public class storeData{        
    public static String stringArray[];
}

then access it from anther class to set or get

String likeTitles[] ={"mazda","whynot","OHYEAAAaa","ok","1983198312893"};

storeData.stringArray = likeTitles; // to set array

likeTitles = storeData.stringArray // to get array
Rohit Patil
  • 1,068
  • 8
  • 9