简体   繁体   中英

passing data from activity to tabs fragment

Not able to pass the retrived list of data from retrofit to a recyclerview in fragment! Tried some solutions but not working!

I have tried creating methods in fragment and passing but not working! Getting null!

Activity

    TabLayout tabLayout;
    ViewPager viewPager;
    RecyclerView recyclerView;
    //vars
    private Dto dto;
    List<Post_DTO> post_dto_list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        dto=new Dto();
        UpdateList();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        tabLayout=(TabLayout)findViewById(R.id.tabLayout);
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        tabLayout.addTab(tabLayout.newTab().setText("Dashboard"));
        tabLayout.addTab(tabLayout.newTab().setText("Converations"));
        tabLayout.addTab(tabLayout.newTab().setText("Profile"));
        PostFragment pf=new PostFragment();
        pf.passData(getApplicationContext(),post_dto_list);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        //ft.add(R.id.posts_recyclerview, ft);
        ft.addToBackStack(null);
        ft.commit();
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final MyAdapter adapter = new MyAdapter(this,getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                System.out.println(tab.getPosition());
                viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
    private void UpdateList() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getString(R.string.postUrl))
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        PostsInterface postsInterface =retrofit.create(PostsInterface.class);
        Call<Post_DTO_Resp> call= postsInterface.getPosts();
        call.enqueue(new Callback<Post_DTO_Resp>() {
            @Override
            public void onResponse(Call<Post_DTO_Resp> call, Response<Post_DTO_Resp> response) {
                //pDialog.dismiss();
                if(!response.isSuccessful())
                {
                    Toast.makeText(getApplicationContext(),"Loading the posts!",Toast.LENGTH_SHORT).show();
                }

                Post_DTO_Resp posts = response.body();
                post_dto_list=posts.getPost_dto_list();
                //dto.setList(post_dto_list);
            }

            @Override
            public void onFailure(Call<Post_DTO_Resp> call, Throwable t) {
                //pDialog.dismiss();
                if(t.getMessage().equals("End of input at line 1 column 1 path $"))
                {
                    Toast.makeText(getApplicationContext(), "There are no posts!", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Api Error!", Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}

Fragment!

public class PostFragment extends Fragment {
    RecyclerView recyclerView;
    View v;
    Dto dtobject;
    private static final String DESCRIBABLE_KEY = "list";
    private Describable mDescribable;
    List<Post_DTO> post_dto_list;
    public PostFragment() {
        // Required empty public constructor
    }
    public void passData(Context context, List<Post_DTO> list) {
        //mContext = context;
        post_dto_list = list;
        // mIndex = pos;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        v=inflater.inflate(R.layout.fragment_posts, container, false);
        recyclerView=(RecyclerView) v.findViewById(R.id.posts_recyclerview);
        RecyclerViewAdapter recyclerViewAdapter=new RecyclerViewAdapter(getContext(),post_dto_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(recyclerViewAdapter);
        //listupdater();
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Just want to get the post_dto_list from Activity to fragment class! Suggestions please!

You can use a bundle to pass a list from activity to fragment.

In activity:

Bundle bundle = new Bundle();
bundle.putSerializable("list", mList);

mFragment.setArguments(bundle);

And then in your fragment:

mList = getIntent().getExtras().getSerializable("list"));

But the POJO that your list contains should be parcelable ( Parcelable better than Serializable ).

Your POJO should be something like:

public class MyPOJO implements Parcelable {

For more info refer this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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