RSS

Search Engine

Friday, November 19, 2010

Android Listview Header (Two or More) In Android

Hello Friends,

There are two or many more header listview in android. So Today we are discussed about the two header of android.

And See Also Simple Listview Display In Android Device.

So This are the all java and xml file given below. and this are the very useful projects.

main.xml


<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
/>

header.xml

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" android:scrollbars="none"
style="?android:attr/listSeparatorTextViewStyle" />

Now The java File code Given below.

SectionedAdapter .java

package com.android.listview;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;

abstract public class SectionedAdapter extends BaseAdapter {
abstract protected View getHeaderView(String caption, int index,
View convertView, ViewGroup parent);

private List
sections = new ArrayList
();
private static int TYPE_SECTION_HEADER = 1;

public SectionedAdapter() {
super();
}

public void addSection(String caption, Adapter adapter) {
sections.add(new Section(caption, adapter));
}

public Object getItem(int position) {
for (Section section : this.sections) {
if (position == 0) {
return (section);
}

int size = section.adapter.getCount() + 1;

if (position <>
return (section.adapter.getItem(position - 1));
}

position -= size;
}

return (null);
}

public int getCount() {
int total = 0;

for (Section section : this.sections) {
total += section.adapter.getCount() + 1; // add one for header
}

return (total);
}

public int getViewTypeCount() {
int total = 1; // one for the header, plus those from sections

for (Section section : this.sections) {
total += section.adapter.getViewTypeCount();
}

return (total);
}

public int getItemViewType(int position) {
int typeOffset = TYPE_SECTION_HEADER + 1; // start counting from here

for (Section section : this.sections) {
if (position == 0) {
return (TYPE_SECTION_HEADER);
}

int size = section.adapter.getCount() + 1;

if (position <>
return (typeOffset + section.adapter
.getItemViewType(position - 1));
}

position -= size;
typeOffset += section.adapter.getViewTypeCount();
}

return (-1);
}

public boolean areAllItemsSelectable() {
return (false);
}

public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionIndex = 0;

for (Section section : this.sections) {
if (position == 0) {
return (getHeaderView(section.caption, sectionIndex,
convertView, parent));
}

int size = section.adapter.getCount() + 1;

if (position <>
return (section.adapter.getView(position - 1, convertView,
parent));
}

position -= size;
sectionIndex++;
}

return (null);
}

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

class Section {
String caption;
Adapter adapter;

Section(String caption, Adapter adapter) {
this.caption = caption;
this.adapter = adapter;
}
}
}

Selection.java


package com.commonsware.android.listview;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SectionedDemo extends ListActivity {
private static String[] items = { "US", "UK", "CANADA", "JAPAN", "SINGAPORE",
"INDIA", "CHINA" };

private static String[] Sect = { "GOOGLE", "FACEBOOK","DELL" };

private static String[] Doc = { "FRONT", "TOP","BACK" };

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

adapter.addSection("step 1", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, items));

adapter.addSection("Step 2", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, Sect));

adapter.addSection("Step 3", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, Doc));

setListAdapter(adapter);
}

SectionedAdapter adapter = new SectionedAdapter() {
protected View getHeaderView(String caption, int index,
View convertView, ViewGroup parent) {
TextView result = (TextView) convertView;

if (convertView == null) {
result = (TextView) getLayoutInflater().inflate(
R.layout.header, null);
}

result.setText(caption);

return (result);
}
};
}


And Now The output given below.






219 comments:

«Oldest   ‹Older   201 – 219 of 219   Newer›   Newest»
Anonymous said...

Excellent guide, Managing multiple headers in a ListView can get tricky, especially when dealing with sectioned lists or combining different data types.
Medical Coding Courses in Delhi

Meghna said...

Great explanation on implementing sectioned list views in Android! UI organization like this improves user experience significantly. For those exploring healthcare tech careers, check out these practical and job-ready Medical Coding Courses in Delhi.

Tushar gautam said...

