简体   繁体   English

如何将我的 ArrayList 与不同的 java class 分开?

[英]How can I separate my ArrayList to different java class?

Good day, this is my QuestionActivity.美好的一天,这是我的 QuestionActivity。 Class and I want ArrayList<BasicItem> BasicList to put this on different class and still use it in this activity. Class 并且我希望ArrayList<BasicItem> BasicList将其放在不同的 class 上,并且仍然在此活动中使用它。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question);


    final ArrayList<BasicItem> BasicList = new ArrayList<>();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();
    BasicList.add(new BasicItem();

My reason to separate this because, in the actual app, this activity become laggy and I'm gonna put 600 items.我之所以将其分开,是因为在实际应用程序中,此活动变得滞后,我将放置 600 个项目。 and I think separating it might help.我认为分开它可能会有所帮助。 Thanks in advance.提前致谢。

You just need to create a separate utility class that can be sharable for different activities/fragments), and you can utilize the singleton pattern for this to avoid multiple instantiations.您只需要创建一个单独的实用程序 class 可以为不同的活动/片段共享),并且您可以利用singleton 模式来避免多次实例化。

public class Utility {

    ArrayList<BasicItem> BasicList;
    private static Utility mUtility;

    private Utility() {
        BasicList = new ArrayList<>();
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
        BasicList.add(new BasicItem());
    }

    static Utility newInstance() {
        if (mUtility == null) {
            mUtility = new Utility();
        }
        return mUtility;
    }

}

And to use it in activity/fragment并在活动/片段中使用它

Utility utility = Utility.newInstance();
BasicItem item = utility.BasicList.get(0);

You can also consider using the.也可以考虑使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的对话框出了什么问题?如何更改不同类和单独的.java文件中的arraylist? - What's wrong with my Dialog box and how should I change arraylist from different class and on separate .java file? 如何从 Java 中的不同 class 访问 arraylist? - How can I access an an arraylist from a different class in Java? 如何在 Java 中将字符串数组转换为不同的类 ArrayList - How can I convert a String array to a different Class ArrayList in Java 如何将我的枚举类返回设置为 ArrayList Java - How can I set my enum class return into an ArrayList Java 如何将我的 Swing 组件分离到 Java 中的不同类? - How can I separate my swing components to different classes in java? 我如何让我的ArrayList中的不同对象执行每个类具有的方法(salaris()) - How can I let my different objects in my ArrayList perform the method each class has (salaris()) Java-如何扩展ArrayList <class> ? 我无法获得定义的吸气剂 - Java - How to extend the ArrayList<class> ? I can't get my defined getter 如何修改其他班级的arraylist? Java的 - How do I modify an arraylist from a different class? Java 如何在Java中使用单独的枚举类引用静态类? - How can I reference a static class with a separate enum class in Java? 我如何在Java中搜索类类型的数组列表 - How can i search an arraylist of a class type in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM