Variables - Parameters

0

I can not erase the data when I click on a button, I explain, it perfectly opens the string "data", and also the "J1" and "J2", when I open the fragment for the first time, it shows me the url + data, correct, but when I press a button, it does not erase the data that it is showing me, and it adds to the end the data of "J1", how do I do it so that the previous ones are deleted and I show the ones of the button?

public class FragJuv01 extends Fragment {

    Button j1, j2;

    String url = "https://rafelcf.000webhostapp.com//2018/";
    String dato = "jornada_actual_juvenil.php";
    String J1 = "juvenil/jornadas/j_01.php";
    String J2 = "juvenil/jornadas/j_03.php";

    private List<ModelEstadisticas> listJornadas;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private RecyclerView.Adapter adapter;

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

        View view = inflater.inflate(R.layout.jornadas_list, null);

        j1 = (Button)view.findViewById(R.id.j1);
        j1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dato = J1;
                getData();
            }
        });

        j2 = (Button)view.findViewById(R.id.j2);
        j2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dato = J2;
                getData();
            }
        });

        //Initializing Views
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        //layoutManager = new LinearLayoutManager(getActivity());
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(layoutManager);

        final TextView miTexto = (TextView) view.findViewById(R.id.mi_texto);
        miTexto.setText(R.string.list_juveniles);
        //miTexto.setTextColor(color.RED);

        //Initializing our jornadas list
        listJornadas = new ArrayList<>();

        RequestQueue queue;

        //Laamando al metodo get data
        getData();

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

    //This method will get data from the web api
    private void getData() {
        //Showing a progress dialog
        final ProgressDialog loading = ProgressDialog.show(getActivity(), "Cargando datos", "Por favor espere...", false, false);

        //Creating a json array request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url+dato,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //Dismissing progress dialog
                        loading.dismiss();

                        //calling method to parse json array
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }

    //This method will parse json data
    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            ModelEstadisticas jornadas = new ModelEstadisticas();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                jornadas.setFecha(json.getString("fecha"));
                jornadas.setHora(json.getString("hora"));

                jornadas.setE_local(json.getString("nomLocal"));
                jornadas.setE_visi(json.getString("nomVisitante"));
                jornadas.setR_local(json.getString("resulLocal"));
                jornadas.setEstado(json.getString("estadoPartido"));
                jornadas.setR_visi(json.getString("resulVisitante"));

                jornadas.setEsc_local("http://ffcv.es/ncompeticiones/" + (json.getString("escudoLocal")));
                jornadas.setEsc_visi("http://ffcv.es/ncompeticiones/" + (json.getString("escudoVisitante")));


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listJornadas.add(jornadas);
        }

        //Finally initializing our adapter
        adapter = new JornadasAdapter(listJornadas, getActivity());

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }
}

The adapter if necessary

