Display a gridview with content from an api oncreate - java

I want to display set the content of gridview to be as gotten from an api.
I used Ion to call in my api and saved them into a generic list of a class i.e(List list;) i used picasso to load images from a url but when set the gridviews adapter in the oncreate it doesn't show the items in the gridview when the app comes up, but when I minimise and bring it back the contents show. I need help making sure it's contents loads up with the app.

Related

Custom listview for image and text, or only text

I'm trying to build an app of my website(Just new to programming Java), getting data from my site is all going fine, but now i need to display it. I did some research online for the best way to do it(CustomListView), but coundnt find the solution yet, hope you guys can help me out.
The site i'm making an app of, is just a site where people can post text, or text with a photo.
Getting the text in my listview is going fine, but i also need images to display.
The problem is, how can i tell the Listview that it only contains text, so it wont display any image field, and how can i tell the ListView when there is a photo found by the post,that it also should display that one?
You can use a tag if the post contains an image or not for example if the data is in JSON form just set a tag
[{contains_image:"null"},{contains_image:"yes"}]
Then in your adapter check this tag if the result is a null set visibility gone
You have to create a layout containing a listView.
You have to create an xml lyout corresponding to one row of your list view.
You have to create an adapter which will populate data to insert in your listView
You have to create an onClickListener on your button to add data in your list
If you are using an ArrayAdapter or a CursorAdapter, add the new item you created to the list or the cursor used by your adapter and (notifyDataSetChanged() is automatically called), so your adapter will update the listview
Source : http://developer.android.com/resources/tutorials/views/hello-listview.html
Have at look at this article : http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/
Once you are go through with above set placeholder image which will be show for the items that do no get dynamic images from your server response.

Where to do network calls to download JSON for RecyclerView

I am writing a small app which queries a REST API to get JSON Data about movies. Which includes
Movie ID
Movie's Poster
URL Movie Overview etc
My main view consists of a RecyclerView which uses a GridLayout Manager to display a grid of movie posters. On tapping, a grid view item would transition to an activity where details of the movie will be presented.
As mentioned above, the API Call would return all important details to build the experience, including the URL to download the Poster.
I will be using Okhttp and Picasso for networking and Image Download. This is my first app which interacts with the network so I need to know where should I put the Okhttp Async Networking Code to get the JSON Data and WHY?
In the Fragment's OnCreate?
In the hosting Activity's OnCreate?
In the RecyclerView's Adapter?
I don't want the code, I just need to know what's the right place to fetch the JSON via Okhttp and why?
You can fetch the JSON via Okhttp in your main fragment or activity, and parse it and store it in an ArrayList, then pass the ArrayList in adapter class, where you need to use picasso library for image download.

how to make images redrawn on imageView inside ListView

I have a ListView that is like an inbox. The information comes from a user,so the ListView displays text and a userPicture.To display userPictures, I display the userPicture if exists or a imageView of a letter of the first name.
when I receive data from WS I launch an asynctask that compare userPicture ids with DB to ask the WS for ones I don't have or one's that have changed.
After the asyncTask finishes I save pictures locally and notify listView that the data set has changed, in order to look for the new pictures.
my problem comes when there is only 1 user on the listview and the picture wasn't found but was downloaded from WS. after calling notifyDataSetChanged, also calling refreshDrawableState and invalidateViews,that acording to documentation "Causes all the views to be rebuilt and redrawn". the image is not changed and imageView still shows the letter ImageView,
neither scrolling through the listView its updated. (I guess is part of the Viewholder that recycle the views that is meant for performance and a good practive acording to ).
on the getView of the adapter I have a the Following statement to processThe Picture
if(mIDataPersistence.userHasPicture(userId)
ViewHolder.userImg.setImageBitmap(mIDataPersistence.getUserPicture(userId));
else
ViewHolder.userImg.setImageBitmap(BitmapUtils.createBitMap(userName);
note: mIDataPersistence is persistence data interface to access all the data locally stored that I'm using here. and BitmapUtils.createBitMap() makes a bitmap from the first letter of the user.
can anyone help me or point me on a way to make listView update images in this particular case.

Android loading images to ListView AsyncTask

This is more a question of application logic and performance.
Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.
The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.
This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.
Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?
Everytime I go this route the application crashes...
You can use Picasso library for this task.
Visit http://square.github.io/picasso/
In the adapter of your ListView, in the method getView, you can put this:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) .centerInside()
.into(imgView);
You dont need a AsyncTask because this library already implements itself.

Display Images in ImageAdapter (GridView) while downloading

I'm making an application that reads a bunch of images URL on a JSON string. With a new thread the app downloads the images to a specific directory. Then there is a "gallery" based on a GridView with an ImageAdapter that displays all the files. The thing is that it takes too long the first time to download all the images.
My goal is to display the images while they are downloading. Right now the user has to go back and then enter again in the gallery to view the new images. Until all the images are downloaded.
If someone has a workaround for this, let me know.
Thanks..
Once your images are downloaded call notifydatasetchanged on the adapter. This can be done either after every image or a set of images.
Edit : Assuming you are calling the thread in MainActivity and the adapter is being called in the Display Activity , you can have the downloading thread call a listener, defined in the Display activity which calls notifydatasetchanged.
Also I am hoping each image is downloaded in a separate thread :-)

Categories