0

My list consist of one static and all other dynamic item.Each item consist of image and two textviews.The static item is added as last item of list.I am facing following issues.

1.I have set the default images for all dynamic list items same and default image of static list item different.But the image displayed for static list item is same as that of dynamic list items. Inside adapter i have used following code-

     if (i == getCount()-1)
       {
           albumPhotosCount.setVisibility(View.INVISIBLE);
           albumName.setText("Create Album");
           LoadImage.loadImage("xyz", albumImage, R.drawable.create_album);
       }


 else{
 LoadImage.loadImage(uri.toString(), albumImage, R.drawable.emptyalbum);
..}

However the albumname shows correct text for last item and albumPhotosCount is invisible as expected.But imageview of last static item is showing image "R.drawable.emptyalbum" while it should show image "R.drawable.create_album" and the image "R.drawable.create_album" sometimes appear in third item which is dynamic while my lit consists of 15 items and it should appear in 15th item(last item).

2.The value which is coming from server that textview is dislaying is of next item for dynamic list items except the first item which is displaying correct value.i.e.textview of item 2 displays value that should be displayed by item 3 and item 3 displays value that should be displayed by item 4 and so on.
Also in first list item image fetched from server is displayed while it is not displayed for rest of the list items.

This is how I am adding static item

Invites createalbum;
 createalbum=new Invites();
  ArrayList<Invites> albumObjects;
    private InvitesListingAdapter invitesListingAdapter;
 albumObjects = (ArrayList<Invites>) response;

                        albumObjects.add(createalbum);

                    invitesListingAdapter = new InvitesListingAdapter(getActivity(), albumObjects);
                   mAlbumListingView.setAdapter(invitesListingAdapter);

Adapter class

