Communication between two classes

1

I have a class A that contains an ArrayList, I also have a class B in which I want to be able to add an item to the ArrayList. How can I add the item to the array belonging to the other class?

Class A:

public class MainActivity extends AppCompatActivity {
    ArrayList<Item> lista = new ArrayList<>();

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

        makeList();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:

                Intent inten = new Intent(MainActivity.this,SettingsActivity.class);
                startActivity(inten);
                return true;

            default:
                return super.onOptionsItemSelected(item);

        }
    }

Class B:

public class AddItem extends AppCompatActivity {

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

    public void addItem(){

        EditText tex1 = (EditText) findViewById(R.id.nombre);
        String nombre = "" + tex1.getText();

        Item item1 = new Item(nombre);
        //AQUI DEBERIA IR UN 
        //lista.add(item1) 
        //PERO LA VARIABLE lista PERTENECE A LA CLASE A

    }
    
asked by Jose 11.07.2017 в 00:02
source

1 answer

2

You need to use setActivityForResult that tells the new activity that you are expecting a value from it.

This would be in your class at:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:

                Intent inten = new Intent(MainActivity.this,SettingsActivity.class);
                startActivityForResult(inten, 150);
                return true;

            default:
                return super.onOptionsItemSelected(item);

        }
    }

In your class b, when you finish and know which element you want to add to the ArrayList, you assign it the setResult to indicate to the activity that it should send the value to the previous activity:

Intent intent = new Intent();
intent.putExtra("elementoParaElArrayList", "valor")
setResult(RESULT_OK, intent);        
finish();

Then to receive that value on you write onActivityResult in your class to:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 150) {
         if(resultCode == RESULT_OK) {
             String valorLista = data.getStringExtra("elementoParaElArrayList");
             // agregas el elemento a la lista.
         }     
    }
} 

Update:

Now I stop to read your question well (bad me).

Your intention is to have the reference of ArrayList of class MainActivity in class AddItem . This can not be done unless you make the list static and it is not recommended because of the complexity that it requires to know whether to clean the list or not, whether to add elements to the list or not, whether to remove an element from the list or not .. .

But in the end each one at the end decides the design of their application.

Send own object

If what you want is to send an own object. Simply implement the interface Serializable .

public class Item implements Serializable{
 // ...
}

And you get the serializable object of the result using getSerializableExtra(string key) of the intent:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 150) {
         if(resultCode == RESULT_OK) {
             Item valorLista = (Item)data.getSerializableExtra("elementoParaElArrayList");
             // agregas el elemento a la lista.
         }     
    }
} 
    
answered by 11.07.2017 / 00:24
source