Joel Dean
Joel Dean

Reputation: 2454

How to switch an android view based on a condition

I am creating an Android application that will mainly consists of ListViews in each activity. What I hope to achieve is a mechanism that will check to see if network connectivity is present and if so then then the data should be retrieved and supplied to the ListView. If there's no data present or the internet connection is unavailable then a seperate view should be loaded.

Which way is most efficient do to this. So far I have seen answers about using a ViewSwitcher or Viewflipper but I am not sure that's the right approach. Should I use fragments and then load a particular fragment based on the condition that was met? I just need some advice on how to accomplish this.

Upvotes: 1

Views: 504

Answers (3)

Nicolas Durán
Nicolas Durán

Reputation: 292

As @Ivan says you should consider setting adapters in a dinamic way.

And about the fragments thing, only consider using independent fragments (or ListFragments) for this if you are going to re-use that fragments in other activities to avoid code duplication.

Upvotes: 0

Kevin Coppock
Kevin Coppock

Reputation: 134684

The best way is going to be to use the emptyView that's provided as part of the AdapterView base class. You just set the adapter as per usual, but if no data is retrieved (i.e. your adapter's data source is empty) you will show the empty view in place of the list. This empty view can be anything you want it to be.

Upvotes: 0

Ivan
Ivan

Reputation: 6013

Have you considered just swapping the adapter? This question might be of some use to you.

I guess you might add some kind of factory method that will decide what adapter to instantiate depending on connectivity availability. Thus you will control both the data to be used and the appearance of list entries (I assume you will be using some custom extension of one of the existing adapters).

Upvotes: 1

Related Questions