how to make images redrawn on imageView inside ListView - java

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.

Related

How to manage memory with too many items on android recycleview

I am developing an android social media type application, where I have a multiple tab activity. The tabs are fragments using viewpager.I have a home screen with a recycler view. The post have images on them. I am having memory problem with the recyclerview for post. I am using firebase as a database if thats helpful. To load the imageview I am using glide and I am doing diskcache and skipping the memory caches.
Issues I am having are
When too many posts are loaded, recyclerview becomes slow and laggy (I know that there are other post with this but couldnt really found anything helpful).
When I click on a username from the post, a new profile activity starts which also have all the posts from that user, I am trying to find a way clear the homepage tab recyclerview memory but struggleing with how to.
To solve the first problem, I have set up an onscrolllistner that loads 10 new post everytime you are near the end of the post list. The recycler view adapter currently loads 20 items initially and then so on.but I am not sure how to clear the old posts in an efficient way since I want the user to be able to scroll up.
To solve the second problem, In the homescreen fragment, when I launch the new activity, the onPause() function gets called. In the onpause() function I tried setting the list to empty, recyclerview adapter to null, recylerview to null but nothing has worked. In the android profiler tool, I see that I am using 200 mb memory, and when I start the new activity, it adds another 100 mb or so. I tried manual garbage collections but it doesnt work either. To be specific, native memory jumps up in the profiler. Ive tested and made sure that was only because of the images in the recyclerview.
I am not sure why when I set recyclerview, adapter and the post list to null, why the images from posts are still being held in the memory.
Glide caches the images
To load multiple post check paging library
When moving to detail fragment and clearing the reacyclerview of home fragment , I would not recommend this as next time user switches to home tab ,the user will not have previous state saved which he left previously.

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.

Start ViewPager from RecyclerView item

Basically what the title says. I'm trying to start a ViewPager activity when a user clicks a RecyclerView item. I'm not having problems here, that works fine. The goal is to go from a grid based gallery to viewpager gallery. I'm also trying to send the List<> of data which is loaded over the network. This is where I'm having a lot of trouble. The data is sent through a singleton because it's too large to send through intent and if opened in the new activity too quickly upon app startup, more data is loaded in the original activity (Even if Call is cancelled, using Retrofit2 btw) and I need to notify the adapter, which I have a listener that does.
Another crash that is common is a null pointer when reading an item in viewpager activity, which doesn't make sense to me because the item is not null in the original activity before sending, but if I wait a few seconds it is not null. Some of the code pertaining to my issue can be found here, a question I asked yesterday with a solution I could not successfully implement.
Please point me in the right direction for starting a ViewPager of the same dataset from a RecyclerView, thanks
When you are "sending" feed data to the view pager activity, use a shallow copy of the list instead of directly referencing it as shown below
DataTransferer.get().storeItems(new ArrayList(feed));
This should prevent IllegalStateException raised by feed being updated on your RecyclerView activity.

Bug RecyclerView.scrollToPosition(list.size()-1) in Dialog

I have created RecyclerView with list of clients (from 0 till 14). I need maximum down state. For this purpose I am using method RecyclerView.scrollToPosition(14).
When RecyclerView are placing in Activity, I have the good result.
IMAGE: RecyclerView are placing in Activity
And now I am inflating new RecyclerView and put it in Dialog. The Adapter of RecyclerView the same. Method RecyclerView.scrollToPosition(14) give me the bad result (scroll is not set on the 14th position, by the way I can pull it to the down manually):
IMAGE: Bad result in Dialog
Why it happens? I think the problem in parent (Dialog) of RecyclerView. But I don't know what to do.
UPDATED! I think this is a really bug of RecyclerView. Because with ListView is all right (in my case with setSelection(14)).
Because your interaction panel hide the last element. Position your recycler view above the panel.

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.

Categories