在PlaceholderFragment中收集数据并将其发送到Activity

编程入门 行业动态 更新时间:2024-10-28 14:22:46
本文介绍了在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) { mCallbackmunicate(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

本文发布于:2023-07-13 22:20:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1102477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   数据   并将其   PlaceholderFragment   Activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!