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
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
Try to call
ListView.addHeaderView (View v)
before you set adapter to this ListView
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
}
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);
Related
I have a listview with an adapter. I want to add a footer and that this footer will be displayed when scrolling the listview. Right now with this code the footerview is displayed when the listview is finished. In the onCreate method of the list i have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listado_imagenes_subidas_main);
// load list application
listImagenes = (ListView)findViewById(R.id.lvImages);
LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer3buttons, listImagenes,
false);
listImagenes.addFooterView(footer, null, false);
// create new adapter
ImagenesAdapter adapter = new ImagenesAdapter(this, ListadoImagenes());
// set adapter to list view
listImagenes.setAdapter(adapter);
}
Could you please help me with this issue?
Thank you in advanced.
If i understood correctly what you wan't is a "sticky" footer the addFooterView method add a footer after the last view item, for a sticky footer you have to roll your own, or use an external library for it.
try with this one
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.
Can anyone help me to find out how to add an ImageView as a header to a ListView?
My code is here:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snapshot = inflater.inflate(R.layout.map_snapshot, null);
ListView channelsList = (ListView) findViewById(R.id.channelsList);
channelsList.addHeaderView(snapshot);
By now it shows nothing.
Try inflating your header view passing the parent (ListView) as parameter:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false);
Then, just add it to the ListView:
channelsList.addHeaderView(snapshot);
Try:
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false);
channelsList .addHeaderView(header, null, false);
In layout map_snapshot,you must be having parent layout as here i have taken Linear Layout likewise take that.
ListView channelsList = (ListView) findViewById(R.id.channelsList);
LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null);
listView.addHeaderView(headerView);
if you want to add views in layout:
TextView mUsername=(TextView) headerView.findViewById(R.id.user_name);
You can use CommonsWare MergeAdapter or SectionedListAdapter. With this library you can use any View as Header in your Listview. See this SO question and accepted answer for more information.
I'm reusing the ListView AND the LayoutInflater here for both a header and a footer:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = (View)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
I reckon reusing the ListView is perfectly sensible, but I'm not so sure about the LayoutInflater. Am I flirting with disaster here, or is this okay?
Please refer to : http://developer.android.com/reference/android/widget/ListView.html#getFooterViewsCount()
Returns the number of footer views in the list. Footer views are
special views at the bottom of the list that should not be recycled
during a layout.
So yeah... I don't think you are doing anything wrong.
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();
}
});
}
}
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);
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
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