ListView items not getting populated when activity is launched

Here is my scenario: I have a MainActivity and a CustomListViewActivity. In my MainActivity, I have 1 button and 1 spinner. On click of the button, I pass the selected spinner value to the CustomListViewActivity via Bundle and using Intents.

Now, in my CustomListViewActivity, I have a ListView that uses ArrayAdapter for populating it. I send a ArrayList from MainActivity, say for example

 items = [abc]

In my CustomListViewActivity, I receive the same and use it to populate my ListView. The first time I do this, my value gets populated. The second time I do the same, the value existing is now replaced with the new one and the ListView shows one item instead of showing two.

So basically the problem is that the ListView is not updating and not showing both the items. Instead it shows me a single item always.

Here are snippets of my code

  MainActivity.java

//code inside button click ..

        {          
            items.add(spinner1.getSelectedItem().toString());


                 Bundle bundle =new Bundle();                   
                 bundle.putStringArrayList("data",items);


                Intent i = new Intent(MainActivity.this,MyListViewActivity.class);
                i.putExtras(bundle);
                startActivity(i);
                finish();
        }

        protected void onSaveInstanceState(Bundle icicle) {
      super.onSaveInstanceState(icicle);
      Log.i("App","onSave");
      icicle.putStringArrayList("data",items);
    }

    @Override
        protected void onRestoreInstanceState(Bundle icicle2) {
     // TODO Auto-generated method stub
           super.onRestoreInstanceState(icicle2);
           Log.i("App","onRestore");
       icicle2.putStringArrayList("data",items);
     }

MyListViewActivity.java

      private ArrayList<String> myItems;
      private static String[] titles;
      CustomListViewAdapter adapter;

  @Override
       public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.list_main);

          listView = (ListView) findViewById(R.id.list1);
          rowItems = new ArrayList<RowItem>();
          adapter = new CustomListViewAdapter(this,R.layout.list_item, rowItems);
          listView.setAdapter(adapter);

          b=getIntent().getExtras();

          if(b!=null)
          {
              myItems=b.getStringArrayList("data");

             titles= new String[myItems.size()];
             titles = myItems.toArray(myItems);
          } 


          ...

            int i=0;
        while(i<titles.length) {
            item = new RowItem(titles[i]);
            //rowItems.add(item);
            Log.i("ROW", ""+rowItems.size());
            i++;
            adapter.add(item);
            listView.setAdapter(adapter);    
           }


           adapter.setNotifyOnChange(true);
           adapter.notifyDataSetChanged();            
    }

 }

How to make the ListView maintain the current data as well reflect the added data?

Thanks in advance

EDIT : Forgot to mention one more thing. In my MyListViewActivity class I have a button above the ListView that on clicked takes me to my MainActivity so that I can add a new Item again. So when I go back to MainActivity and try and add a new Item , it's the new Item that get displayed rather than showing both the previous and the new one

Answers


You need to persist and restore your items list in MainActivity's onSaveInstanceState and onRestoreInstanceState methods.

You also probably don't need to close the MainActivity by calling finish() every time you start the ListView activity but that depends on your application.

Basically, the problem you are having is that items is being recreated with each new instance of MainActivity. Since you finish MainActivity, a new instance is used every time you access that activity (and I assume you thought that items would just keep getting items added to it).


Need Your Help

Friendship relations tracking algorithm

algorithm social-networking

I'm thinking of an application that would try to prove the "Six degrees of separation" theory with a set of users that are part of a social network.

Are nulls in a relational database okay?

database-design oop null

There's a school of thought that null values should not be allowed in a relational database. That is, a table's attribute (column) should not allow null values. Coming from a software development