如何将数据从活动发送/传递到TabLayout / ViewPager片段

时间:2016-07-30 22:42:55

标签: android android-activity android-viewpager android-fragmentactivity android-tablayout

我有问题将数据传递给TabLayout / ViewPager包含Fragment,

我试图发送使用捆绑buat dosen't工作,我试图从goole serching我没有找到任何与我的问题相同的答案,另一个arround谷歌只是将数据传递到片段在活动中没有TabLayout / ViewPager和那里所有是静态标签。

在我的案例中,我尝试使用日历制作Recyleview和Filtering,所以我把所有的一天都放在TabLayout上,就像这个截图

屏幕截图用户界面,暗区是查看寻呼机,底部的红色标签是TabLayout,包括与所选月份相同的标签

img

因此,如果你引用我使用Bundle或Fragmment Communication,请告诉我在哪里必须放置我的脚本。请看下面我的剧本

我删除了bundel脚本

来自Tab布局的此活动父级,您可以将其称为主活动

    public class daftar_jadwal extends FragmentActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    Button pilihBulan_btn, pilihTahun_btn;
    Bundle data;
    String[] thn_array;
    public String tahun, bulan_fix, tgl_fix;


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

        pilihBulan_btn = (Button) findViewById(R.id.btnPilihBulan);
        pilihTahun_btn = (Button) findViewById(R.id.btnPilihtahun);

        pilihBulan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pilihBulan();
            }
        });

        pilihTahun_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadTahun();
            }
        });

        tabLayout = (TabLayout) findViewById(R.id.jadwal_menu);
        viewPager = (ViewPager) findViewById(R.id.viewPageJadwal);


//        int tahunini = Calendar.getInstance().get(Calendar.YEAR);
//        for (int i = 2000; i <= tahunini; i++ ){
//            tahun_fix.add(Integer.toString(i));
//        }



        loadTahun();

    }




    public void pilihBulan(){
        new MaterialDialog.Builder(this)
                .title("Pilih Bulan")
                .items(R.array.list_bulan)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                        switch (which){
                            case 0:
                                setContentView(which, text);
                                break;
                            case 1:
                                setContentView(which, text);
                                break;
                            case 2:
                                setContentView(which, text);
                                break;
                            case 3:
                                setContentView(which, text);
                                break;
                            case 4:
                                setContentView(which, text);
                                break;
                            case 5:
                                setContentView(which, text);
                                break;
                            case 6:
                                setContentView(which, text);
                                break;
                            case 7:
                                setContentView(which, text);
                                break;
                            case 8:
                                setContentView(which, text);
                                break;
                            case 9:
                                setContentView(which, text);
                                break;
                            case 10:
                                setContentView(which, text);
                                break;
                            case 11:
                                setContentView(which, text);
                                break;

                        }
                    }
                })
                .show();
    }



    public void setView(String thnFix, String blnFix){
        SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        String sdate1 = blnFix+"-1-"+thnFix+" 00:00:00";
        try {
            Date date = df.parse(sdate1);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int days = calendar.getActualMaximum(calendar.DAY_OF_MONTH);



            viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            for (int i =0; i < days; i++){
                viewPagerAdapter.addFragment(new listJadwalFragment(), String.valueOf(i+1));
               }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tgl_fix = (String) tab.getText();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }



    public void loadTahun() {
        String urlTahun = "http://"+ parser.IP_PUBLIC+"/ditlantas/json/jadwal/view_tahun.php";
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                urlTahun, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONObject jPesan = response.getJSONObject("data");
                    JSONArray jsonArray = jPesan.getJSONArray("listtahun");
                    List<String> temp = new ArrayList<String>();
                    for (int i = 0; i < jsonArray.length(); i++){
                        JSONObject listGiat = jsonArray.getJSONObject(i);
                        temp.add(listGiat.getString("tahun"));
                    }
                    thn_array = temp.toArray(new String[0]);
                    pilihTahun(thn_array);

                    Log.d("isitahun", Arrays.toString(thn_array));

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
             Toast.makeText(getApplicationContext(),
                        "Terjadi Kesalahan Jarigan", Toast.LENGTH_SHORT).show();


            }
        });

        // Adding request to request queue
        MyApplication.getInstance().addToReqQueue(jsonObjReq);

    }


    public void pilihTahun(final String[] tahun_met){
        new MaterialDialog.Builder(daftar_jadwal.this)
                .title("Pilih Tahun")
                .items(tahun_met)
                .itemsCallback(new MaterialDialog.ListCallback() {
                    @Override
                    public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                        int banyak_tahun = tahun_met.length;
                        for (int i = 0; i < banyak_tahun; i++){
                            if (i == which){
                                tahun = tahun_met[i];
                                pilihTahun_btn.setText(tahun);
                                pilihBulan();
                            }

                        }
                    }
                })

                .show();
    }


    public void setContentView(int i, CharSequence text){
        bulan_fix = String.valueOf(i+1);
        pilihBulan_btn.setText(text);
        setView(tahun, bulan_fix);
    }

}

ViewPagerAdapter

public class ViewPagerAdapter extends FragmentPagerAdapter {

    ArrayList<Fragment> fragments = new ArrayList<>();
    ArrayList<String> tabTitles = new ArrayList<>();


    public void addFragment(Fragment fragment, String titles){
        this.fragments.add(fragment);
        this.tabTitles.add(titles);
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }


    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }
}

这里是碎片

public class listJadwalFragment extends Fragment {
    public TextView text;
    daftar_jadwal daftarJadwal;

    private List<ListItemJadwal> listItemJadwal = new ArrayList<ListItemJadwal>();
    RecyclerView recyclerView;
    JadwalRecyclerAdapter adapter;
    private ProgressDialog PD;

    String url;


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


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



    }

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


//      url = "http://"+ parser.IP_PUBLIC +"/ditlantas/json/jadwal/view.php?thn="+params_thn+"&bln="+ params_bln+ "&tgl="+params_tgl;
        url = "http://"+ parser.IP_PUBLIC +"/ditlantas/json/jadwal/view.php?thn=2016&bln=06&tgl=29";

        recyclerView = (RecyclerView) rootView.findViewById(R.id.rec_jadwal);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getContext()).build());

        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new JadwalRecyclerAdapter(getActivity(), listItemJadwal);
        recyclerView.setAdapter(adapter);
        adapter.clearAdapter();


        PD = new ProgressDialog(getContext());
        PD.setMessage("Loading.....");
        PD.setCancelable(false);

        updateList();

        return rootView;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);




    }

    private void updateList() {

        PD.show();




        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                PD.dismiss();
                try {
                    JSONObject jPesan = response.getJSONObject("data");
                    JSONArray jsonArray = jPesan.getJSONArray("kegiatan");

                    for (int i = 0; i < jsonArray.length(); i++){
                        JSONObject listJadwal = jsonArray.getJSONObject(i);
                        ListItemJadwal item = new ListItemJadwal();

                        item.setNamaKegiatan(listJadwal.getString("namaKegiatan"));
                        item.setTglMulai(listJadwal.getString("tglMulai"));
                        item.setNamaLokasi(listJadwal.getString("namaLokasi"));
                        item.setJamMulai(listJadwal.getString("jamMulai"));

                        listItemJadwal.add(item);
                    }



                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getActivity(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                PD.dismiss();
                Toast.makeText(getActivity(),
                        "Terjadi Kesalahan Jarigan", Toast.LENGTH_SHORT).show();


            }
        });

        // Adding request to request queue
        MyApplication.getInstance().addToReqQueue(jsonObjReq);

    }


}

0 个答案:

没有答案