How to modify a ListView present in another activity without using Intent?

0

I created a listView in the initial activity of a project, and would like to know how to modify it from other activities, without having to use intent.putExtras .

Activity01 (where the listView is):

package genesysgeneration.lvatt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public ListView lvItens;
    public ArrayList<String> alsItens;
    public ArrayAdapter<String> aasItens;
    private Button btnNext;

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

        lvItens=(ListView)findViewById(R.id.lvItens);
        alsItens=new ArrayList<String>();
        aasItens=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1);
        lvItens.setAdapter(aasItens);

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(it);

            }
        });

    }

}

Activity02 (one of the activities that modify it):

package genesysgeneration.lvatt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class Main2Activity extends AppCompatActivity {

    private Button btnArco, btnEspada, btnBack;

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

        btnArco=(Button)findViewById(R.id.btnArco);
        btnEspada=(Button)findViewById(R.id.btnEspada);
        btnBack=(Button)findViewById(R.id.btnBack);

        btnArco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alsItens.add("Arco");
                aasItens.notifyDataSetChanged();

            }
        });

        btnEspada.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alsItens.add("Espada");
                aasItens.notifyDataSetChanged();

            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(Main2Activity.this, Main3Activity.class);
                startActivity(it);

            }
        });

    }
}

Note that I put in the "sets" of each Button the modification I would like to happen ( this would work if everything was in the same activity ).

I would like the changes to happen by the time each button was clicked (setOnClickListener) and that the listView was updated and correct regardless of the previous activity.

The initial activity, where the listView is, is always running.

    
asked by anonymous 25.02.2017 / 23:56

1 answer

2

As I said, the simplest way is to use Activity results to submit a result of MainActivity2 for MainActivity .

Using this technique would look like this:

public class Main2Activity extends AppCompatActivity {

    private Button btnArco, btnEspada, btnBack;
    private ArrayList<String> resultados = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Restante do seu código...

        btnArco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultados.add("Arco");
            }
        });

        btnEspada.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultados.add("Espada");
            }
        });
    }

    @Override
    public void onBackPressed() {
        final Intent resultado = new Intent();

        resultado.putStringArrayListExtra("resultado", resultados);
        setResult(Activity.RESULT_OK, resultado);

        super.onBackPressed();
    }
}

public class MainActivity extends AppCompatActivity {

    private static final int RC_ITEMS = 231;

    // Restante das suas variáveis...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Restante do seu código...
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(MainActivity.this, Main2Activity.class);
                startActivityForResult(it, RC_ITEMS);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_ITEMS && resultCode == Activity.RESULT_OK) {
            ArrayList<String> items = data.getStringArrayListExtra("resultado");

            alsItens.addAll(items);
            aasItens.addAll(items);    
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
    
26.02.2017 / 18:41