传递ArrayList <hashmap <string,string =“”>&gt;到片段</hashmap <string,>

时间:2012-08-09 13:50:24

标签: android android-fragments

我有3个ArrayList&gt;我想传递给3个片段。除了使它们静止之外,最好的方法是什么?

2 个答案:

答案 0 :(得分:3)

您可以在片段中使用setArguments。看看http://developer.android.com/guide/components/fragments.html,基本上,在创建Fragment之前创建一个Bundle,然后设置为Argument。

Android文档中的示例:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

您可以创建捆绑并设置参数

,而不是使用getIntent()。getExtras()。
Bundle bundle = new Bundle();
bundle.putSerializable(YOUR_KEY, yourObject);
fragment.setArguments(bundle);

对于你的碎片:

public static class DetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}

答案 1 :(得分:1)

您可以创建侦听器回调接口并在片段中实现它们。像这样:

@Override
public void onSomeEvent(List<SomeData> data) {
    //do something with data
}

在您的活动中创建此界面:

public interface OnSomeEventListener {
    onSomeEvent(List<SomeData> data);
}

然后使用findFragmentById或findFragmentByTag获取你的片段并将其分配给一个监听器:

this.onSomeEventListener = fragment; 

然后,您可以调用该接口的方法,并且您的片段将接收回调。

片段和活动之间第二种更简单的通信方式是BroadcastReceivers。您可以在片段中注册一些BroadcastReceiver,然后从活动中调用sendBroadcast()。您的数据列表可以放在一组广播消息中。