ListView doesn't update unless screen orientation is changed - java

I'm trying to make an app which displays a list of books using google book's API. I'm able to populate the list items, but unless the screen orientation is changed the list views do not show up.
How can I update the list views as soon the search button is clicked?
Here is the entire project on github.

The AsyncTask is asynchronous, so the extractBooks method is called before the request is done.
You should call this method in doInBackground and binds data in list in the onPostExecute method.

Related

Update RecyclerView From Different Tab In Viewpager

I have a ViewPager with 2 tabs. Each tab just holds a Fragment with a RecyclerView. The RecyclerView in the first tab holds a list of all Articles while the RecyclerView in the second tab only holds the list of user's favorite Articles. Each Article in the first tab has a button in the RecyclerView row that allows the user to favorite it. I want new favorites to be displayed automatically (without forcing the user to manually press the refresh button) when the user navigates to the second tab. How can I go about doing this? Essentially I need to add / remove items from the RecyclerView which contains the favorites, but from the first fragment.
Things I have tried
Call adapter.notifyDataSetChanged() in the onRefresh() method of the favorites fragment. This works, but the performance hit is big because notifyDataSetChanged() is not a cheap operation. I would like to avoid this if possible.
Tried following this guide, and implemented some interfaces. The problem is, when I try to reference the adapter in my fragment from the containing activity, the adapter is always null. I can post code if needed to show what I was doing if this is supposed to be possible.
Now I am considering using SQLite DB to store changes (favorite removals/additions) in the onClickListeners, and then checking that table in the onResume() method and processing the changes individually, one by one with notifyItemChanged()
Is there a better way to do this?
For SQlite use CursorLoader to load data from SQlite. You can register to be notified when a data changed in a table. Then your loader will automatically load data again. Check this answer. Also check this Thread.
For getting data from internet you can use
Communicate with 2 fragments via parent Activity. Check this and this answer. This tutorial is good for better understanding.
LocalBroadcastManager to notify from one fragment to another fragment.

"Nested" RecyclerViews

Hi everyone.
I've come up with an idea for an Android App, and I was thinking how to turn my thoughts into something working. I know how to program for Android even though I'm not that advanced as you might see, so I wanted some tips from you guys who I'm sure will be able to help.
Idea
I was thinking about an App to organize stuff, see your objects on a list and be able to add, move or remove them from the list.
Thoughts
I first thought I needed a RecyclerViewto display each item. Then I thought every item itself might be a box, and so be containing other items inside: this brought me to think of a sort of "nested" system of RecyclerViews. Before going too deep into this system I had to clarify each item should have been a Class, each of which should have had a RecyclerView assigned.
Question
I was wondering how I could make this "nested" system of RecyclerViews. I thought about making a RecyclerView an object, because I need each RecyclerView to display, function and be always the same in any screen of any item. But I don't know what the best way is.
Should I make it an object? How do I create a new RecyclerView through a button so the user can first tap an item and then, eventually, tap a button (inside the item details view for instance) to make it a box item and so create and open a RecyclerView when tapped?
P.S.
It may seem like multiple questions but it actually is only one: how to add and display a RecycerView when I tap a button inside a details screen of an item, of course automatically (have a reference to that RecyclerView).
You have to create the RecyclerView that holds your items list, let's call it rv_list, this RecyclerView has an adapter that takes each item and adapts it to an xml layout row_item.xml that you should define. Now to make an item itself display its own recyclerview you should put a recyclerview inside row_item.xml and populate it when adapting that item to rv_list inside the method onBindViewHolder.

How to allow click item during notifyDataSetChanged() in adapter

I use adapter in RecyclerView. Each item contains a ProgressBar. Since progress speed is sometimes very high, a fragment permanently call notifyDataSetChanged(), which initiates update adapter. But there is one unpleasant feature: at this time ViewHolder doesn't respond to a click and user can't open the item, while progress speed will not decrease. Is it possible to solve this problem?
notifyDataSetChanged actually redraws the list for you. So click on the list item at that instant may actually refer to the content which was available before you refreshed the adapter contents. Hence its a good thing that click isn't enabled. Having said that, the refresh usually happens so quick that the user won't see a visible lag. Hence please go through you getView() method in the adapter if there is an intense or time taking process in the adapter implementation. Hope this helps! :)

how to render view in android based on the data received from server?

I am new to android development. I am trying to build basic shopping cart. I want to know how can I render view based on the data (product details) received from server. Because I will make a call to server in AsyncTask and I will have to wait for the data and then only I will be able to render them. So, I couldn't be able to figure out how would I manage it? Any help is appreciated.
Use a broadcastreceiver in the activity that contains the view you want to render.
Look at this answer:
How to send data to another app which is not started
Render your view in the onReceive() function of your BroadcastReceiver.
As you said you're going to use AsyncTask, you can render any view you want in the onPostExecute() method, which runs on the UIThread.
I would display a progressbar before you execute the AsyncTask and then AsyncTask's onPostExecute() method you would hide the progressbar and update the views with the requested data.

AsyncTask updates data after fragment is shown

I have fragment that displays data about user downloaded from web service. I want to download that data after the fragment is shown, just like some applications do. While there's no data avaliable, I would like to use either some default value or ProgressBar. What is the proper way to do this? Should I call asyncTask in onCreate or onCreateView method? (since it's not clear if this fragment will be ever shown)
I would do it in the on create method. You want the dialog to begin in the doInBackground method of the Async task and hide the dialog in the on post execute method where you update your view depending on what information you get back.

Categories

Resources