0

I need to use the same ArrayAdapter in two activities. In MainActivity i need to show only Items with isConsumido() = true and in another activity i need to show all Items.

My MainActivity adapter:

        final ArrayAdapter<ItemCultural> arrayAdapterOrdenado = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturaisOrdenados());

Now, im using two adapters, but when i check isConsumido() in one item of adapter from TelaCadastrados, the same item in MainActivity need to turn invisible, and when i remove item on TelaCadastrados, the same item need to be removed on MainActivity.

My TelaCadastrados adapter:

        final ArrayAdapter<ItemCultural> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais());

2 Answers2

1

You should use 2 adapters, but they can have a common data set, so when you change something it affects both adapters at the same time. But remember to call notifyDataSetChanged() in both adapters.

Francesc
  • 25,014
  • 10
  • 66
  • 84
1

Don't share the adapter, share the underlying data set. You have list of the data objects, and the adapter wraps that list. Share the list, not the adapter.

Your isConsumido() = true logic is a property of the adapter (constructor argument). You'll have to create a custom adapter to add that behavior.

You can store the list of ItemCultural in your application class, so in each activity, you do something like,

List<ItemCultural> data = ((MyApplication)getApplication).getData();
final ArrayAdapter<ItemCultural> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, data);

Someone will probably tell you to store the data in a static. Don't do that.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • How can I make invisible certain item of my ArrayAdapter,when the same shall belong to two screens, but i just want to make invisible in one. – Guilherme Bertoluchi Mar 04 '16 at 15:55
  • Code it :) You need to have a custom adapter. That custom adapter conditional renders your `ItemCultural` object. Take a look at this answer: http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view to get started with a custom adapter. `ArrayAdapter` is not sophisticated enough to give you what you want. – Jeffrey Blattman Mar 04 '16 at 16:44