0
I created a listview in the initial Activity of a project, and would like to know how to modify it from other activitys, without having to use the "Intent.".
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 activitys that modifies 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);
}
});
}
}
Notice that I put in the "sets" of each Button the modification that I would like to happen (It would work if everything was in the same).
I would like the modifications to happen at the moment when each of the buttons were clicked (setOnClickListener) and that the listview was updated and correct independent of the previous Activity.
The initial Activity, where the listview, will always be executed.
My suggestion is to use the results of
Activity
to receive a value resulting fromMainActivity2
in hisMainActivity
. The only problem I see is you need to finalize theMainActivity2
at some point informing the items to be added toMainActivity
.– Wakim
then the change would only occur when the main2 was finished?
– Boneco Sinforoso
That, exact, you need to set the result and finalize the
MainActivity2
for another Activity to be notified.– Wakim
but I could do this in order to finish main2 and go, for example, to a main3 and still the main1 be modified?
– Boneco Sinforoso
But you don’t have to upgrade Main to Main3, right? Just need to update the Main when you go back to it, which you would do when giving back press I think, you would have to intercept the back press in Main2 and set the result.
– Wakim
how do I do that?
– Boneco Sinforoso
I’m going to need to run a little test and put in an answer.
– Wakim
Okay, thanks in advance!
– Boneco Sinforoso