For some reason, my first image displays correctly, then gets overwrriten with another user's image. Any ideas:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if( convertView == null ){
vi = inflater.inflate(R.layout.feed_item, null);
holder=new ViewHolder();
holder.userImage = (ImageView) vi.findViewById(R.id.feed_userimage);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}
if(user.has("image") &&
user.getString("image") != null &&
!user.getString("image").equals("null")) {
holder.userImage.setTag(user.getString("image"));
imageLoader.DisplayImage(user.getString("image"), act, holder.userImage,USER_IMAGE_SIZE);
} else {
holder.userImage.setImageDrawable(null);
}
Try this fix
if(user.has("image") &&
user.getString("image") != null &&
!user.getString("image").equals("null")) {
holder.userImage.setTag(user.getString("image"));
imageLoader.DisplayImage(user.getString("image"), act, holder.userImage,USER_IMAGE_SIZE);
} else {
holder.userImage.setTag(null);//add this line
holder.userImage.setImageDrawable(null);
}
It is happenning because you are using convertView. The convertView passed to the getView method is essentially a view object you had created (by inflating) in a previous getView call (which is no more be needed because it is not visible anymore, due to scrolling).
You are assuming that every time getView is called a new view is created, whereas you are actually using previously created views. The convertView is passed as an optimisation so that too many views does not have to be created when only a few are visible.
So the setTag call actually overwrites a previously created view's tag.
You should reconsider rewriting your code without using tags. Or, you could always inflate the view, instead of only inflating when the convertView is not null. But I wouldn't reccommend this approach as for a long list it would mean too many unnecessary views in the memory.
Related
I have an Adapter with ViewHolder. Let's say I want to set data only once to the ViewHoler inside the getView() method, what's actually happened is, its set the data every time I'm scrolling the view.
How can I check if the ViewHolder was already init or not?
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Deal deal = dealsArr.get(position);
holder.textView.setText(deal.getPrice);
// Here is my issue. how can I do it only once?
changePrice(deal, true);
}
I asssume you already have the basic understanding of Android Adapters & working of getCount() & getView() if not see this
Either adapter returns a null View or an Inflated View; findViewbyId(R.id.xmlID) is always executed;
ViewHolder is used to avoid frequent call and ensure the smooth scrolling of your listview.
Excellent Explanation here
First spend sometime of understand Listview's recycling mechanism!
The link I had shared also has a link to that.!
public View getView(final int position, View convertView, ViewGroup parent) {
//rest of the code goes here
}
focus on the parameters of this method,
the convertViewwhich is for recycling will be null initially because there is no recycling done yet
so this part of the code will be executed;
if (convertView == null) { //convertView is null when recycling isn't done
holder = new ViewHolder();
convertView.setTag(holder);
}
so we initialize the view-holder & refer all our Views(Buttons, TextViews etc) using findview by id.
the next time getView() is called; it executes this part of the code;
else {
holder = (ViewHolder) convertView.getTag();
}
I'd strongly recommend to first understand working of Android Adapters & List View Recyling Mechanism without using ViewHolders in your code. first you understand how & when the methods are invoked then use ViewHolder for more optimization
I have a question that after searching SO and Google I can't find an answer to. When working with an adapter in Android its good practice to reuse the list item view by using the convertView parameter in the getView() method. My question is this, if I make a change to convertView, will that change persist to future calls to getView()?
For example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
ViewHolder holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
convertView.setTag(holder);
// if i call this method here, will all future views passed into convertView
// also have this set??
// From what I know about Java and objects I would guess yes
// but I'm not 100% sure how Android processes the convertView behind the scenes
((ViewGroup) convertView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
ViewHolder holder = (ViewHolder) convertView.getTag();
// currently setDescendantFocusability is called here,
// I want to move it to where it above to help improve performance
return convertView;
}
I think so.
The converted views you operate will be reused in future.
When you get a converted view passed by getView() method, the specific converted view might have been used before, so make sure to update all the attributes which might be dirty for current converted view.
Should be kind of below code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
view = createSpecificView();
}
updateSpecificView(view);//update all attributes here.
return view;
}
Hopefully can help you.
The ONLY reason you need to return the convertView at the end of getView() is for the situation where it was null and/or you create a new instance. When an object is passed as a parameter, you can modify the underlying object's state but cannot change or create a new object. You can't change which object convertView points to.
So, you also return the convertView at the end of the method. This covers the situation where you create a new object.
I have a ListView that I put through a complex bit of coding. The list changes often with different types of data that require different views. On rare occasion, I'll end up with 1 view being reused by Android for a row that's supposed to look different. It seems to only happen when the data being displayed radically changes. I was hoping there was a way to programmatically wipe the ListView's memory clean. Is this possible?
Here is the beginning of my getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchHolder holder = null;
int type = getItemViewType(position);
if (null == convertView) {
holder = new SearchHolder();
if (type == SEARCH_TYPE_FREETEXT) {
convertView = mInflater.inflate(R.layout.layout_search_item_freetext, null);
holder.txtText = (TextView) convertView.findViewById(R.id.search_itemname);
holder.vHeaderWrapper = (LinearLayout) convertView.findViewById(R.id.search_headerwrapper);
holder.txtHeader = (TextView) convertView.findViewById(R.id.search_header);
}
else {
if (items.get(position).mData == null) {
convertView = mInflater.inflate(R.layout.layout_loadmoreresults_white, null);
}
else {
convertView = mInflater.inflate(R.layout.layout_search_item, null);
holder.txtText = (TextView) convertView.findViewById(R.id.search_itemname);
holder.vHeaderWrapper = (LinearLayout) convertView.findViewById(R.id.search_headerwrapper);
holder.txtHeader = (TextView) convertView.findViewById(R.id.search_header);
}
}
convertView.setTag(holder);
}
else {
holder = (SearchHolder)convertView.getTag();
}
...
ListView does something called "recycling" when you scroll through a list, and what you will need to do is override the getView() method to update the individual listView item that is being recycled. By default android does not clear out these views. Check out the following link on ListView recycling:
http://mobile.cs.fsu.edu/the-nuance-of-android-listview-recycling-for-n00bs/
Without code it is hard to tell if you already know about this or not, but this is the cause of such problems in my experience.
If you use more than one layout for your list items then I suggest to inflate the appropriate layout from the xml every time in the getView() method.
I have a question, maybe a silly one, but I think it is important.
Why the parameter: convertView (View) on the
public View getView(int position, View convertView, ViewGroup parent)
is always null? android is supposed to recycle the views once they're created the first time, isn't it? or how can I do to recycle those views?
I feel like the method receives those 3 parameters, but in none of the google examples they use either of them.
Unfortunately, convertView will always be null, due to Android bug 3376. Gallery does not implement View recycling (at least as of Gingerbread/2.3.4).
A commenter in the bug suggests forking Gallery.java (from AOSP) and implementing it yourself, which may be the best option.
The convertView parameter indeed will be null a few first times, when this function will be called. Then if you scroll the list / galery Android will give you the same view, which was constructed earlier using this function, and you should use it to optimally construct the new view, based on the old one.
Also, you should store the references to child view somewhere.
To better undestand that, look at this code example (taken from Android Developers):
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
On getView() method normally you check if convertView is null, and if it isn't you just rewrite the fields in the View adapting it to the data you get based on the position instead of creating a new View (from inflation or whatever method you want).
Hope it helped,
JQCorreia
getView() has a second parameter as view(convertView).This convertView is the view which is returned from previous iteration.For the first iteration it will be null and adapter will create (instance) view.When it is done with creating required layout,the view will be returned to its caller.This returned value will be available as 2nd parameter from the next iteration onwards.So one can decide to reuse previously returned view instead of recreating by looking at this parameter.Thus Android achives re-usability feature while creating multiple list items.
Since convertView will always be null you should implement your own algorithm of caching and reusing items.
This is my implementation of Gallery adapter
public View getView(int position, View convertView, ViewGroup parent) {
int arrPosition = position % VIEW_CHACHE_SIZE;
ImageView imageView;
mCursor.moveToPosition(position);
if (parent.getHeight() > 0 && layoutParams.height == 0) {
layoutParams = new Gallery.LayoutParams(parent.getWidth() / VISIBLE_IMAGES_COUNT, (int) (parent.getHeight() * IMAGE_HEIGHT_COEFICIENT));
viewsList[0].setLayoutParams(layoutParams);
}
if (convertView != null) {
Log.i("GALLERY", "convert view not null");
}
// check views cache
if (viewsList[arrPosition] == null) {
imageView = new ImageView(mContext);
imageView.setPadding(3, 3, 3, 3);
viewsList[arrPosition] = imageView;
} else {
imageView = viewsList[arrPosition];
if (position == arrPosition) {
if (imageView.getDrawable().equals(imagesList.get(position))) {
return imageView;
}
}
}
// check images cache
if (imagesList.get(position) != null) {
imageView.setImageDrawable(imagesList.get(position));
} else {
byte[] photo = mCursor.getBlob(mCursor.getColumnIndex(DataProxy.PHOTO_COLUMN));
imagesList.put(position, new BitmapDrawable(BitmapFactory.decodeByteArray(photo, 0, photo.length)));
imageView.setImageDrawable(imagesList.get(position));
}
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(layoutParams);
return imageView;
}
.........................................................
private SparseArray<Drawable> imagesList = new SparseArray<Drawable>();
private ImageView[] viewsList = new ImageView[VIEW_CHACHE_SIZE];
private Gallery.LayoutParams layoutParams = new LayoutParams(0, 0);
private static final int VIEW_CHACHE_SIZE = 4;
I have a ListView in a custom ArrayAdapter that displays an icon ImageView and a TextView in each row. When I make the list long enough to let you scroll through it, the order starts out right, but when I start to scroll down, some of the earlier entries start re-appearing. If I scroll back up, the old order changes. Doing this repeatedly eventually causes the entire list order to be seemingly random. So scrolling the list is either causing the child order to change, or the drawing is not refreshing correctly.
What could cause something like this to happen? I need the order the items are displayed to the user to be the same order they are added to the ArrayList, or at LEAST to remain in one static order. If I need to provide more detailed information, please let me know. Any help is appreciated. Thanks.
I was having similar issues, but when clicking an item in the custom list, the items on the screen would reverse in sequence. If I clicked again, they'd reverse back to where they were originally.
After reading this, I checked my code where I overload the getView method. I was getting the view from the convertedView, and if it was null, that's when I'd build my stuff. However, after placing a breakpoint, I found that it was calling this method on every click and on subsequent clicks, the convertedView was not null therefore the items weren't being set.
Here is an example of what it was:
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listitemrow, null);
RssItem rssItem = (RssItem) super.getItem(position);
if (rssItem != null)
{
TextView title = (TextView) view.findViewById(R.id.rowtitle);
if (title != null)
{
title.setText(rssItem.getTitle());
}
}
}
return view;
}
The subtle change is moving the close brace for the null check on the view to just after inflating:
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listitemrow, null);
}
RssItem rssItem = (RssItem) super.getItem(position);
if (rssItem != null)
{
TextView title = (TextView) view.findViewById(R.id.rowtitle);
if (title != null)
{
title.setText(rssItem.getTitle());
}
}
return view;
}
I hope this helps others who experience this same problem.
To further clarify the answer of farcats below in more general way, here is my explanation:
The vi.inflate operation (needed here for parsing of the layout of a row from XML and creating the appropriate View object) is wrapped by an if (view == null) statement for efficiency, so the inflation of the same object will not happen again and again every time it pops into view.
HOWEVER, the other parts of the getView method are used to set other parameters and therefore should NOT be included within the if (view == null) statement.
Similarily, in other common implementation of this method, some textView, ImageView or ImageButton elements need to be populated by values from the list[position], using findViewById and after that .setText or .setImageBitmap operations.
These operations must come after both creating a view from scratch by inflation and getting an existing view if not null.
Another good example where this solution is applied for BaseAdapter appears in BaseAdapter causing ListView to go out of order when scrolled
The ListView reuses view objects when you scroll. Are you overriding the getView method? You need to make sure you set each property for every view, don't assume that it will remember what you had before. If you post that method, someone can probably point you at the part that is incorrect.
I have a ListView, AdapterView and a View (search_options) that contains EditText and 3 Spinners. ListView items are multiple copies of (search_options) layout, where user can add more options in ListView then click search to send sql query built according to users options.
I found that convertView mixing indecies so I added a global list (myViews) in activity and passed it to ArrayAdapter. Then in ArrayAdapter (getView) I add every newly added view to it (myViews).
Also on getView instead of checking if convertView is null, I check if the global list (myViews) has a view on the selected (position).. It totally solved problems after losing 3 days reading the internet!!
1- on Activity add this:
Map<Integer, View> myViews = new HashMap<>();
and then pass it to ArrayAdapter using adapter constructor.
mSOAdapter = new SearchOptionsAdapter(getActivity(), resultStrs, myViews);
2- on getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
if (!myViews.containsKey(position)) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.search_options, parent, false);
/// ...... YOUR CODE
myViews.put(position, view);
FontUtils.setCustomFontsIn(view, getContext().getAssets());
}else {
view = myViews.get(position);
}
return view;
}
Finally no more mixing items...