0

I have a View extending google MapView. And a ListView with items. What I need is to put MyMapView as a header to the list view. Help please what to do

Update: When I try to do this:

LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView list = (ListView) findViewById(R.id.list);
View m = inflater.inflate(R.layout.map, list, false);
list.addHeaderView(m); 

I got an error:

MapViews can only be created inside an instances of MapActivity

I think it's because I have removed my xml layout for map into different.xml from my activity

Ajeett
  • 814
  • 2
  • 7
  • 18
Graykos
  • 1,281
  • 3
  • 20
  • 31

4 Answers4

3

Try this

Create your ListViewHeader.xml first

and do this

ListView lv = getListView(); // Your listView
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.YourHeader, lv, false);
lv.addHeaderView(header, null, false);

take a look on Android: Adding static header to the top of a ListActivity

Community
  • 1
  • 1
AITAALI_ABDERRAHMANE
  • 2,499
  • 1
  • 26
  • 31
1

Try to call

ListView.addHeaderView (View v)

before you set adapter to this ListView

Tang Ke
  • 1,508
  • 12
  • 12
  • @user1531195 this method must called before calling setAdapter if it didn't help, you can try HeaderViewListAdapter – Tang Ke Jul 20 '12 at 08:45
  • @user1531195 how exactly do you want it to look? Do you want the view to remain at the top of the screen when you scroll or do you want it to scroll together with the list elements? – vikki Jul 20 '12 at 08:46
  • Vikki, I want it to scroll together with the list elements, yep. – Graykos Jul 20 '12 at 08:48
  • @user1531195 then this answer should work, are you getting any errors? – vikki Jul 20 '12 at 09:02
  • Read my initial post. I added info there – Graykos Jul 20 '12 at 09:09
0

if you are using a custom adapter, in getView() method provide different view at position 0.

if(position==0)
{
//Provide header view
}
else  
{
//provide normal row
}
Kamal
  • 1,415
  • 13
  • 24
0

You can do the following to add a view as ListView header in android.


ListView list = (ListView) findViewById(R.id.list);

LayoutInflater inflater = getLayoutInflater();

ViewGroup viewGroup= (ViewGroup)inflater.inflate(R.layout.YourLayoutFile,list, false);

list.addHeaderView(viewGroup, null, false);

Amjad Omari
  • 1,104
  • 3
  • 15
  • 40