Passing a List from one class to another in Android

0

I'm trying to have several values in a List and within this there will also be another list, for that create a model class and then use it with a List to save the different data. Everything is fine, I keep all the data normal, but at the moment of consuming that data, the only one that does not arrive is the data contained in the list of the model class, then I leave my code:

public class Modelo {

public String IdActi;
int Duracion, Costo;
public List<String> Prreq = new ArrayList<String>();

Model class

public Modelo(){

}

public Modelo(String idActi, int duracion, int costo, List<String> prreq) {
    IdActi = idActi;
    Duracion = duracion;
    Costo = costo;
    Prreq = prreq;
    Log.i("Actvidad tamano", ""+Prreq.size());

}

}

Main class

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private List<Modelo> Actividades;
private List<String> Prerreq = new ArrayList<String>();

private EditText nom,dur,cost,pre;
private Button AgregarPre,AgregarActividad,Generar;

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

    Actividades = new ArrayList<>();

    nom=(EditText) findViewById(R.id.Id);
    dur=(EditText) findViewById(R.id.Duracion);
    cost=(EditText) findViewById(R.id.Costo);
    pre=(EditText) findViewById(R.id.Prreq);

    AgregarPre=(Button) findViewById(R.id.btnAgregarPre);
    AgregarActividad=(Button) findViewById(R.id.btnAgregarActividad);
    Generar=(Button) findViewById(R.id.btnGenerar);

    AgregarPre.setOnClickListener(this);
    AgregarActividad.setOnClickListener(this);
    Generar.setOnClickListener(this);


}

@Override
public void onClick(View view) {
    if(view==AgregarPre){
        Prerreq.add(pre.getText().toString());
    }
    if (view==AgregarActividad){

        Actividades.add(new Modelo(nom.getText().toString(),Integer.parseInt(dur.getText().toString()),
                Integer.parseInt(cost.getText().toString()),  Prerreq));
        int cont2=Prerreq.size();
        for (int b=0;b<cont2;b++) {
            Log.i("Actvidad prerreq", Prerreq.get(b));
        }


        Prerreq.clear();
    }
    if (view==Generar){
        int cont=Actividades.size();
        String id;StringBuilder pre= new StringBuilder();
        int du,co;

        for (int a=0;a<cont;a++){
            int cont2=Actividades.get(a).Prreq.size();
            Log.i("Actvidad tamano", ""+Actividades.get(0).Prreq.size());
            id=Actividades.get(a).IdActi;
            du=Actividades.get(a).Duracion;
            co=Actividades.get(a).Costo;
            for (int b=0;b<cont2;b++) {
                pre.append(Actividades.get(a).Prreq.get(b)).append(", ");
            }
            Log.i("Actvidad",id+", "+du+", "+co+", "+pre);
        }
    }
}
}

Here I add the elements of the list

 Actividades.add(new Modelo(nom.getText().toString(),Integer.valueOf(dur.getText().toString()),
                Integer.valueOf(cost.getText().toString()),  Prerreq));

And this way I consume the data to show them in console

int cont=Actividades.size();
        String id;StringBuilder pre= new StringBuilder();
        int du,co;

        for (int a=0;a<cont;a++){
            int cont2=Actividades.get(a).Prreq.size();
            Log.i("Actvidad tamano", ""+Actividades.get(0).Prreq.size());
            id=Actividades.get(a).IdActi;
            du=Actividades.get(a).Duracion;
            co=Actividades.get(a).Costo;
            for (int b=0;b<cont2;b++) {
                pre.append(Actividades.get(a).Prreq.get(b)).append(", ");
            }
            Log.i("Actvidad",id+", "+du+", "+co+", "+pre);
    
asked by Pericles Slas 23.07.2018 в 01:50
source

0 answers