D4rWiNS
D4rWiNS

Reputation: 2615

Add differents items to custom list adapter

I have a List view with a custom adapter (imageviews), in the same activity I have a header and footer, the listView is between those.

What I want is to add a button as the last item of the list view so when you arrive to the last item it appear, i cant add the button outside because it wont scroll

Sorry about my english

Regards

Upvotes: 0

Views: 143

Answers (2)

Tim Strehlow
Tim Strehlow

Reputation: 188

If you already have a custom adapter class implemented, the solution is rather simple. Based on an xml layout implemented for your list-view rows which contains both a Button and an ImageView, you can hide/display them in the adapter's getView() method based on the index. This is a code sample, which I currently don't have the chance to test and might not be the most efficient solution, but it should give you an idea:

class CustomAdapter extends SimpleAdapter {
    [...]

    @Override
    public int getCount() {
        // number of images to be displayed + 1 for the button
        return images.length + 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View row = inflater.inflate(R.layout.row, parent, false);
        final ImageView imageView = (ImageView) row.findViewById(R.id.image);
        final Button button = (Button) row.findViewById(R.id.button);

        if (position == getCount() - 1) {
            // The last element
            imageView.setVisibility(View.GONE);
            // set an OnClickListener on the button or whatever...
        } else {
            button.setVisibility(View.GONE);
            // do your regular ImageView handling...
        }

        return row;
    }
}

Upvotes: 1

Tomas Žemaitis
Tomas Žemaitis

Reputation: 1907

use ListView.addFooterView() to add view as a footer which is visible only in the end of the list:

http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

Upvotes: 1

Related Questions