maysi
maysi

Reputation: 5507

What to do in the Fragment onCreateView Method?

What I have are 3 Layouts. fragment_tag, fragment_stunde and fragment_fach

fach should be in stunde and stunde in tag.

And in fach are some TextViews I want to set the text.

So my code so far is:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);

        database = new Database(context);

        database.open();

        for (int j = 1; j < 12; j++) {
            LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
            String[][] vplanDaten = database.getVplan(Integer.valueOf(DateHelper.get(DateHelper.WEEK_OF_YEAR)), index + 1, j);

            for (int k = 0; k < vplanDaten.length; k++) {
                LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);

                TextView fach_lehrer = (TextView) fach.findViewById(R.id.textView_lehrer);
                TextView fach_fach = (TextView) fach.findViewById(R.id.textView_fach);
                TextView fach_raum = (TextView) fach.findViewById(R.id.textView_raum);

                fach_lehrer.setText(vplanDaten[k][0]);
                fach_fach.setText(vplanDaten[k][1]);
                fach_raum.setText(vplanDaten[k][2]);
            }
        }
        database.close();
        return tag;
    }

But this gives me an Error:

 java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
        at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)

So how do I have to change the code that that what I want to do happens?

Upvotes: 0

Views: 523

Answers (2)

Vikram
Vikram

Reputation: 51581

Try making the following changes:

LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);

to:

LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);

Similarly:

LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);

to:

LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);

And:

LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);

to:

LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);

Upvotes: 1

TronicZomB
TronicZomB

Reputation: 8747

Change the LinearLayout to a View like such:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View tag = inflater.inflate(R.layout.fragment_tag, container, true);
       //Everything else...
    return tag;
}

Also I believe you can only inflate one view per onCreateView otherwise the layouts will replace one another. You need to have three separate fragments that each inflate its own view and call these fragments individually from the top parent activity.

Upvotes: 0

Related Questions