简体   繁体   English

如何将数据从类传递到片段?

[英]How do I pass data from a class to a fragment?

I'm trying to create some fragments for an app and wanted to use one class to represent its data like names, levels, health, etc. 我正在尝试为应用创建一些片段,并希望使用一个类来表示其数据,例如名称,级别,运行状况等。

So inside my fragment I'm currently working on, I only know how to use the data declared in its own class file and applying it to a spinner. 因此,在我目前正在处理的片段中,我只知道如何使用在其自己的类文件中声明的数据并将其应用于微调器。 Below is the code for it and works perfectly how I want it to. 以下是它的代码,可以按照我的意愿完美地工作。 But I want to have a class file with just data variables so I can use globally between all my fragments and not repeat the code over and over again. 但是我希望有一个仅包含数据变量的类文件,以便可以在所有片段之间全局使用,而不必一遍又一遍地重复代码。 Below is my code inside of my fragment that I have now. 下面是片段中的代码。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_hunt, container, false);

        String [] names = {"Peter Pan", "Captain Hook", "Jack Sparrow"};

        Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, names);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner.setAdapter(adapter);

        return view;
    }

So really, does anyone know how would I pull the data from the below pure java class into this fragment so I can use them between many fragments? 真的,有人知道如何将下面的纯Java类中的数据提取到此片段中,以便可以在许多片段之间使用它们吗?

The class I have below is the one I created just to store my data variables. 我下面的类是我创建的仅用于存储数据变量的类。

public class TreasureHunter {

    public static String[] name = new String[] {
            "Peter Pan",
            "Captain Hook",
            "Jack Sparrow"
    };

    public static int[] picturePath = new int[] {
            R.drawable.peter,
            R.drawable.hook,
            R.drawable.jack
    };

    public static int[] health = new int[] {
            120,
            100,
            110

    };
}

Not sure if I understand you correctly but if you want to pull/save data from a static class you just call it as is : String[] names = TreasureHunter.name . 不知道我是否理解正确,但是如果您想从静态类中提取/保存数据,则可以直接调用它:String [] names = TreasureHunter.name。 The fragment has nothing to do with it. 该片段与它无关。
Edit 编辑
Additionally from a more OOP approach you could create a class "Character" with name, image, health, etc variables. 另外,通过一种更加面向对象的方法,您可以创建具有名称,图像,健康状况等变量的“字符”类。 Create a class with a static array of characters "roster". 创建一个带有字符“ roster”的静态数组的类。 Then build your characters: Character peterPan = new Character(name, img, health) then add to your roster array. 然后构建您的角色:Character peterPan = new Character(name,img,health)然后添加到您的名册数组。 That way you have an array of individual characters instead of arrays you need to keep in parrallel. 这样,您将拥有一个单独字符的数组,而不是需要保持平行的数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM