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

  • Home
  • SEARCH
  • 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 7183709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:02:48+00:00 2026-05-28T18:02:48+00:00

I have a problem with adding a listview as a header in my listview

  • 0

I have a problem with adding a listview as a header in my listview (go go redundancy). The code is working, however only the first item shows up in the header. The body looks fine.

listView = (ListView) findViewById(R.id.default_list_view);
header = (ListView) getLayoutInflater().inflate(R.layout.savings_overview_header_list, null, false);
HeaderAdapter hAdapter = new HeaderAdapter(getLayoutInflater());

hAdapter.addItem("1");
hAdapter.addItem("2");
hAdapter.addItem("3");

header.setAdapter(hAdapter);    

for (Policy p : saving.getPolicies()) {
    adapter.addItem(p);
}

listView.addHeaderView(header);
listView.setAdapter(adapter);

headerView:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/default_list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="2px"
android:fadingEdge="none"
android:fitsSystemWindows="true"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:listSelector="@drawable/list_item_background_selected" 
>
</ListView>

I have tried wrapping the ListView in a vertical LinearLayout but no cigar there either 🙁

hAdapter

private static class HeaderAdapter extends BaseAdapter {

    private final LayoutInflater inflater;

    protected ArrayList<String> data = new ArrayList<String>();

    public HeaderAdapter(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    public void addItem(String s) {
        data.add(s);
    }

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

        final PolicyViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.savings_overview_header_item, parent, false);
            holder = new PolicyViewHolder();

            holder.header = (TextView) convertView.findViewById(R.id.list_item_header);
                            holder.subHeader = (TextView) convertView.findViewById(R.id.list_item_subheader);
                            holder.img = (ImageView) convertView.findViewById(R.id.list_item_img);
            convertView.setTag(holder);
        } else {
            holder = (PolicyViewHolder) convertView.getTag();
        }

        String s = data.get(position);

        holder.header.setText(s);
                    holder.subHeader.setText(s);
        holder.img.setImageResId(R.drawable.test);

        return convertView;
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return data.get(position);
    }

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

    private static class PolicyViewHolder {
        TextView header;
                    TextView subHeader;
                    ImageView img;
    }
}

header_item

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <ImageView 
        android:id="@+id/list_item_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="5dp"
        android:layout_toLeftOf="@+id/list_item_value"
        android:layout_toRightOf="@+id/header_image"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/list_item_header"
            style="@style/header_list_header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/list_item_subheader"
            style="@style/list_header_sub_header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="sub header" />
    </LinearLayout>

</RelativeLayout>

i have tried to wrap a linearlayout=”vertical” around this one as well

  • 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-05-28T18:02:50+00:00Added an answer on May 28, 2026 at 6:02 pm

    You have to tell the parent view how to stack your elememnts. This is necessary for parents like a LinearLayout. If you’re going to have such an adapter, you need to have a parent to contain these things, UNLESS its a single view.

    Update

    Whenever you want to do processing with the views in a ListView you
    need to create a custom adapter that will handle your logic
    implementation and pass that information to the views as necessary.

    A custom adater would inflate the views piece by piece, this can be
    dynamic of fixed.

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

Sidebar

Related Questions

I have a problem in listview.I my listview i have five columns(question_number,question_text,start_time,end_time,status). the first
I have a problem adding ADO.Net Entity Data Model to my existing project, or
I have a problem adding entities to a collection. public void SaveNotificationUsergroups(int bookingobjectID, int[]
I have some problem with my .htaccess file. Here I am adding my problem.
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a listview and also a DataGrid having records with images. My problem
I have problem adding entity framework model to my project. Here is what I
I have a problem adding strings to a TStringList. I've searched other posts but
I have a problem adding CSS classes to my HTML links in CakePHP: I
I have a ListView with an ArrayAdapter providing the data. I'm adding paging, so

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.