Cameron
Cameron

Reputation: 3108

How to create a different view for the last item of an ArrayAdapter?

I am retrieving a list of items from a URL and displaying them in a ListView using a customized ArrayAdapter. I display at most 25 results, but retrieve up to 26 from the URL because I want to display a "next" link if there are more than 25. I am trying to figure out a way to make the last item in the view just display the next link instead of the image and two TextViews that the other items display. Scrolling down, all the items show up the way they are supposed to, but the last item continues to look like the rest. When scrolling back up, every fifth item is blank (can tell it is still there because of the separating lines).

Inside getView:

if (position + 1 == limit) { // Limit = 26, corresponding position is 25
    Log.e("Last Item", String.valueOf(position));
    // Gone
    image.setVisibility(8);
    text.setVisibility(8);

    // Visible
    next.setVisibility(0);
} else {
    if (item != null) {

        Log.e("Other item", String.valueOf(position));
        if (top_text != null) {
            top_text.setText(item.getItemTitle(), BufferType.SPANNABLE);
        }
        if(bottom_text != null){
            bottom_text.setText(item.getItemDescription());
        }
    }
}
return view;

In ddms, it is printing the "Last Item" tag only for the last item, and the "Other Item" tag for the rest, but the items aren't being changed based off the if-else. In the XML, next is set to gone so it doesn't need changed in the else. When I remove the if-else and leave only the code within the else, the list functions properly but I need the next link. Any ideas for the cause of the odd behavior? Please let me know if you need to see any additional code or need clarification on what is happening.

Upvotes: 0

Views: 1511

Answers (1)

merrymenvn
merrymenvn

Reputation: 898

Please post full code of getView. You return view; but what is view, is it convertView or inflated from xml?

Here is way how i get different view for last item: 1. create 2 layout, the first for normal items, the sencond for last item 2. in getView(int position, View convertView, ViewGroup parent)

View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) mContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.xxx, null);
} else {
    //check if view is not layout we need (based on position), we need inflate from xml
}

//set text ...

Upvotes: 1

Related Questions