Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ListView : How to add a header view?

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();     }   }); }   } 
like image 821
Belgi Avatar asked Nov 02 '11 09:11

Belgi


People also ask

How do I add a header to a list view?

This example demonstrates How to add header item for Listview in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

What does ListView uses to place each item?

ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView.

What is a ListView?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


1 Answers

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); 
like image 112
Paresh Mayani Avatar answered Sep 25 '22 08:09

Paresh Mayani