如何保存'创建片段的新实例'在一个变量?

时间:2016-04-11 19:39:23

标签: java android android-fragments instantiation

我有一个迭代数组的方法。根据该数组的内容,它应该创建(不同)片段的新实例,并在它们上设置一些文本视图。但是我在确定我添加的片段是该片段的新实例时遇到了一些问题。我试着存储"创建新片段"像这样:Fragment myFragment = new myFragment();

当数组只包含一个条目时,这是有效的,但是当要添加更多片段时,它会抛出一个错误:java.lang.IllegalStateException: Can't change tag of fragment myFragment,基本上告诉我我没有创建一个新实例,但使用相同的实例。

我还尝试使用基本Fragment myFragment = myFragment.newInstance();方法的newInstance(),这给了我完全相同的错误。

public static myFragment newInstance() {
    return new myFragment();
}

那么,我如何在一个变量(下面的例子中为fragmentVariable)中存储片段的新实例的创建,它将像这样使用:

FragmentManager fm = getFragmentManager();
    fm.beginTransaction().add(R.id.container, fragmentVariable, "tag").
}

PS。我怀疑我是以一种糟糕而愚蠢的方式做到这一点,但我只是在享受一些乐趣,为我自己创造一个应用程序。

DrawVariableFragments方法

public void DrawVariableFragments(String base,String token){

    ArrayList<String> Data;
    dbHandler = DBHandler.getInstance(this);

    int AmountOfEntries;
    int SettingsContainer;
    String SettingsTag;
    Fragment SettingsVariableFragment;
    Fragment SettingsEmptyFragment;

    if (base.equalsIgnoreCase("StuffManager")) {
        Log.d(tag, "StuffManager");
        Data = new ArrayList<String>() {{add("StuffManager"); add("name"); add("tag"); }};
        SettingsContainer = R.id.FragmentContainer2;
        SettingsTag = getString(R.string.navdrawer_stuffmanager);
        SettingsVariableFragment = new StuffManagerVariableFragment();
        SettingsEmptyFragment = new StuffManagerEmptyFragment();
    } else if (base.equalsIgnoreCase("GeneralSettings")) {
        Log.d(tag, "GeneralSettings");
        Data = new ArrayList<String>() {{add("GeneralSettings"); add("name"); add("ip"); add("port"); add("username"); add("pass"); }};
        SettingsContainer = R.id.FragmentContainerGeneralSettings;
        SettingsTag = getString(R.string.navdrawer_generalsettings);
        SettingsVariableFragment = new GeneralSettingsVariableFragment();
        SettingsEmptyFragment = new GeneralSettingsEmptyFragment();
    } else {
        Log.e(tag, "String Base not recognised");
        return;
    }
    AmountOfEntries = dbHandler.returnArray(base, Data.get(1)).size();
    FragmentManager fm = getFragmentManager().findFragmentByTag(SettingsTag).getChildFragmentManager();

    // ****
    // If no devices/links are set up=
    // ****

    if (AmountOfEntries == 0 ) {
        Log.d(tag, "AmountOfEntries == 0");
        fm.beginTransaction().add(SettingsContainer, SettingsEmptyFragment, (base + "EmptyFragment")).commit();
        fm.executePendingTransactions();
        return;
    }

    // ****
    // If more then 0 devices/links are set up
    // ****

    if (AmountOfEntries > 0) {
        Log.d(tag, "AmountOfEntries > 0");

        // ****
        // Remove 'no devices created' fragment if available
        // ****
        String EmptyFragName = (base + "EmptyFragment");
        if ((fm.findFragmentByTag(EmptyFragName)) != null) {
            Log.d(tag, "EmptyFragment removed");
            fm.beginTransaction().remove(fm.findFragmentByTag(EmptyFragName)).commit();
            fm.executePendingTransactions();
        }

        // ****
        //
        // ****

        for (int iEntry = 0; iEntry < AmountOfEntries; iEntry++) {     // For each entry
            ArrayList<String> fragmentData = new ArrayList<>();         // Create new Array for every entry

            // ****
            // Create and fill FragmentData array
            // ****

            Log.d(tag, "Data array size = " + Data.size());
            for (int i = 0; i < Data.size(); i++) {
                if (i != 0) {
                    fragmentData.add((dbHandler.returnArray(base, Data.get(i))).get(iEntry));
                }
            }

//            for (String s : fragmentData) {
//                Log.d(" FD " + tag, "fragmentData " + s);
//            }

            // ****
            // Do based on token
            // ****

            if (token.equalsIgnoreCase("edit")) {
                ((LinearLayout) findViewById(SettingsContainer)).removeAllViews();
                DrawVariableFragments(base ,"draw");
            } else if (token.equalsIgnoreCase("add")) {
                if (fm.findFragmentByTag(fragmentData.get(0)) == null) {
                    fm.beginTransaction().add(SettingsContainer, SettingsVariableFragment, fragmentData.get(0)).commit();
                    fm.executePendingTransactions();
                    if (base.equalsIgnoreCase("StuffManager")) {
                        ((StuffManagerVariableFragment) fm.findFragmentByTag(fragmentData.get(0)))
                                .setText(fragmentData.get(0), fragmentData.get(1));
                    } else if (base.equalsIgnoreCase("GeneralSettings")) {
                        ((GeneralSettingsVariableFragment) fm.findFragmentByTag(fragmentData.get(0)))
                                .setText(fragmentData.get(0), fragmentData.get(1), fragmentData.get(2), fragmentData.get(3));
                    }
                }
            } else if (token.equalsIgnoreCase("draw")) {
                fm.beginTransaction().add(SettingsContainer, SettingsVariableFragment, fragmentData.get(0)).commit();
                fm.executePendingTransactions();
                if (base.equalsIgnoreCase("StuffManager")) {
                    ((StuffManagerVariableFragment) fm.findFragmentByTag(fragmentData.get(0)))
                            .setText(fragmentData.get(0), fragmentData.get(1));
                } else if (base.equalsIgnoreCase("GeneralSettings")) {
                    ((GeneralSettingsVariableFragment) fm.findFragmentByTag(fragmentData.get(0)))
                            .setText(fragmentData.get(0), fragmentData.get(1), fragmentData.get(2), fragmentData.get(3));
                }
            }
        }
    } else {
        Log.d("TEST", "WTF, nameArray.size != 0 && !> 0");
    }
}

dbHandler.returnArray(arg1, arg2)返回一个ArrayList,其中包含名为arg2的SQLite表中列arg1的所有条目。

1 个答案:

答案 0 :(得分:1)

所有积分均转至George Mulligan

他的建议是在循环中移动新片段的创建,循环遍历数组,这就像一个魅力。

此外,作为补充,他建议调查RecyclerView我正在尝试做什么(这是在列表形状中添加同一事物的多个实例)。