Several Url in a class

1

My app deals with football, with which it consists of 30 days, they are all the same, just change the URL that is a Json each, and that's where I take the data, I mean, do as in Layouts , with one 30 Fragments work.
What I would like to know is if it is possible with a Fragment , go through these URLs to not have 30 what I have.

Frag_J_01, Frag_J_02 ......... Frag_J_29, Frag_J_30.

    public class Frag_J_01 extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout swipeLayout;

    private List<Estadisticas> jornada;

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;

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

        setRetainInstance(true);

        View view = inflater.inflate(R.layout.activity_main1, null);
        //Initializing Views
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        TextView miTexto = (TextView)view.findViewById(R.id.mi_java);
        miTexto.setText("JORNADA 1");

        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        jornada = new ArrayList<>();
        getData();

        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DecoracionLineaDivisoria(getActivity()));
        return view;
    }

    @Override
    public void onRefresh() {

        if (swipeLayout!=null) {
            swipeLayout.setRefreshing(false);
            swipeLayout.destroyDrawingCache();
            swipeLayout.clearAnimation();
        }
    }

    private void getData(){
        final ProgressDialog loading = ProgressDialog.show(getActivity(),"Cargando datos", "Por favor espere...",false,false);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_JORNADA01,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        loading.dismiss();
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(jsonArrayRequest);
    }

    private void parseData(JSONArray array){
        for(int i = 0; i<array.length(); i++) {
            Estadisticas superHero = new Estadisticas();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                superHero.setFecha(json.getString(Config.TAG_FECHA));
                superHero.setHora(json.getString(Config.TAG_HORA));
                superHero.setEquipo_Local(json.getString(Config.TAG_EQUIPO_LOCAL));
                superHero.setEquipo_Visitante(json.getString(Config.TAG_EQUIPO_VISITANTE));
                superHero.setResultado_Local(json.getString(Config.TAG_RESULTADO_LOCAL));
                superHero.setEstado_Partido(json.getString(Config.TAG_ESTADO));
                superHero.setResultado_Visitante(json.getString(Config.TAG_RESULTADO_VISITANTE));
                superHero.setEscudo_Local("http://ffcv.es/ncompeticiones/" + (json.getString(Config.TAG_ESCUDO_LOCAL)));
                superHero.setEscudo_Visitante("http://ffcv.es/ncompeticiones/" + (json.getString(Config.TAG_ESCUDO_VISITANTE)));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            jornada.add(superHero);
        }
        adapter = new Jornadas_Adapter(jornada, getActivity());
        recyclerView.setAdapter(adapter);
    }
}

Would it be possible?

    
asked by Rafel C.F 07.12.2016 в 12:58
source

1 answer

0

It's possible! Within your fragment add a static method which will serve to instantiate your Fragment, doing this is a good practice when instantiating Fragments with arguments:

        public class MyFragment extends Fragment {

            private String urlJson;

            public static MyFragment newInstance(String urlJson) {
                MyFragment f = new MyFragment();
                Bundle args = new Bundle();
                args.putString("urljson", urlJson );
                f.setArguments(args);
                return f;
            }


    private void leeBundle(Bundle bundle) {
            if (bundle != null) {
                urlJson = bundle.getString("urljson");

            }
        }


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

By adding this method to your Fragment now when you instantiate the Fragment, you would add the url of the .json file:

Fragment fragment = MyFragment.newInstance( archivoJson);

With this you only need a Fragment which can use different url. When using Volley you can use that url to get the data inside the fragment:

JsonArrayRequest(java.lang.String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) 

You can even only get values but not save them in a Bundle:

        public class MyFragment extends Fragment {

            private String urlJson;

            public static MyFragment newInstance(String urlJson) {
                this.urlJson = urJson;
                return new MyFragment();
            }               

        }
    
answered by 07.12.2016 / 14:06
source