在PlaceholderFragment中收集数据并将其发送到Activity [英] collect data in PlaceholderFragment and send it to Activity

查看:100
本文介绍了在PlaceholderFragment中收集数据并将其发送到Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个游戏,每个玩家都有自己的标签.每个选项卡都是PlaceholderFragment中的一个实例.我以为实现选项卡和片段的动态计数会很容易,但是现在我真的不知道如何将每个实例的输入数据获取到ParentActivity. 也许我应该提到我想使用Java 7(SDK版本min< 24)

I'm writing a game where every player has his own tab. Every Tab is an instance from PlaceholderFragment. I thought it would be easy to implement the dynamic count of tabs and fragments but now I don't really know how to get the input data from each instance to the ParentActivity. Maybe I should mention that I want to work with java 7 (SDK version min < 24)

我使用的接口是正确的,但是问题是,用户需要在Activity中按下菜单项保存数据"来保存数据,但是接口是在片段中定义的.

I'm using the interface right know, but the problem is, that the user needs to press the menu item "save data" in the Activity to save the data, but the interface is defined in the fragment.

对我来说重要的是,我首先从每个片段中收集数据,然后再将其保存,因为之后我要将给定的数据列表转换为字符串并将此字符串保存在数据库中(否则,我不会保存整个游戏,但是只是一个球员).

It is important to me that I firstly collect the data from each fragment before saving it becuase afterwards I want to transform the given data lists to a string and save this string in the database (otherwise I would not save a whole game but just a player).

我知道我可以在数据库中定义更多的列,但是我有一个动态的玩家数和每个玩家的回合数.

I know that i could define more columns in the database but i have a dynamic number of player and rounds per player.

将数组转换为字符串:

private void mergeAll(ArrayList<ArrayList<Integer>> list) {
        ArrayList<String> wholeList = new ArrayList<>();
        for(ArrayList<Integer> partList : list) {
            for(int i = 0; i < partList.size(); i++) {
                wholeList.add(partList.get(i).toString());
            }
        }
        StringBuilder sb = new StringBuilder();
        for(String s : wholeList) {
            sb.append(s);
            sb.append("\t");
        }
        playerAsString = sb.toString();
    }

片段接口

public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("player", index);
        fragment.setArguments(bundle);
        return fragment;
    }

    public interface FragmentToActivity {
        void communicate(String data);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mCallback = (FragmentToActivity) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement FragmentToActivity");
        }
    }

    @Override
    public void onDetach() {
        mCallback = null;
        super.onDetach();
    }

    private void sendData(String comm)
    {
        mCallback.communicate(comm);
    }

活动:


@Override
    public void communicate(String data) {
        Log.i("got it", data);
    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.action_saveData:
                //here i want to collect the strings from each fragment and save it to the database
                return true;

            ...

        }
        return super.onOptionsItemSelected(item);
    }

还有其他方法吗? 据我所知,PlaceholderFragments没有片段标签或ID,所以我可以循环获取所需的String.

Is there any other way? As far as I know PlaceholderFragments doesn't have fragment tags or ids so I just can loop and get the String i need.

推荐答案

找到了一种方法:

@Override
    public void communicate(ArrayList<ArrayList<Integer>> data) {
        for(int i = 0; i < numPlayer; i++) {
            if(viewPager.getCurrentItem() == i) {
                oneGame.set(i, data);
            }
        }
    }

该活动中的此方法从每个片段中收集数据并将其存储在ArrayList中,然后再使用此ArrayList.

This method in the activity collects from every fragment the data und stores it in an ArrayList, afterwards I work with this ArrayList.

这篇关于在PlaceholderFragment中收集数据并将其发送到Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