1

Im Trying to dynamically add buttons from items in a String[]. My approach is to programmatically add as many buttons as items in the array, and assign each button a text from that array. (I understand how to add buttons programmatically from the items).

My problem is not being able to pass the String[] buttonStrings from class TestOne to the fragment FragmentTest.

The log returns null for String[] buttonStrings, however it returns the correct value for String title.

My guess is I'm not initializing String[] buttonStrings somewhere. Or maybe my approach is incorrect.

This is my TestOne Class

public class TestOne extends ActualFragmetIntro {

 @Override
 public void init(Bundle savedInstanceState) {
    addSlide(FragmentTest.newInstance("This is the string from TestOne", new String[]{"Button1 Text, Button2 Text ,Button3 text , Button4 Text"}));

Below is my FragmentTest fragment

public class FragmentTest extends Fragment { 
 private static final String ARG_TITLE = "title";
 private static final String[] ARG_BUTTON_TEXTS_STRINGS = {"buttons_texts"};
 //private static final String ARG_BUTTON_TEXTS_STRINGS = {"buttons_texts"};
 //private static final ArrayList<String> ARG_BUTTON_TEXTS_STRINGS = new ArrayList<String>();

public static FragmentTest newInstance(String title, String[] buttonStrings) {
  FragmentTest sampleSlide = new FragmentTest();
  Bundle args = new Bundle();
  args.putString(ARG_TITLE, title);
  args.putStringArrayList(buttonStrings.toString(), ARG_BUTTON_TEXTS_STRINGS);
  sampleSlide.setArguments(args);

  return sampleSlide;
}

public static FragmentTest newInstance(String title, String[] buttonStrings) {
  FragmentTest sampleSlide = new FragmentTest();
  Bundle args = new Bundle();
  args.putString(ARG_TITLE, title);
  args.putStringArrayList(buttonStrings.toString(), ARG_BUTTON_TEXTS_STRINGS);
  sampleSlide.setArguments(args);

  return sampleSlide;
}

private String[] getArgButtonTextsStrings = new String[0];

public FragmentTest() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
  ....
if (getArguments() != null && getArguments().size() != 0) {
  title = getArguments().getString(ARG_TITLE);
  getArgButtonTextsStrings = getArguments().getStringArray(String.format(String.valueOf(ARG_BUTTON_TEXTS_STRINGS)));

  System.out.println("logged onCreate " + title +"  "+ getArgButtonTextsStrings );
 }
}
...

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
 super.setUserVisibleHint(isVisibleToUser);
  if(isVisibleToUser&& isResumed()){
  title = getArguments().getString(ARG_TITLE);
  getArgButtonTextsStrings = getArguments().getStringArray(String.format(String.valueOf(ARG_BUTTON_TEXTS_STRINGS)));

}else {

}
}

EDIT Added addSlide code

public abstract class ActualFragmetIntro extends FragmentActivity implements SceneIntroFragment4Objects.ClickCounterListener, TextToSpeech.OnInitListener {
private PagerIntroAdapter mPagerAdapter;
private ViewPager pager;
private List<Fragment> fragments = new Vector<>();
...

public void addSlide(@NonNull Fragment fragment) {
    fragments.add(fragment);
    mPagerAdapter.notifyDataSetChanged();
}

1 Answer 1

1

You set the data using tag: buttonStrings.toString().
But you are trying to get the data using: String.format(String.valueOf(ARG_BUTTON_TEXTS_STRINGS)).

Those tags are different and that's why you are getting a null array.

You should define the tag as a constant:

private static final String ARG_BUTTONS_TEXT = "buttons";

Then in your newInstance:

public static FragmentTest newInstance(String title, String[] buttonStrings) {
    FragmentTest sampleSlide = new FragmentTest();
    Bundle args = new Bundle();
    args.putString(ARG_TITLE, title);
    args.putStringArray(ARG_BUTTONS_TEXT, buttonStrings);
    sampleSlide.setArguments(args);

    return sampleSlide;
}

And in your onCreate:

getArgButtonTextsStrings = getArguments().getStringArray(ARG_BUTTONS_TEXT);
Sign up to request clarification or add additional context in comments.

4 Comments

Hey thanks for the reply. If I do args.putStringArrayList(ARG_BUTTONS_TEXT, buttonStrings); I get an error on buttonStrings, saying second parameter is wrong, it found java.lang.String[] and it requires java.util.ArrayList<java.lang.String>. So I changed the args.putStringArrayList to args.putStringArray, but that still returns a null value. Should I do new ArrayList<String>(Arrays.asList("one, two ,three , four")) instead of new String[]{"one, two ,three , four"} in TestOne` class
Could you post the code in addSlide ? Probably you are not using the same Fragment instance
Sorry for delayed response. I added the addslide code to my question
Ahh, your answer was correct. I was making two silly errors. I changed this private String[] getArgButtonTextsStrings = new String[0]; to private String[] getArgButtonTextsStrings; and for log System.out.println("onCreate " +" "+ Arrays.toString(getArgButtonTextsStrings)); Thank you very much for your help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.