How to switch an android view based on a condition - android

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.

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).

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.

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.

Related

How to create multilevel recyclerview in Android kotlin?

I want to create 3 level RecyclerView like tree view in kotlin. Is there any tutorial and suggestions please let me know.
I already tried so many times with ExpandableListView and 3 RecyclerView, but didn't find any proper solution.
By a multi-level RecyclerView, do you mean a RecyclerView with paths to different lists that branch depending upon which item has been selected? If that's the case, I would honestly recommend using a single adapter to cycle through multiple lists depending upon user input.
If you have a root list containing two items, each of which opens up its own list with its own unique set of data, you can easily implement code that notifies the adapter of which item in the root list was selected. From there, the adapter can update and switch the view accordingly. This can be applied to series of lists ad nauseam if you so choose, though I can't say I would recommend this kind of method for incredibly complex webs of lists that interact with each other.
Like Ircover said in their comment, I don't think a tree is necessary in this situation either, if only because (1) as stated, it isn't really best practice to do so for the kind of application you're making, and (2) it may unnecessarily over-complicate whatever you're trying to achieve with these branching paths in the first place. If you're willing/able to provide more information about what you're trying to do here, that may help others help you more precisely than I can :)
Full disclosure here: the blog post linked above is not a direct match that will solve your problem - it pertains specifically to displaying different types of data sets (from data classes and what have you,) but employs code that shows how different sets of data can be switched between in a single RecyclerView. Even if it isn't a god-sent solution or is only halfway helpful in solving your problem, I think it can provide some useful information to you.
Maybe a slightly dated question but looking into the same concept and I located this page https://blog.usejournal.com/multi-level-expandable-recycler-view-e75cf1f4ac4b .
They have made a single adapter class to take care of all the navigation and so far seems to be the least complicated example of an expandable RecyclerView, though not in kotlin.

Some questions about Room and Paging Library

I am trying to set up all my lists with Room Persistance Library and Paging Library but I am facing some problems when implementing PagedListAdapter.
Question 1
I don't want to write any if, when... conditions in the onBindViewHolder so the scrolling is completely fluid. I have a model with its attributes. As an example, I want to set the visibility of a view that it is inside the layout (like a TextView) depending on a Boolean of the model, but I don't want to use an if. What would be the correct way of achieving that?
Should I create an Int attribute in the model which has the View.VISIBLE or View.GONE? But then the model can get very complex with lots of attributes and all of them are on all the model objects of the Room database.
Should I create another model which only has the attributes needed for the adapter UI? But then every time the real model is modified, I also have to modify the adapter model in order to see changes on the UI. And I think that's not good at all.
Do you know if there is somewhere where I can do this asynchronously in PagedListAdapter?
Question 2
I need to use functions like getString(R.string.resource), which requires a context. I also need to use Glide to load an image into an ImageView, but it requires an Activity context or Fragment context. I tried to inject it using dagger but that's not possible. It is safe to pass that context through the constructor? Or what is the best way of doing that?
(I suppose the same problem happens implementing RecyclerViewAdapter)
In my studied application I have table which save the current player state. This table I use in multiselect queries, so when I change this table, Paging library will change my DataSource. Example app.
I use Context instance from View instance with method View.getContext().

Activity fragment in expandable listview?

I'm looking for any examples or (even better) tutorials that can help me accomplish this. But I keep coming up short in my google searches, which has me worried that it's not even possible. So that's my first question: Can an expandable listview inflate a fragment when it expands and call it's onDestroy when retracted? and if so, can you please link me to any help with accomplishing this. Thanks
Here's a little visual of what I'm looking to do:
first, the general UI paradigm could be handled with an ExpandableListView. whether this gives you enough to match exactly what you need is TBD.
as for having fragments inside the list ... theoretically, in the list view's adapter's getView() method, you could do a FragmentTransaction.replace(), passing in a container ID that exists in the inflated view. i have not tried this.
that being said, i seriously doubt this will work. list views are smart about managing views. the views in the list are often destroyed and re-created as the user scrolls the list. adding fragments to a view is an expensive operation.
as an alternative, you might consider a ViewPager, which can hold fragments by design. it's not the UI you have spec'd above, but it's a common UI pattern used by many stock Google apps, so you know it's been tested.

Android: Best way to fill Activity with new data

i got the following "problem".
I want to have an activity thats shows me the standings of some teams at a specific gameday.
therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.
Whats the best way to create this activity?
assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml
assemble the layout during runtime, add ids, update via ids
assemble the layout during runtime. on update remove old views and create new ones
assemble the layout during runtime. on update restart the activity
just whant to know which one is the best. or is there any other way to achieve that
thx Cheetah
If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.
In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.
Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.

Refresh view display when underlying data changes

I have seen this question answered a few times in which it is suggested to use the method notifyDataSetChanged() from BaseAdapter.
Is there a way to refresh when your application does not use any adapters? I have a simple application where I use a few activities with preferences, and relative layouts with text views and buttons. At the moment I do not use any of the adapters like SimpleAdapter or ArrayAdapter or CursorAdapter. It seems like in my case I have to create one of them just to get to use notifyDataSetChanged()? There is no easier way for me?
Looks like (I may be mistaken, but it really looks like) you don't catch the purpose of those classes - SimpleAdapter, ArrayAdapter or CursorAdapter. They are expected to work with ListView inside of an Activity (or even better - inside of a ListActivity). If you don't use ListView then those adapers are most likely useless for you.
I assume you have your data persisted in some way (SharedPreferences or file). So if you start any of your Activities, then it just reads the data to populate the views. In this case nothing extra is needed. In case if you need to reload data for a currently visible Activity, then just reread the data from persistent storage and repopulate the views.
If your data is changing and you need to refresh your view, how is the data actually changing? Is it in a separate thread or, do you just want something to happen periodically (polling an RSS feed, or something)?
I had a similar problem. I had a radio group, and each radio button had a label. When the screen rotated, I used a different layout with a radio group & radio buttons with the same IDs. I was calling setContentView() in onCreate(): and after rotation, the “old” labels would show up on the new layout (bizarre). When I moved setContentView() to onResume, everything seems to update ok. Thanks to Arhimed for his answer above.

Categories

Resources