Using ListView : How to add a header view?
Asked Answered
L

3

42

I looke at the ListView API and I saw the method:

addHeaderView(View v)

What I want to do is to have a layout above the list, is this possible ?

I tried doing something like :

  EditText et=new EditText(this);
  et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,     LayoutParams.WRAP_CONTENT));
  et.setInputType(InputType.TYPE_CLASS_TEXT); 
  addHeaderView(et); //makes app crash

I also tried

setContentView(R.layout.tryview);

but it also make the app crash.

Help is very much appreciated!

Edit : The code for this class is:

public class GroupsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  String your_array_contents[] = {"a","ab","c"};
  setListAdapter(new ArrayAdapter<String>(this, R.layout.groups_layout, your_array_contents));
  EditText et=new EditText(this);
  et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  et.setInputType(InputType.TYPE_CLASS_TEXT); 

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

  lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
    android.R.layout.simple_list_item_multiple_choice, your_array_contents));
    lv.addHeaderView(et); //makes app crash
  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      // When clicked, show a toast with the TextView text
     // Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          //Toast.LENGTH_SHORT).show();
    }
  });
}


}
Lionhearted answered 2/11, 2011 at 9:49 Comment(7)
Look at my answer. Hope it will help you. Also refer the link I mentioned in my answer.Sad
@Sad - look at my comment on your answerLionhearted
What is crashes log? I think you get nullPointerException. right?Sad
@Sad - I tried to run it in debug mode and got resource not found on " lv.addHeaderView(header);"Lionhearted
the code in the link I suggested is just for the understand purpose, you can't direct use it, because you don't know which layout I used in it, just make simple layout.xml file for header, inflate it for view and set as headerView().Sad
@Sad - but I call inflate with my xml file: View header = (View)getLayoutInflater().inflate(R.layout.tryview,null);Lionhearted
Look at my edited answer I put a link of tutorial listview with header and footer just try that and understand how it works.Sad
S
29

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

Sad answered 2/11, 2011 at 9:54 Comment(3)
I tried adding View header = (View)getLayoutInflater().inflate(R.layout.headerView,null); ls.addHeaderView(header); but it crashes. (I added this after the line lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this, android.R.layout.simple_list_item_multiple_choice, your_array_contents));)Lionhearted
Before Android version 4.4 you can't use addHeaderView after setAdapter method.Soma
Link Edit 2, gives Error 404, so remove it or fixing the link will be appreciated.Broddy
I
78

You can add as many headers as you like by calling addHeaderView() multiple times. You have to do it before setting the adapter to the list view.

And yes you can add header something like this way:

LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, myListView, false);
myListView.addHeaderView(header, null, false);
Inmesh answered 2/11, 2011 at 9:52 Comment(5)
it won't lead to crash, if you add it after.so i think that is not the problem.Marlomarlon
@YashwanthKumar This is as per the android doc: goo.gl/AWU4K, read carefully the NOTE section :)Inmesh
true, i just said, it won't cause crash, it will not show the header view.Marlomarlon
Before Android version 4.4 you can't use addHeaderView after setAdapter method.Soma
Clean. thanks! :) I was just wondering if you could call those views from that layout and use it in this Activity. Apparently it gave a npe. Any solution?Menstruation
S
29

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

Sad answered 2/11, 2011 at 9:54 Comment(3)
I tried adding View header = (View)getLayoutInflater().inflate(R.layout.headerView,null); ls.addHeaderView(header); but it crashes. (I added this after the line lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this, android.R.layout.simple_list_item_multiple_choice, your_array_contents));)Lionhearted
Before Android version 4.4 you can't use addHeaderView after setAdapter method.Soma
Link Edit 2, gives Error 404, so remove it or fixing the link will be appreciated.Broddy
E
1

I found out that inflating the header view as:

inflater.inflate(R.layout.listheader, container, false);

being container the Fragment's ViewGroup, inflates the headerview with a LayoutParam that extends from FragmentLayout but ListView expect it to be a AbsListView.LayoutParams instead.

So, my problem was solved solved by inflating the header view passing the list as container:

ListView list = fragmentview.findViewById(R.id.listview);
View headerView = inflater.inflate(R.layout.listheader, list, false);

then

list.addHeaderView(headerView, null, false);

Kinda late answer but I hope this can help someone

Elsewhere answered 29/8, 2019 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.