Home > Mobile >  Can't make the button add a new item on the RecyclerList, only works once
Can't make the button add a new item on the RecyclerList, only works once

Time:12-16

Been working on this code for a while and as im new to recyclerList I can't seem to figure out why the button works only once.

Here is the adapter:


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class AdaptadorLista extends RecyclerView.Adapter<AdaptadorLista.ViewHolderLista> {

    ArrayList<ComponentesLista> listaComponentes;

    public AdaptadorLista(ArrayList<ComponentesLista> listaComponentes) {
        this.listaComponentes = listaComponentes;
    }

    @NonNull
    @Override
    public ViewHolderLista onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_lista, parent, false);
        return new ViewHolderLista(view);
    }

    @Override
    public void onBindViewHolder(ViewHolderLista holder, int position) {

        holder.textViewDescripcion.setText(String.valueOf(listaComponentes.get(position).getDescripcion()));
        holder.textViewDinero.setText(String.valueOf(listaComponentes.get(position).getDinero()));
        holder.textViewCantidad.setText(String.valueOf(listaComponentes.get(position).getCantidad()));



    }

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

    public class ViewHolderLista extends RecyclerView.ViewHolder {

        private TextView textViewCantidad, textViewDescripcion, textViewDinero;

        public ViewHolderLista(@NonNull View itemView) {
            super(itemView);
            textViewCantidad= (TextView) itemView.findViewById(R.id.textMuestraCantidad);
            textViewDescripcion = (TextView) itemView.findViewById(R.id.textMuestraDescripcion);
            textViewDinero = (TextView) itemView.findViewById(R.id.textMuestraPrecio);
        }
    }
}

Here is the object it works with:

public class ComponentesLista {
    private String descripcion;
    private String cantidad;
    private String dinero;

    public ComponentesLista() {

    }

    public ComponentesLista(String descripcion, String cantidad, String dinero) {
        this.descripcion = descripcion;
        this.cantidad = cantidad;
        this.dinero = dinero;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getCantidad() {
        return cantidad;
    }

    public void setCantidad(String cantidad) {
        this.cantidad = cantidad;
    }

    public String getDinero() {
        return dinero;
    }

    public void setDinero(String dinero) {
        this.dinero = dinero;
    }
}

And here is the main method (It's not the main method, it's another screen I use after a button press but it shouldn't be the issue right?)

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class PantallaLista extends AppCompatActivity {

    ArrayList<ComponentesLista> componentesLista;
    RecyclerView recyclerLista;
    
    //int cont = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista);

        // Seccion ArrayList Lista

        componentesLista=new ArrayList<>();

        recyclerLista= (RecyclerView) findViewById(R.id.recyclerViewLista);
        recyclerLista.setLayoutManager(new LinearLayoutManager(this));

        
        // Botones

        Button nuevo = (Button) findViewById(R.id.nuevoButton);
        
        nuevo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                llenarLista();
            }
        });
        
        Button guardarVolver = (Button) findViewById(R.id.terminarButton);

        guardarVolver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PantallaLista.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    private void llenarLista () {

        EditText textoGetDescripcion = (EditText) findViewById(R.id.textInputProducto);
        EditText textoGetCantidad = (EditText) findViewById(R.id.textInputCantidad);
        EditText textoGetPrecio = (EditText) findViewById(R.id.textInputPrecio);


        componentesLista.add(new ComponentesLista(textoGetDescripcion.getText().toString(), textoGetPrecio.getText().toString(), textoGetCantidad.getText().toString()));

        AdaptadorLista adapter = new AdaptadorLista(componentesLista);

        recyclerLista.setAdapter(adapter);
        
        //System.out.println(textoGetDescripcion.getText().toString()   textoGetPrecio.getText().toString()   textoGetPrecio.getText().toString());
    }

}

CodePudding user response:

You are setting an adapter every time you add the item to the list. Whenever you are adding a new item to the list, you've to use adapter.notifyItemInserted(position)

CodePudding user response:

Here's the activiy

public class PantallaLista extends AppCompatActivity {

ArrayList<ComponentesLista> componentesLista;
RecyclerView recyclerLista;
AdaptadorLista adapter;

//int cont = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista);

    // Seccion ArrayList Lista

    componentesLista=new ArrayList<>();

    recyclerLista= (RecyclerView) findViewById(R.id.recyclerViewLista);
    recyclerLista.setLayoutManager(new LinearLayoutManager(this));

    adapter = new AdaptadorLista(componentesLista);
    recyclerLista.setAdapter(adapter);
    
    // Botones

    Button nuevo = (Button) findViewById(R.id.nuevoButton);
    
    nuevo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            llenarLista();
        }
    });
    
    Button guardarVolver = (Button) findViewById(R.id.terminarButton);

    guardarVolver.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(PantallaLista.this, MainActivity.class);
            startActivity(intent);
        }
    });
}

private void llenarLista () {

    EditText textoGetDescripcion = (EditText) findViewById(R.id.textInputProducto);
    EditText textoGetCantidad = (EditText) findViewById(R.id.textInputCantidad);
    EditText textoGetPrecio = (EditText) findViewById(R.id.textInputPrecio);

    ComponentesLista tempData = new ComponentesLista(textoGetDescripcion.getText().toString(), textoGetPrecio.getText().toString(), textoGetCantidad.getText().toString())
    adapter.addNewItems(tempData);
 
}

}

heres the adapter

public class AdaptadorLista extends RecyclerView.Adapter<AdaptadorLista.ViewHolderLista> {

ArrayList<ComponentesLista> listaComponentes;

public AdaptadorLista(ArrayList<ComponentesLista> listaComponentes) {
    this.listaComponentes = listaComponentes;
}

public addNewItems(ComponentesLista tempComponent){
    this.listaComponentes.add(tempComponent);
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolderLista onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_lista, parent, false);
    return new ViewHolderLista(view);
}

@Override
public void onBindViewHolder(ViewHolderLista holder, int position) {

    holder.textViewDescripcion.setText(String.valueOf(listaComponentes.get(position).getDescripcion()));
    holder.textViewDinero.setText(String.valueOf(listaComponentes.get(position).getDinero()));
    holder.textViewCantidad.setText(String.valueOf(listaComponentes.get(position).getCantidad()));
}

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

public class ViewHolderLista extends RecyclerView.ViewHolder {

    private TextView textViewCantidad, textViewDescripcion, textViewDinero;

    public ViewHolderLista(@NonNull View itemView) {
        super(itemView);
        textViewCantidad= (TextView) itemView.findViewById(R.id.textMuestraCantidad);
        textViewDescripcion = (TextView) itemView.findViewById(R.id.textMuestraDescripcion);
        textViewDinero = (TextView) itemView.findViewById(R.id.textMuestraPrecio);
    }
}

}

please tell if you are not understanding something.

  • Related