public class InvitesListingAdapter extends BaseAdapter {

private List < Invites > mAlbumList;

private Context mContext;

public InvitesListingAdapter(Context context, List < Invites > albums) {
    mContext = context;
    mAlbumList = albums;
}

@Override
public int getCount() {
    Log.e("count", "c" + mAlbumList.size());
    return mAlbumList.size();

}

@Override
public Object getItem(int i) {

    return mAlbumList.get(i);
}

@Override
public long getItemId(int i) {

    return i;
}

@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
    if (convertView == null) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(R.layout.item_album_listing, null);
    }
    NetworkImageView albumImage = (NetworkImageView) convertView.findViewById(R.id.album_thumbnail);
    TextView albumName = (TextView) convertView.findViewById(R.id.album_name);
    TextView albumPhotosCount = (TextView) convertView.findViewById(R.id.album_photos_number);

    if (i == getCount() - 1) {
        albumPhotosCount.setVisibility(View.INVISIBLE);
        albumName.setText("Create Album");
        LoadImage.loadImage("xyz", albumImage, R.drawable.create_album);
    } else {
        albumPhotosCount.setVisibility(View.VISIBLE);
        Uri uri;
        if (mAlbumList.get(i).getnPics() != 0) {
            uri = Uri.parse(mAlbumList.get(i).getCoverPhotoUrl());

            if (uri.getHost() == null) {
                byte[] decodedImage = Base64.decode(uri.toString(), Base64.URL_SAFE);

                albumImage.setImageBitmap(BitmapFactory.decodeByteArray(decodedImage,
                0, decodedImage.length));
            } else {
                Toast.makeText(mContext, uri.toString(), Toast.LENGTH_LONG).show();

                LoadImage.loadImage(uri.toString(), albumImage, R.drawable.emptyalbum);


            }
        } else {


            LoadImage.loadImage("xyz", albumImage, R.drawable.emptyalbum);
        }

        albumName.setVisibility(View.VISIBLE);

        albumName.setText(mAlbumList.get(i).getName());
        albumPhotosCount.setText(mAlbumList.get(i).getnPics() + " Photos");
    }

    return convertView;
}

}

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • Are you using the ImageLoader Library ? – Shivam Verma Jan 15 '15 at 12:36
  • @ShivamVerma yes I am using ImageLoader library.Also in first list item image fetched from server is displayed while it is not displayed for rest of the list items. – Android Developer Jan 15 '15 at 12:37
  • I think rather than using `loadImage()` you should be using `displayImage()` method. Check this : https://github.com/nostra13/Android-Universal-Image-Loader#simple – Shivam Verma Jan 15 '15 at 12:39
  • @ShivamVerma However i have used loadImage() for rest of the project and it is working fine..only i am having problem in listview with one static and other dynamic items.. – Android Developer Jan 15 '15 at 12:40
  • 1
    Also, since you always need the last item to have a different view, you should use a footer. Check this out : http://stackoverflow.com/questions/4265228/how-to-add-a-footer-in-listview – Shivam Verma Jan 15 '15 at 12:44
  • if you need to display the static item always as the last item you can use `ListViewaddFooterView()` as it will make this easier for you. – SMR Jan 15 '15 at 12:44
  • In first list item textview shows correct value and image fetched from server is displayed.For rest of list items default image is displayed and textview shows value that should be displayed in its next item's textview.And for last list item which is static and for which i have used separate default image it displayes default image of other list items – Android Developer Jan 15 '15 at 12:44
  • @ShivamVerma Actually i am displaying the list in gridview – Android Developer Jan 15 '15 at 12:45
  • Since you already have the drawable, why not just use `albumImage.setImageResource(R.drawable.create_album)` – Shivam Verma Jan 15 '15 at 12:57
  • @ShivamVerma I tried that but it doedn't work..As I am using NetworkImageView of Volley library probably because of that it doesn't woth..loadImage() is working fine elsewhere..problem is something else.. – Android Developer Jan 15 '15 at 12:59
  • @ShivamVerma I failed to understand Why is textview of all items giving value that should be displayed in its next item's textview except the first item..and also the image of first item is displayed correctly fetched from server and for rest of items default image is displayed – Android Developer Jan 15 '15 at 13:02
  • 1
    Okay, so first of all, The recycling mechanism that you've implemented is incorrect, if you're seeing random data here and there it's because of that. You should either get rid of this part `if (convertView == null) { LayoutInflater layoutInflater = LayoutInflater.from(mContext); convertView = layoutInflater.inflate(R.layout.item_album_listing, null); }` completely or implement the recycling mechanism properly. – Shivam Verma Jan 15 '15 at 13:19
  • @ShivamVerma Thanku for ur time..can u write in answer what code changes should i do? – Android Developer Jan 15 '15 at 13:46
  • I'll add a small sample as to how you should be implementing recycling. – Shivam Verma Jan 15 '15 at 13:47
  • So, I am guessing it worked ? – Shivam Verma Jan 16 '15 at 05:23
  • Actually the first issue is resolved..the first part of second issue that text view showing wrong value is because the value coming from server side is wrong...its server side problem..the second part of second issue that image fetched from server is not displayed for most of the items is still not resolved.. – Android Developer Jan 16 '15 at 05:33

1 Answers1

1

Create a private class inside your adapter. This class should contain all the views which your list item has.

private static class ViewHolder {
    NetworkImageView albumImage;
    TextView albumName;
    TextView albumPhotosCount;
}

And then inside getView() :

ViewHolder viewHolder;
if (convertView == null) {
    viewHolder = new ViewHolder();
    LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    convertView = layoutInflater.inflate(R.layout.item_album_listing, null);
    viewHolder.albumImage = (NetworkImageView) convertView.findViewById(R.id.album_thumbnail);
    viewHolder.albumName = (TextView) convertView.findViewById(R.id.album_name);
    viewHolder.albumPhotosCount = (TextView) convertView.findViewById(R.id.album_photos_number);
    convertView.setTag(viewHolder);
} else {
    //This means that the view has already been inflated once 
    //so you do not need to inflate it again, just use 
    //the references from the last time. 
    viewHolder = convertView.getTag();
}

//Now this is where you want to work on your views. 
viewHolder.albumPhotosCount.setVisibility(View.VISIBLE);
...
...
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • i'll try tht and let u know – Android Developer Jan 15 '15 at 14:05
  • okay..thanks..one issue is resolved..Now the last item of list which is static shows correct default image..but other issue is still there.. textview of all items giving value that should be displayed in its next item's textview except the first item..and also the image of first item is displayed correctly fetched from server and for rest of items default image is displayed..+1 for ur answer – Android Developer Jan 15 '15 at 14:21
  • I don't see anything wrong now. The only thing wrong could be the logic so, try to log the data that is being set in each list item. See if your mAlbumList has the correct data etc. – Shivam Verma Jan 15 '15 at 14:30