I have created a ListView and added header with addHeaderView, then I called setListAdapter in my ListActivity. Any idea how can I dynamically addFooterView after I called setListAdapter?
ANSWER:
I added both header view and footer view (actually buttons) into my list view,
but both of them I wrapped into a FrameLayout using wrap_content height, then when I do not need to be the header button to be shown I just setVisibility(View.GONE) and FrameLayout wraps to 0 height and vissualy it is not visible (same effect as if I would call removeHeaderView), and if I need to show it again I setVisibilty(View.VISIBLE) and it is shown (same effect as addHeaderView - which is of course not possible after calling setting list adapter)
Discussed here:
Hide footer view in ListView?
View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
android.R.id.text1, names));
You Have to do like this
View header = (View)getLayoutInflater().inflate(R.layout.header,null);
SimpleAdapter myAdapter=new SimpleAdapter(this,myList,R.layout.transactionvalues,
new String[] {"transaction_date_time","user_name","site_name","machine_name"},new int[] {R.id.Date_Time,R.id.User,R.id.Site,R.id.Machine});
if(header == null){
lst.removeHeaderView(header);
}else
{
lst.addHeaderView(header,null,false);
}
lst.setAdapter(myAdapter);
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);
I wanted to display a web view in between in the list view and bottom of the list view.
I was able to add footer to the list view to display the web view in the bottom, but I don't know how to display in the middle
Code where I have added the footer:
ListView list = (ListView)view.findViewById(android.R.id.list);
WebView web;
LayoutInflater inflaterOf = getActivity().getLayoutInflater();
LinearLayout listFooterView =
(LinearLayout)inflaterOf.inflate(R.layout.list_footer, null);
web = (WebView)listFooterView.findViewById(R.id.webview_footer);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/news_footer.html");
list.addFooterView(listFooterView);
Please let me know how to insert the layout with web view in-between the list view
You should create a custom object and in your ArrayAdapter of listview, change the layout to inflate when getCount is equal to list.size()/2.
That's the way I'll handle this.
I am using:
https://github.com/chrisbanes/Android-PullToRefresh
PullToRefreshListView listView = (PullToRefreshListView) findViewById(R.id.listView);
footerView = inflater.inflate(R.layout.footer, listView, false);
listView.addFooterView(footerView);
I get the Error:
The method addFooterView(View) is undefined for the type
PullToRefreshListView
How to solve it?
Thank You
To add footer view, you have to get the pure ListView first then you can use it to add Footer View. You have to call getRefreshableView() to get pure ListView. Here is the sample code:
PullToRefreshListView listView = (PullToRefreshListView) findViewById(R.id.listView);
footerView = inflater.inflate(R.layout.footer, listView, false);
//get ListView object first, and then add footer view
listView.getRefreshableView().addFooterView(footerView);
You should get the normal ListView first, then use the normal ListView's addFooterView function.
ListView mList = listView.getRefreshableView();
mList.addFooterView(footerView);
i am progrmatically adding list view to a linear layout like this:
ArrayList<Answer> ans = (ArrayList<Answer>) ques.getAnswers();
adapter = new AnswerAdapter(Test.this, ans);
ansList = new ListView(Test.this); // my list view adding dynamically
ansList.setAdapter(adapter);
ansList.setVerticalScrollBarEnabled(false);
ansList.setOnItemClickListener(cellClickListener);
ansLayout.addView(ansList);
Now the problem is it has the default divider after each item except the last item, i want the divider to be visible after the last item also.
I have looked into many questions in SO where they are adding views in the layout but i need to add programatically.
You can add footer view in listview... so make a layout of footerview... and bind as below
View footerView = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.track_footer_view, null, false);
listview.addFooterView(footerView);
so you can have footerview at end of last item..
- Add that separator as the footer to your ListView.
Eg:
View mfooter = View.inflate(MyClass.this, R.layout.imagelayout, null);
lv.addFooterView(mfooter, null, false);
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