Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:53:13+00:00 2026-06-16T11:53:13+00:00

Problem occurs when I try to reuse views in ListView having several custom layouts.

  • 0

Problem occurs when I try to reuse views in ListView having several custom layouts. When I change number of items I get wrong convertView being passed to getView method. In details:
I have a header layout, a row layout and a footer layout. In the header I can change number of rows. Example for 1 row:
Header
row1
Footer
When I reuse views I get wrong result (after adding another row). This happens because previously created footer view is passed for position = 2 and since it is not null I don’t recreate it:
Header
row1
Footer
Footer

Instead of expected:
Header
row1
row2
Footer

What am I doing wrong? Or is it by design and I just should recreate views instead of reusing them?
Here goes my code:

public class NewProgramAdapter extends BBBaseAdapter {

public static int TYPE_HEADER = 1;
public static int TYPE_FORM = 2;
public static int TYPE_FOOTER = 3;

protected String mDays;
protected String mProgramId;
protected int currentDay = 0;
protected int numberOfExercises = 1;
protected JSONArray items = new JSONArray();

public NewProgramAdapter(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    return numberOfExercises + 2; // header and footer
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (position > 0 && position < getCount() - 1) {
        return getFormView(position, convertView, parent);
    } else if (position == 0) {
        return getHeaderView(position, convertView, parent);
    } else {
        return getFooterView(position, convertView, parent);
    }
}

private View getFooterView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = mA.getLayoutInflater();
        v = inflater.inflate(R.layout.new_exercise_footer, null);
    }
    return v;
}

private View getHeaderView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = mA.getLayoutInflater();
        v = inflater.inflate(R.layout.new_exercise_header, null);
        Spinner sp = (Spinner) v.findViewById(R.id.spinnerExercises);
        sp.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView,
                    View selectedItemView, int position, long id) {
                int n = position + 1;
                setNumberOfExercises(n);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });
    }
    return v;
}

private View getFormView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = mA.getLayoutInflater();
        v = inflater.inflate(R.layout.new_exercise_form, null);
    }
    return v;
}

@Override
public int getItemViewType(int position) {
    if (position > 0 && position < getCount() - 1) {
        return TYPE_FORM;
    } else if (position == 0) {
        return TYPE_HEADER;
    } else {
        return TYPE_FOOTER;
    }
}

@Override
public void updateEntries(Object data) {
    items = (JSONArray) data;
    notifyDataSetChanged();
}

public void next() {
    currentDay++;
    notifyDataSetChanged();
}

public void setNumberOfExercises(int n) {
    numberOfExercises = n;
    notifyDataSetChanged();
}

public void setDays(String string) {
    mDays = string;
}

public void setProgramId(String string) {
    mProgramId = string;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:53:15+00:00Added an answer on June 16, 2026 at 11:53 am

    It looks like you forgot to override getViewTypeCount() which tells the Adapter to use more than one row layout.

    However if you only want a header, footer, and row layout then you should use ListView#addHeaderView() and ListView#addFooterView(). Don’t try to control the headers and footers is your Adapter, the ListView already does that for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem occurs when i try to manipulate an image uploaded from sinatra. File.open(params[:file][:tempfile])
My problem occurs when I try to pass 3 variables to my template tag
Problem occurs when i try to create dir using mkdir -p command with .
My problem occurs when I try to load multiple images in one class in
This problem occurs when I try to hide a file in my directory with
The problem occurs when I do a division operation. I would like to know
Strange problem occurs on the production platform (64 bit win 2008 server). It is
Note: This problem occurs only on Mac, on Windows works fine. I have a
Seems like cuke doesn't show the full error message (at least when problem occurs
When i put an NSOpenglView in NSSplitView, a problem occurs while dragging splitter. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.