将数据从片段传递到适配器的问题

时间:2017-12-20 09:16:56

标签: java android android-fragments

我有将数据从片段传递到适配器的问题。

1)我将数据从活动传递到片段

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Set the fragment initially
    UominiEDonne fragment = new UominiEDonne();
    FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    Intent intent = getIntent();
    if(intent!=null){
        nome = intent.getStringExtra("nome");
        email = intent.getStringExtra("email");
        uid= intent.getStringExtra("uid");

        UominiEDonne mainFragment = UominiEDonne.newInstance(uid);
    }
}

2)我在fragment

中获取数据
public static UominiEDonne newInstance(String param1) {
    UominiEDonne fragment = new UominiEDonne();
    Bundle args = new Bundle();
    args.putString("ARG_PARAM1", param1);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString("ARG_PARAM1");
        System.out.println("mParam1"+mParam1);

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


    View rootView = inflater.inflate(R.layout.fragment_main, container, false);


    db = new SQLiteHandler(getActivity());

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mSwipeRefreshLayout = (SwipeRefreshLayout)rootView. findViewById(R.id.swipe_container);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new GetVideos().execute();
        }
    });

    videoList = new ArrayList<>();
    videoList2 = new ArrayList<>();
    System.out.println("ID2:"+idd);

    mAdapter = new VideoAdapter(videoList,videoList2, this,this,mParam1);

    iv=(ImageView) rootView.findViewById(R.id.shareButton2);


    videoRecyclerView = (RecyclerView)rootView. findViewById(R.id.recycler_view);
    videoRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    videoRecyclerView.setHasFixedSize(true);

    videoRecyclerView.setAdapter(mAdapter);
    new GetVideos().execute(part1,part2);
    uiHelper = new UiLifecycleHelper(getActivity(), null);
    uiHelper.onCreate(savedInstanceState);

    // Inflate the layout for this fragment
    return rootView;
}

但如果我将idd传递给adapter构造函数,则onCreate idd为null ...我不知道为什么!

这是代码 FRAGMENT

public static UominiEDonne newInstance(String param1) {
    UominiEDonne fragment = new UominiEDonne();
    Bundle args = new Bundle();
    args.putString("ARG_PARAM1", param1);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString("ARG_PARAM1");
        System.out.println("mParam1"+mParam1);

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


    View rootView = inflater.inflate(R.layout.fragment_main, container, false);


    db = new SQLiteHandler(getActivity());

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mSwipeRefreshLayout = (SwipeRefreshLayout)rootView. findViewById(R.id.swipe_container);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new GetVideos().execute();
        }
    });

    videoList = new ArrayList<>();
    videoList2 = new ArrayList<>();
    System.out.println("ID2:"+idd);

    mAdapter = new VideoAdapter(videoList,videoList2, this,this,mParam1);

    iv=(ImageView) rootView.findViewById(R.id.shareButton2);


    videoRecyclerView = (RecyclerView)rootView. findViewById(R.id.recycler_view);
    videoRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    videoRecyclerView.setHasFixedSize(true);

    videoRecyclerView.setAdapter(mAdapter);
    new GetVideos().execute(part1,part2);
    uiHelper = new UiLifecycleHelper(getActivity(), null);
    uiHelper.onCreate(savedInstanceState);

    // Inflate the layout for this fragment
    return rootView;
}

1 个答案:

答案 0 :(得分:1)

你不能像这样将参数传递给片段。你需要使构造函数静态。检查以下代码;

public class BlankFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;



    public BlankFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

然后你需要用那个构造函数

创建片段
BlankFragment fragment = BlankFragment.newInstance("param1","param2");

基于更新问题的更新

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    Intent intent = getIntent();
    if(intent!=null){
        nome = intent.getStringExtra("nome");
        email = intent.getStringExtra("email");
        uid= intent.getStringExtra("uid");
        UominiEDonne mainFragment = UominiEDonne.newInstance(uid);
        FragmentTransaction fragmentTransaction =
        getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, mainFragment);
        fragmentTransaction.commit();
    }
}