Using Listview:How to Add a Header View

Using ListView : How to add a header view?

You simply can't use View as a Header of ListView.

Because the view which is being passed in has to be inflated.

Look at my answer at Android ListView addHeaderView() nullPointerException for predefined Views for more info.

EDIT:

Look at this tutorial Android ListView and ListActivity - Tutorial .

EDIT 2: This link is broken Android ListActivity with a header or footer

How add listView header as a custom layout

We Can add header to ListView as below :

LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.headerlayout, myListView, false);
myListView.addHeaderView(myHeader, null, false);

But, As per your error you might have taken Relative Layout in your xml and in your java file, You are using LinearLayout.

You also have to initialize your ListView as :

lv=(ListView)findViewById(R.id.yourlistview);`

Adding Header in ListView in android

The solution that works for me is to create a TableLayout that has a row with the heading and a row with the list like this:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/header" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</TableLayout>

Please make sure that the ListView has @android:id/list as the ID.

How to add Header to List View In Android

Some of the examples are:

  1. Android ListView Example

  2. ListView with headers above sections

AND it's important to read the API first.

if you like to have another example, a simple google search would do. :)

Flutter : How to add a Header Row to a ListView

Here's how I solved this. Thanks @najeira for getting me thinking about other solutions.

In the first body Column I used the same layout for my Header that I used for the ListTile.

Because my data ListTile, in this case, includes a CircleAvatar, all the horizontal spacing is off a bit... 5 columns where the CircleAvatar is rendered... then 4 evenly spaced columns.

So... I added a ListTile to the first body Column, a CircleAvatar with a backgroundColor of transparent, and then a Row of my 4 Headings.

        ListTile(
onTap: null,
leading: CircleAvatar(
backgroundColor: Colors.transparent,
),
title: Row(
children: <Widget>[
Expanded(child: Text("First Name")),
Expanded(child: Text("Last Name")),
Expanded(child: Text("City")),
Expanded(child: Text("Id")),
]
),
),

Sample Image

Android listview: there is a header of listview, how can I add a click event to a textview element?

You add the header as list view header.

But find view and set onclick in a another instance of view listheader

So your setonclick code will not work.

Try this: View listheader = header;

Hope this helps.

adding header sections to my listview... how to add these sections according to my code

Using your current code, try modifying your arrays slightly like this:

int[] img = { null, R.drawable.brazil_flag, R.drawable.croatian_flag,
R.drawable.mexico_flag, R.drawable.cameroon_flag, null, R.drawable.spain,
R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
R.drawable.australia, null, ... };

String[] name = { "Group A", "BRA", "CRO", "MEX", "CMR", "Group B","ESP", "NED", "CHI", "AUS", ... };

And then in the getView method in your adapter do this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = null;
/// inflate a header view here
if (itemDetailsarrayList.get(position).getImage()==null) {
row = layoutInflater.inflate(R.layout.row_group_header, parent,
false);
TextView txt = (TextView) row.findViewById(R.id.groupTitle);
txt.setText(itemDetailsarrayList.get(position).getName());
/// Inflate your regular item row here.
} else {
row = layoutInflater.inflate(R.layout.row_list_group, parent,
false);
TextView txt = (TextView) row.findViewById(R.id.textView1);
ImageView imgv = (ImageView) row.findViewById(R.id.imageView1);
txt.setText(itemDetailsarrayList.get(position).getName());
imgv.setImageResource(itemDetailsarrayList.get(position).getImage());
}
return row;
}

EDIT

/// FILE: res/layout/row_group_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000"
android:padding="5dip" >

<TextView
android:id="@+id/groupTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView1"
android:textColor="#FFF"
android:ems="10" />

</RelativeLayout>

Notice the Null on the image array and the "Group X" in the names array.

How to add multiple header views in a ListView

I don't think what you want to do is possible the way you are trying to do it. When you use addHeaderView it wraps your ListAdapter in HeaderViewListAdapter. I looked at the docs for it here and that seems to imply that you could have multiple headers, but they would all be at the top (duh, header).

It sounds like what you actually want is seperators...

You could use CommonWare's MergeAdapter. It will let you insert adapters and views (in whatever order you wish) and present them all as a single adapter to a listview. You just hand it headers and adapters for each section of content and then set it to your list.

Pseudo-code example:

myMergeAdapter = new MergeAdapter(); 
myMergeAdapter.addView(HeaderView1);
myMergeAdapter.addAdapter(listAdapter1);
myMergeAdapter.addView(HeaderView2);
myMergeAdapter.addAdapter(listAdapter2);
setListAdapter(myMergeAdapter);

ListView addHeaderView causes position to increase by one?

I just came across this problem and the best way seems to use the ListView.getItemAtPosition(position) instead of ListAdapter.getItem(position) as the ListView version accounts for the headers, ie:-

Do this instead:

myListView.getItemAtPosition(position)


Related Topics



Leave a reply



Submit