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.