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

MultyQuery is working in phpmyadmin, is not working in Mysqli, did work on an other server

php mysql phpmyadmin mysqli

I'm using Msqli to make a multiquery, the query works exactly like i want within phpmyadmin.

CakePHP DebugKit / Plugins webroot not being found

php cakephp

I've installed CakePHP's debug kit but it's not loading correctly on my pages, its just a mess of text and arrays at the bottom of the page. My browser is saying that it's getting a 404 on:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.