Jak załadować tablice string z MainActivity we Fragment

0

Witajcie,

udało mi się przesłać do Fragmentu text i drawable z MainActivity. Jak przesłać string array do Fragmentu (w metodzie onCreateView) i wyświetlić dynamicznie w radioButtonach?

MainActivity

frags = new ArrayList<>();
        frags.add(new Fragment1("Text1", R.drawable.nature2, new String[]{"option 1", "option 2", "option 3"}));
        frags.add(new Fragment1("Text2",R.drawable.nature4, new String[]{"option 4", "option 5", "option 6"}));

Fragment

public class Fragment1 extends Fragment {

    public Fragment1() {
    }

    String stringValue;
    int imagesResId;
    TextView text;
    String[] rbData;
    View answer;

    public Fragment1(String str, int imageView , String[] rb) {

        this.stringValue = str;
        this.imagesResId = imageView;
        this.rbData = rb;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("x","onCreateViewFragment");

        View view = inflater.inflate(R.layout.fragment1, container, false);
        text =  view.findViewById(R.id.textView);
        ImageView imageResId = view.findViewById(image);
        answer =(RadioGroup) view.findViewById(R.id.radioGroup);

        text.setText(stringValue);
        imageResId.setImageResource(imagesResId);
1

fragment Communicating with Fragments oraz Fragment with Arguments
https://guides.codepath.com/android/Creating-and-Using-Fragments

do tego 2 libki ktore zaoszczedza Ci duzo czasu:
https://github.com/f2prateek/dart
https://github.com/johncarl81/parceler

0

Dziękuję Ci serdecznie. To zaawansowane github linki jak dla mnie początkującego :) Jednak na pewno kiedyś okażą się bardzo przydatne.
Tymczasem gdybyś mógł napisać ciut kodu od siebie/wyjaśnić co mam zrobić odnośnie tej tablicy string, to byłoby super.
Jeszcze raz serdeczne dzięki.

0

Nie zagłębiałem się dokładnie w ten kod i nie wiem czy o coś takiego Ci chodziło ale możesz spróbować

if (answer != null) {
        for (int i = 0; i < answer.getChildCount(); i++) {
            ((RadioButton) answer.getChildAt(i)).setText(rbData[i]);
        }
}
1
    private String stringValue;
    private int imagesResId;
    private String[] rbData;

    private Fragment1() {
    }

    public static Fragment1 newInstance(String str, int imageView , String[] rb) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString("yourString", str);
        args.putInt("yourImage", imageView);
        args.putStringArrayList("yourStringArray", rb);
        fragment.setArguments(args);
        return fragment;
    }

   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // Get back arguments
        stringValue = getArguments().getString("yourString", "");	
       imagesResId = getArguments().getInt("yourImage", 0);	
       rbData = getArguments().getStringArrayList("yourStringArray");
   }

sprawdz czy dziala. ogolnie parametry podaje sie przed bundle. wiecej pod tym linkiem
https://guides.codepath.com/android/Creating-and-Using-Fragments

0

tutaj masz jak dynamicznie dodawac widoki
https://stackoverflow.com/questions/19380526/how-to-add-radio-button-dynamically-as-per-the-given-number-of-counts

czyli robisz tzw. placeholder w xml'u np.

<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />

oraz:

1 użytkowników online, w tym zalogowanych: 0, gości: 1