2

I have made changes as suggested and it's working fine.but I have put button mainactivity to go to main2activity to check list but it's giving error. This is my code and button to check list is R.Id.viewlist

 public class MainActivity extends AppCompatActivity {
 EditText editText;
  Button addButton;
      @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     final EditText editText =(EditText)findViewById(R.id.editText);
     Button button = (Button)findViewById(R.id.addButton);
     button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(MainActivity.this,Main2Activity.class);
             intent.putExtra("text", editText.getText().toString());
             startActivity(intent);
         }
     });
     Button button1 = (Button)findViewById(R.id.viewlist);
     button1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(MainActivity.this,Main2Activity.class);
              startActivity(intent);
         }
     });

main2acctivity

public class Main2Activity extends AppCompatActivity {
 ListView listView;
    ArrayAdapter<String> adapter;
   @Override
 protected void onRestart() {
     super.onRestart();
     adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Global.list1);
      listView.setAdapter(adapter);
     if (Global.list1.size()<1){
         Global.list1=new ArrayList<>();
     }
  }
  @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main2);
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
       Intent intent1= getIntent();
        listView = (ListView) findViewById(R.id.list);
          adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Global.list1);
      listView.setAdapter(adapter);
     Global.list1.add(intent1.getStringExtra("text"));
      adapter.notifyDataSetChanged();
                   } 

Global. Class

3 Answers3

0

why not use shared preferences? Save the data in the onPause of your activity and load it in when the you fire up the other activity. http://developer.android.com/training/basics/data-storage/shared-preferences.html.

You could also solve this using multiple fragments and save the state of the app in the activity that contains both fragments I suppose.

Bram
  • 4,533
  • 6
  • 29
  • 41
0

This depends on if you want to persist this list just within the app use session (meaning if they close the app, the list would reset from the beginning. If that is the case, you would want to use a "SharedPreferences" and store your items in a list that maps to a field in SharedPreferences. If that is the case, you can go here:

This will show you how to store an array list of items that persists even if the user restarts the app and goes back to that 2nd activity.

Otherwise, if you don't want to have a persistence across app sessions/restarts, then you can actually just use a Global Singleton to store list of sorts as was suggested above.

 public static class GlobalListStorage {
     private static GlobalListStorage instance = null;
     public ArrayList<String> mItems=new ArrayList<String>();
     protected GlobalListStorage() {
     }

     public GlobalListStorage getInstance() {
         if (instance == null) {
             instance = new GlobalListStorage();
         }
         return instance;
     }
 }

Now when you want to add items to your list, you would do the following in your MainActivity.java, in your button click listener.

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        GlobalListStorage.getInstance().mItems.add(editText.getText().toString());
        startActivity(intent);
    }
});

Now in your ArrayListAdapter, make sure to use the GlobalListStorage.getInstance().mItems as your arrayList that is supposed to be used.

ngoctranfire
  • 793
  • 9
  • 15
0

You should override the onRestart metod of your activity and put in the code for populate the list view with the global array.

@Override
public void onRestart()
{
super.onRestart();
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, Global.list1);
listView.setAdapter(adapter);
}

And maybe you don't want to do this

Global.list1 = new ArrayList();

Instead, you should do this

if(Global.list1.size()<1)
{
Global.list1 = new ArrayList();
Global.list1.add("First Item - added on Activity Create");
}

EDIT: For the new problem:

//In the Main2Activity
String haveString=intent1.getStringExtra("text");
if(haveString!=null)
   {
   Global.list1.add(haveString);
   adapter.notifyDataSetChanged();
   }
DonLeopardo
  • 140
  • 6