简体   繁体   English

从活动传递数据以查看寻呼机片段

[英]Pass data from activity to view pager fragment

Sorry for broken english. 对不起,英语不好。

I created tab layout using retrofit, and there is no problem with tabs name or title. 我使用改造创建了选项卡布局,并且选项卡名称或标题没有问题。 But when i tried to passing data id to the fragment, there's a problem. 但是,当我尝试将数据ID传递给片段时,出现了问题。

In My Activity : 在我的活动中:

     public void onResponse(Response<CategoryResponse> response, Retrofit retrofit) {
            mProgressDialog.hide();
            FragmentKonten deskripsi;
            if(response.body().getStatus().equals("ok"))
            {
                CategoryResponse produkListResponse = response.body();
                for (Category category : produkListResponse.getListProduk())
                {
                    bundle.putInt("category",category.getId());

                    deskripsi=new FragmentKonten();
                    deskripsi.setArguments(bundle);

                    adapter.addFragment(deskripsi,category.getTitle());
                    adapter.notifyDataSetChanged();
                }
            }
            else{
                pesan(getActivity());
            }
        }

In my fragment : 在我的片段中:

 bundle = this.getArguments();

    Toast.makeText(getActivity(),String.valueOf(bundle.getInt("category")),Toast.LENGTH_LONG).show();

the results that i want are : 我想要的结果是:

Category Title and ID : 类别标题和ID:

Diet Sehat = 3

Khas Indonesia = 4

Resep Kue = 7

Resep Macanegara = 9

etc

But the actual results are : 但是实际结果是:

Diet Sehat = 9

Khas Indonesia = 9

Resep Kue = 9

Resep Macanegara = 9

etc

Have you tried: 你有没有尝试过:

for (Category category : produkListResponse.getListProduk())
                {
                    Bundle bundle = new Bundle(); //I guess you're missing this
                    bundle.putInt("category",category.getId());

                    deskripsi=new FragmentKonten();
                    deskripsi.setArguments(bundle);

                    adapter.addFragment(deskripsi,category.getTitle());
                    adapter.notifyDataSetChanged();
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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