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 7975719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:43:15+00:00 2026-06-04T08:43:15+00:00

I need to create a ListView in Android that has headers and is multiline.

  • 0

I need to create a ListView in Android that has headers and is multiline. I use this to display the assignment title and the due date below it. The assignments are sorted under two headers: “Upcoming Assignments” and “Past Assignments” (based on date, of course).

I have the headers working and the assignment title displaying properly, but I am stumped as to how to incorporate a second line into the list.

Here is my code:

ListCourseAssignments.java

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    final SeparatedListAdapter adapter;
    setContentView(R.layout.courseassignmentslistview);
    adapter = new SeparatedListAdapter(getContext());
    ArrayAdapter<String> upcomingList = new ArrayAdapter<String>(getContext(), R.layout.list_item, Homework.getUpcomingDates()); // Homework.getUpcomingDates() Returns string array
    ArrayAdapter<String> pastList = new ArrayAdapter<String>(getContext(), R.layout.list_item, Homework.getPastDates()); // Homework.getPastDates() Returns string array

    adapter.addSection("Upcoming Assignments", upcomingList); 
    adapter.addSection("Past Assignments", pastList);

    ListView lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long duration){
            String item = (String) adapter.getItem(position);
            Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
        }
    });
}

SeparatedListAdapter.java

public class SeparatedListAdapter extends BaseAdapter {
public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
public final ArrayAdapter<String> headers;

public SeparatedListAdapter(Context context) {
    headers = new ArrayAdapter<String>(context, R.layout.courseassignmentslistview_header);
}

public void addSection(String section, Adapter adapter) {
    this.headers.add(section);
    this.sections.put(section, adapter);
}

public Object getItem(int position) {
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return section;
        if (position < size) return adapter.getItem(position - 1);

        position -= size;
    }
    return null;
}

public int getCount() {
    int total = 0;
    for (Adapter adapter : this.sections.values()) total += adapter.getCount() + 1;
    return total;
}

@Override
public int getViewTypeCount() {
    int total = 1;
    for (Adapter adapter : this.sections.values()) total += adapter.getViewTypeCount();
    return total;
}

@Override
public int getItemViewType(int position) {
    int type = 1;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return 0;
        if (position < size) return type + adapter.getItemViewType(position - 1);

        position -= size;
        type += adapter.getViewTypeCount();
    }
    return -1;
}

public boolean areAllItemsSelectable() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (getItemViewType(position) != 0);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int sectionnum = 0;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return headers.getView(sectionnum, convertView, parent);
        if (position < size) return adapter.getView(position - 1, convertView, parent);

        position -= size;
        sectionnum++;
    }
    return null;
}

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

courseassignmentslistview_header.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_header_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:paddingLeft="5dip"
    style="?android:attr/listSeparatorTextViewStyle" />

courseassignmentslistview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

Here’s a screenshot of what it currently looks like.

EDIT: I know how to create a list with multiline, but I just don’t know how to put it together with a list with headers (such as this)…

  • 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-04T08:43:16+00:00Added an answer on June 4, 2026 at 8:43 am

    I don’t know how to do it with what you have so far, but you could do it with CommonWare’s MergeAdapter.

    Quoting the readme:

    MergeAdapter accepts a mix of Adapters and Views and presents them as one contiguous 
    whole to whatever ListView it is poured into. This is good for cases where you have
    multiple data sources, or if you have a handful of ordinary Views to mix in with lists 
    of data, or the like.
    

    You can get it here.

    I wrote a brief overview of using it here

    Using MergeAdapter, you can use it to do your section headers by supplying a view for them, and then set your adapter up to create the two-line format you want.

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

Sidebar

Related Questions

I need to create and populate a ListView with 3 strings that come from
I am working on android applications. In my project I need to create Listview.
I'm working with expandable listview. In that I need to use click-able imageview. I've
So I'm trying to create a screen which has a ListView and over that
I use the below code to create listview without listactivity.I need to know how
I need to create an ASP.net page that has a control on the page
I need to create a ListView or TableLayout that each row consists of 3
After reading some posts like this one: Choose File Dialog It appears that Android
I need to create a very small ListView. Each row could have a different
I am making an android application that uses a listview. I want to get

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.