gpt4 book ai didi

java - 如何将 'creating of a new instance of a fragment' 保存在变量中?

转载 作者:行者123 更新时间:2023-12-01 10:04:23 24 4
gpt4 key购买 nike

我有一个方法,它迭代一个数组。根据该数组的内容,它应该创建(不同) fragment 的新实例,并在它们上设置一些 TextView 。然而,我在确保我添加的 fragment 是该 fragment 的新实例方面遇到了一些问题。我尝试像这样存储“创建新 fragment ”:Fragment myFragment = new myFragment();

当数组只包含一个条目时,这是有效的,但是当要添加更多 fragment 时,它会抛出一个错误:java.lang.IllegalStateException:无法更改 fragment myFragment 的标记,基本上告诉我我不是创建新实例,而是使用同一个实例。

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

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

那么,如何将 fragment 的新实例的创建存储在变量中(下面示例中的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,其中包含 SQLite 表中名为 arg1 的列 arg2 的所有条目>.

最佳答案

所有积分均转到 George Mulligan .

他的建议是将新 fragment 的创建移到迭代数组的循环内,这就像一个魅力。

此外,作为补充,他建议研究一下 RecyclerView 来了解我想要做的事情(即在列表形状中添加同一事物的多个实例)。

关于java - 如何将 'creating of a new instance of a fragment' 保存在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36557778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com