user1446988
user1446988

Reputation: 333

Programmatically adding buttons from String[]

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();
}

Upvotes: 1

Views: 113

Answers (1)

Juan Cruz Soler
Juan Cruz Soler

Reputation: 8254

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);

Upvotes: 1

Related Questions