garciam202
garciam202

Reputation: 631

Add dynamic item to listview

I need to add items dynamically to listview.

Now i am using FAB (crearEntrenamintoActivity) to open a New activity (crearEjercicioActivity) and here complete the necesary data of the New item to add into listview.

But by this way i need to pass the parceableExtra all time between both activities.

Are there other simpler way to do this?

Edit:

Here when i press FAB button i send to the new activity the extra "entrenamiento" because i need to save all the items of the listview into this object.

public void initFAB() {
        FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fabCrearEntrenamiento);
        if (floatingActionButton != null) {
            floatingActionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //lanzamos la actividad de crear ejercicio
                    entrenamiento.setUbicacion(etUbicacion.getText().toString());
                    entrenamiento.setHoraInicio(etHoraInicio.getText().toString());
                    Intent intent = new Intent(getApplicationContext(), CrearEjercicioActivity.class);
                    intent.putExtra("entrenamiento", entrenamiento);
                    startActivity(intent);
                }
            });
        }
    }

Then in "crearEjercicioActivity" i have this. Here when i press save button i add in the arrayList the new item.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_save:
                Entrenamiento entrenamiento = getIntent().getParcelableExtra("entrenamiento");
                Ejercicio ejercicio = new Ejercicio();
                EditText titulo = (EditText) findViewById(R.id.etTituloEjercicio);
                ejercicio.setTitulo(titulo.getText().toString());
                entrenamiento.getEjercicios().add(ejercicio);
                Intent intentEjercicio = new Intent(getApplicationContext(), CrearEntrenamientoActivity.class);
                intentEjercicio.putExtra("entrenamiento", entrenamiento);
                startActivity(intentEjercicio);
                return true;
            case R.id.action_home:
                Intent intent = new Intent(getApplicationContext(), DashBoardActivity.class);
                startActivity(intent);
            default:
                return super.onOptionsItemSelected(item);
        }
    }

And finally i do this:

if (entrenamiento.getEjercicios().size() > 0) {
            ListView listViewEjercicios = (ListView) findViewById(R.id.listViewEjercicios);
            ArrayList<String> titulosEjercicios = new ArrayList<>();

            for (Ejercicio ejercicio : entrenamiento.getEjercicios()) {
                titulosEjercicios.add(ejercicio.getTitulo());
            }

            ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, R.layout.simple_row_ejercicio_layout, titulosEjercicios);
            if (listViewEjercicios != null) {
                listViewEjercicios.setAdapter(listAdapter);
            }
        }

Upvotes: 0

Views: 214

Answers (2)

beeb
beeb

Reputation: 1615

I'd recommend a manager class for this which is singleton. The manager should include data structure like a list or a map. There you can save your objects. If you want to add a new object call manager.add(yourobject). Additionally you can a something like a notify callback. Every view which use the data of the manager have to implement your callback. Register the views on the manager with manager.registerCallback(). And everytime when you modify the data of the manager like add() or remove() you have to call notifyCallbacks().

If you implement my solution then you don't have to create multiple adapter objects. You're working with one list/map object which is saved in your manager. And after modifying the data you just have to call adapter.notifyDataSetChanged().

Check my code example on this post: Unable to refresh listview data within same fragments used under different tabs

Upvotes: 2

Hendrik Marx
Hendrik Marx

Reputation: 744

You could use just one Activity and then have Fragments for each of your two views. The Activity can then hold the reference to your element and handle the communication between the two Fragments by calling public methods on them (like described here at the bottom).

If you want to stick to activities the intents are probably the recommended approach.

Upvotes: 0

Related Questions