Thanks for the detailed guide! Adding two or more headers to a ListView can be tricky, but your clear examples made it easy to implement. Very useful for organizing complex lists.
Medical Coding Courses in Delhi

Anonymous said...

I was looking for this kind of information and enjoyed reading this one. Keep posting.


Medical Coding Courses in Delhi

Hima Rasheed said...

Thanks for the tutorial on adding multiple headers in Android ListView! The example with custom SectionedAdapter and XML layouts is useful for organizing list items into clear sections with headers. Do check out Medical Coding Courses in Delhi for more career options.

DIMPLE said...

It is amazing and wonderful to visit your site .Thanks for sharing this.
Medical Coding Courses in Delhi

princy jain said...

financial modeling courses in delhi
Really helpful post! I was looking for a way to add multiple headers in an Android ListView, and your explanation made it so much easier to understand. The example was clear, and it saved me a lot of time. Thanks for sharing such a useful tutorial. Great work!

princy jain said...

financial modeling courses in delhi

This tutorial was super helpful! I was trying to figure out how to add more than one header to a ListView, and your explanation cleared up my confusion. The code snippet was straightforward and easy to follow. Thanks a lot for sharing this — it really made my work easier!

Bhavika said...


financial modeling courses in delhi
Great tutorial! Handling multiple headers in Android List View can be tricky, especially when trying to maintain smooth scrolling and consistent layout behavior. Your explanation really helps demystify the process.

Using multiple times is a simple yet powerful approach, and I appreciate how you clarified the order of insertion and its impact on item indexing. It’s also good to note that headers added this way aren’t part of the adapter’s data set, which can be a gotcha for some developers.

akashiimskill said...

Nicely explained
financial modeling courses in delhi

Sohail Digi said...

The code snippets and explanations are well-structured and make it easier to understand the process. Thanks for sharing such useful insights!
https://iimskills.com/financial-modelling-course-in-delhi/

IIM Skills said...

Your tutorial on creating a ListView header with multiple items in Android is exactly the kind of practical guide developers appreciate. It addresses a common need for building complex, user-friendly interfaces, and the example code is easy to follow. Many tutorials miss small but important details, like handling view inflation properly, but you’ve covered those well. For developers aiming to improve their Android UI/UX, this post serves as a reliable reference point for implementing custom list headers effectively.
financial modeling courses in delhi

Nilabh said...

This tutorial provides a clear explanation of implementing multiple headers in an Android ListView. The step-by-step guide is helpful for developers looking to organize their list items effectively. Thanks for sharing this insightful resource!
financial modeling courses in delhi

Monika Khatnani said...

This guide really helped me understand how to handle multiple headers in List View without messing up the data binding. Thanks for the clear explanation.
financial modeling courses in delhi

iim.skills said...

This example shows a clear method for building a multi-header ListView in Android with a custom `SectionedAdapter`. It organizes items into labeled groups, making the list easier to read and navigate. Each section header helps users quickly find related content, improving the overall browsing experience.
financial modeling courses in delhi

IIM Skills (Shreya Saha) said...

Thanks for sharing this detailed tutorial! The example of using multiple headers in a ListView is very clear, and the code snippets are really helpful for beginners looking to understand Android ListView customization.
financial modeling courses in delhi

indu said...

Well explained—your tutorial on adding multiple headers to an Android ListView is straightforward and practical, simplifying the process of creating custom list designs. It’s a valuable resource for developers striving to build polished and user-friendly interfaces!
financial modeling courses in delhi

Sanjana said...

Your insights are genuinely inspiring and provide a much-needed boost.
Thanks for sharing!
VISA Management Courses in Delhi

sravanthi said...

Thank you for your article post. Really looking forward to read more. Really Cool.

QlikView Training Classes
Togaf 9.2 Training
Hyperion Essbase And Planning Online Training from USA
Best Oracle EBS SCM Online Training
Microsoft SQL Server Integration Services (SSIS) Online Course from India

«Oldest ‹Older   201 – 219 of 219   Newer› Newest»

Post a Comment