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.






24 comments:

Yousuf Qureshi said...

hello,
I 'd like to add image in listview I mean per row. can you please help me

thanks

Radu Motisan said...

here is another Listview with HEader for Android. It is dynamic (doesn't use XML), and comes with nice background images that are joint together using their position ID: http://www.pocketmagic.net/?p=1678

There's the complete source code included, and more details on how to achieve this.

Nick said...

It looks like there are multiple syntax problems with this code. It might have messed up when you pasted it here, but its spitting a ridiculous amount of errors when i try to use this. Can you please verify if the code will work as you pull it off the site?

decades software said...

Rather crappy source code formatting. Doesn't work...

Frikish said...

Like Nick said.. Please either fix the code or paste the java files :)

neil said...

Don't know, who the guy of this blog is, but the real code of the (obviously) real author of the code can be found here:
https://github.com/commonsguy/cw-advandroid.git

George said...

Crappy code, unintelligible speech. Top result on Google.

Oh my... copy and past junkies won this time!

Anonymous said...

http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/

Anonymous said...

Crap.. shit code.

Anonymous said...

If adapter have a lot of subadapters and items, you will face with OOM exception. The problem is in getItemViewType() function implementation - when you calculate number of types, you should treat adapters with the same class as a single adapter.

Anonymous said...

code has errors.......

Anonymous said...

Here is the original java file:

http://code.google.com/p/dailyburndroid/source/browse/branches/ui_changes/src/com/commonsware/android/listview/SectionedAdapter.java?r=169

Anonymous said...

Thank you very much

Bhagyashree said...

Thank you so much...

Android Reviews said...

this code error not work

Android Ashish said...

I have three ArrayList
ArrayList data;
ArratList to;
ArrayList from;


there array list containing corresponding at a same index. I want to set date in Section header and and show to and from data of same date as Section Items.

Please Help me. thanks to advanced

Sachin Shelke said...

you can try this
http://smartphonebysachin.blogspot.in/2012/03/custom-listview-with-separator-and.html

Anonymous said...

Are you realy Test Your Code ?

Cause I try to test that is very poor code

You just Copy it from other source and paste in your blog!!!!

Go Check out your Code

Anonymous said...

This is good code and exactly what I want.
Thank you very much.

SHIDHIN T.S said...

Check this.This is to display custom listview like this..
http://custom-listview-with-separate-headers.blogspot.in

Chad Bingham said...

This has so many errors. Beyond trying to repair if you don't know how to do this. DO NOT USE THIS TUTORIAL. very poor quality. -1!

SHIDHIN T.S said...

HI Chad Bingham,

Can you Check this.This is to display custom listview like this..
http://custom-listview-with-separate-headers.blogspot.in

Anonymous said...

how to add multiple selection in this listview with header sections.

Anonymous said...

full of error , just a crap

Post a Comment