public class JornadasAdapter extends RecyclerView.Adapter<JornadasAdapter.ViewHolder> {

private ImageLoader imageLoader;
private Context context;

List<ModelEstadisticas> estadisticas;

public JornadasAdapter(List<ModelEstadisticas> estadisticas, Context context) {
    super();
    this.estadisticas = estadisticas;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.jornadas_row, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Toast.makeText(context, "PROXIMAMENTE",
            //Toast.LENGTH_LONG).show();

            Toast.makeText(context, estadisticas.get(position).getE_local(), Toast.LENGTH_SHORT).show();

            /*Intent intent= new Intent(context, DetailPartidos.class);

            //intent.putExtra("Jornada", estadisticas.get(position).getJornada());
            intent.putExtra("Fecha", estadisticas.get(position).getFecha());
            intent.putExtra("E_local", estadisticas.get(position).getE_local());
            intent.putExtra("E_visi", estadisticas.get(position).getE_visi());
            intent.putExtra("R_Local", estadisticas.get(position).getR_local()+estadisticas.get(position).getR_visi());



            intent.putExtra("Esc_Local", estadisticas.get(position).getEsc_local());
            intent.putExtra("Esc_Visi", estadisticas.get(position).getEsc_visi());

            context.startActivity(intent);*/


            ModelEstadisticas modelEstadisticas1 = estadisticas.get(getAdapterPosition());
        }

        private int getAdapterPosition() {
            return 0;
        }
    });
    ModelEstadisticas jornadas = estadisticas.get(position);
    imageLoader = DecoracionLineaDivisoria.CustomVolleyRequest.getInstance(context).getImageLoader();
    imageLoader.get(jornadas.getEsc_local(), ImageLoader.getImageListener(holder.escudo_local, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
    imageLoader.get(jornadas.getEsc_visi(), ImageLoader.getImageListener(holder.escudo_visi, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

    holder.textV_Fecha.setText(jornadas.getFecha());
    //holder.textV_Hora.setText(" a las " + jornadas.getHora());
    holder.textV_Hora.setText(jornadas.getHora());
    holder.textV_Equipo_Local.setText(jornadas.getE_local());
    holder.textV_Equipo_Visitante.setText(jornadas.getE_visi());
    holder.textV_Res_Local.setText(jornadas.getR_local());
    holder.textV_Estado.setText("  -  " + jornadas.getEstado() + "  -  ");
    holder.textV_Res_Visitante.setText(jornadas.getR_visi());
    holder.textV_Arbitro.setText(jornadas.getArbitro());

    //holder.escudo_local.setImageUrl(jornadas.getEsc_local(), imageLoader);
    //holder.escudo_visi.setImageUrl(jornadas.getEsc_visi(), imageLoader);

    /*Glide.with(context)
            .load(jornadas.getEsc_local())
            //.placeholder(R.drawable.friends_red)
            //.transform(new CircleTransform(context))
            .into(holder.escudo_local);

    Glide.with(context)
            .load(jornadas.getEsc_visi())
            //.placeholder(R.drawable.friends_red)
            //.transform(new CircleTransform(context))
            .into(holder.escudo_visi);*/
}

@Override
public int getItemCount() {
    return estadisticas.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView textV_Fecha;
    public TextView textV_Hora;
    public TextView textV_Equipo_Local;
    public TextView textV_Equipo_Visitante;
    public TextView textV_Res_Local;
    //public TextView textV_Res_Visi;
    public TextView textV_Estado;
    public TextView textV_Res_Visitante;
    public TextView textV_Arbitro;

    public ImageView escudo_local;
    public ImageView escudo_visi;


    public View root;

    public ViewHolder(View itemView) {
        super(itemView);
        root = itemView;


        textV_Fecha = (TextView) itemView.findViewById(R.id.tv_Fecha);
        textV_Hora = (TextView) itemView.findViewById(R.id.tv_Hora);
        textV_Equipo_Local = (TextView) itemView.findViewById(R.id.tv_Equipo_Local);
        textV_Equipo_Visitante = (TextView) itemView.findViewById(R.id.tv_Equipo_Visitante);

        textV_Res_Local = (TextView) itemView.findViewById(R.id.tv_Result_Local);
        textV_Estado = (TextView) itemView.findViewById(R.id.tv_Estado);
        textV_Res_Visitante = (TextView) itemView.findViewById(R.id.tv_Result_Visitante);
        textV_Arbitro = (TextView) itemView.findViewById(R.id.tv_Arbitro);

        escudo_local = (ImageView) itemView.findViewById(R.id.tv_esc_local);
        escudo_visi = (ImageView) itemView.findViewById(R.id.tv_esc_visi);
    }
}

}

    
asked by Rafel C.F 08.10.2017 в 12:31
source

1 answer

1

This should solve your problem, I have only added the necessary sentences to delete the content of the List listJornadas , every time you call the parseData() method if the list contains elements will be deleted, that way in the RecyclerView will only load the current data.

public class FragJuv01 extends Fragment {

    ...

    //This method will parse json data
    private void parseData(JSONArray array) {

        // Si listJornadas es diferete a cero
        if (listJornadas.size() != 0) {
            // Elimina todos los elementos de la lista
            listJornadas.clear();
        }

        for (int i = 0; i < array.length(); i++) {
            ModelEstadisticas jornadas = new ModelEstadisticas();
            JSONObject json = null;
            try {
                json = array.getJSONObject(i);

                jornadas.setFecha(json.getString("fecha"));
                jornadas.setHora(json.getString("hora"));

                jornadas.setE_local(json.getString("nomLocal"));
                jornadas.setE_visi(json.getString("nomVisitante"));
                jornadas.setR_local(json.getString("resulLocal"));
                jornadas.setEstado(json.getString("estadoPartido"));
                jornadas.setR_visi(json.getString("resulVisitante"));

                jornadas.setEsc_local("http://ffcv.es/ncompeticiones/" + (json.getString("escudoLocal")));
                jornadas.setEsc_visi("http://ffcv.es/ncompeticiones/" + (json.getString("escudoVisitante")));


            } catch (JSONException e) {
                e.printStackTrace();
            }
            listJornadas.add(jornadas);
        }

        //Finally initializing our adapter
        adapter = new JornadasAdapter(listJornadas, getActivity());

        //Adding adapter to recyclerview
        recyclerView.setAdapter(adapter);
    }
}
    
answered by 08.10.2017 / 15:36
source