gpt4 book ai didi

java - ArrayList 每次返回 fragment 时将他的大小加倍

转载 作者:行者123 更新时间:2023-11-29 18:43:09 24 4
gpt4 key购买 nike

我是一名新的 Android 开发人员,我尝试创建一些旅游应用程序"

在我的应用程序中,我有 HomeFragment,它有 3 个按钮:“景点”“事件”“酒店”

public class HomeFragment extends Fragment implements View.OnClickListener {

private ArrayList<Category> mAttractions= new ArrayList<>();
private ArrayList<Category> mEvents= new ArrayList<>();
private ArrayList<Category> mHotels= new ArrayList<>();


private int mAttractionsBtnId = R.id.attractions_btn;
private int mEventsBtnId = R.id.events_btn;
private int mHotelsBtnId = R.id.hotels_btn;

public HomeFragment() {
// Required empty public constructor
}

public void initCategoriesData() {
// init attraction array list
mAttractions.add(new Category("Jaffa Port",5,R.drawable.attractions_jaffa_port));
mAttractions.add(new Category("Beit Hatfusot",5,R.drawable.attractions_beit_hatfutsot));

mAttractions.add(new Category("Independence Hall",5,R.drawable.attractions_independence_hall));

mAttractions.add(new Category("Jaffa Bazar",5,R.drawable.attractions_jaffa_bazar));

mAttractions.add(new Category("Neve Tzedek St",5,R.drawable.attractions_neve_tzedek));

mAttractions.add(new Category("Rothschild Boulevard St",5,R.drawable.attractions_rothschild_boulevard));


// init Events array list
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));

// init Hotels array list
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));




}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);

// Initiate data for each category
initCategoriesData();


// Find Buttons
Button attractionsBtn = (Button)view.findViewById(R.id.attractions_btn);
Button hotelsBtn = (Button)view.findViewById(R.id.hotels_btn);
Button eventsBtn = (Button)view.findViewById(R.id.events_btn);

// Set buttons OnClickListener
attractionsBtn.setOnClickListener(this);
hotelsBtn.setOnClickListener(this);
eventsBtn.setOnClickListener(this);


return view;
}


@Override
public void onClick(View v) {
// Start new Fragment when Button Clicked
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().addToBackStack(null);

Bundle bundle = new Bundle();


// Pass the right data in the bundle
switch (v.getId()){
case R.id.attractions_btn:
bundle.putSerializable("Data",mAttractions);
break;

case R.id.hotels_btn:
bundle.putSerializable("Data",mHotels);
break;

case R.id.events_btn:
bundle.putSerializable("Data",mEvents);
break;

}

ArrayList<Category> cat = (ArrayList<Category>)bundle.getSerializable("Data");
int size = cat.size();

MasterFragment masterFragment = new MasterFragment();
masterFragment.setArguments(bundle);

fragmentTransaction.replace(R.id.fragment_container, masterFragment);
fragmentTransaction.commit();
}

因为所有的内容都是一样的(只是类别名称不同)我决定使用 MasterFragment 以避免重复每个类别的代码。

所有“数据”都在 HomeFragment 中创建,每次用户单击其中一个按钮时,应用程序都会创建新的 MasterFragment 并将 Bundle 中的数据发送给他,以便让他知道他需要哪些数据以显示。

这是“MasterFragment”的样子:

public class MasterFragment extends Fragment {

private ArrayList<Category> mData;

public MasterFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle bundle) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
mData = (ArrayList<Category>)getArguments().getSerializable("Data");

// Create an {@link WordAdapter}, whose data source is a list of {@link Category}s. The
// adapter knows how to create list items for each item in the list.
CategoryAdapter adapter = new CategoryAdapter(getActivity(), mData, R.color.colorAccent);

// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) rootView.findViewById(R.id.list);

// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);


return rootView;
}

一切正常,当用户点击例如 AttractionButton 时,应用程序显示的内容很棒(只有设计不好,哈哈)但!!当我单击“后退按钮”并返回到 HomeFragment 时然后再次单击 AttractionButton 它会复制 Arraylist(从 6 项到 12 项等等……)

我通过在“initCategoriesData”方法中初始化 ArrayList 变量解决了这个问题

但我还是不明白为什么这个应用会像她那样做我很欣赏对此的一个很好的解释,它真的会帮助我更好地理解事情的运作方式

提前致谢,Niv :)

最佳答案

因为你的 fragment 没有被破坏,当你回来的时候,你的数组列表已经有 6 个项目(因为 fragment 没有被重新创建)。所以你也添加了 6 个项目,arraylist 大小变为 12。你可以做这样的事情来解决这个问题。

private ArrayList<Category> mAttractions;
private ArrayList<Category> mEvents;
private ArrayList<Category> mHotels;

public void initCategoriesData() {

//initialize array list here

mAttractions= new ArrayList<>()
mEvents= new ArrayList<>();
mHotels= new ArrayList<>();

// init attraction array list
mAttractions.add(new Category("Jaffa Port",5,R.drawable.attractions_jaffa_port));
mAttractions.add(new Category("Beit Hatfusot",5,R.drawable.attractions_beit_hatfutsot));

mAttractions.add(new Category("Independence Hall",5,R.drawable.attractions_independence_hall));

mAttractions.add(new Category("Jaffa Bazar",5,R.drawable.attractions_jaffa_bazar));

mAttractions.add(new Category("Neve Tzedek St",5,R.drawable.attractions_neve_tzedek));

mAttractions.add(new Category("Rothschild Boulevard St",5,R.drawable.attractions_rothschild_boulevard));


// init Events array list
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));
mEvents.add(new Category("cacdas",3));

// init Hotels array list
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));
mHotels.add(new Category("hotels",3));

希望对你有帮助。

关于java - ArrayList 每次返回 fragment 时将他的大小加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52605376/

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