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
Related
I am working on a Custom list view with a single choice mode. I have followed the below tutorial and successfully achieved it.
Custom Single Choice ListView
I have a use case where I want to set a particular item is the list to be checked by default,
I have tried to do setChecked(true) at the require position in the adapter but it didnt work.
Can anybody help me how to achieve it.
Thanks in advance.
This source says:
Basically, the single-choice ListView expects the widgets you provide it with to implement the Checkable interface. LinearLayout et al don't. So you need to create a custom layout that inherits LinearLayout (or whatever layout you want to use for your items) and implements the necessary interface.
And from this source you can set it by using custom adapter inside your Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// set the choice mode
final ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// custom adapter
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
R.id.title, text) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ImageView icon = (ImageView) v.findViewById(R.id.img);
if (list.isItemChecked(position)) {
icon.setImageResource(R.drawable.checked);
} else {
icon.setImageResource(R.drawable.unchecked);
}
return v;
}
});
}
However this version has some performance issues - findViewById and setImageResource are relatively time-consuming operations so you should consider using some caching.
Hope it helps for you.
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 have a problem using the footer of my list view. In fact, I inflate an xml and I send it to the footerView. When my activity appears, my listview is showing and after two or three seconds (sometimes more) my footer is showing. Why is it so long ?
Here is a part of my code :
private View footerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.order_footer_item, null);
super.onCreate(savedInstanceState);
}
// This is Android annotation
#AfterViews
protected void setAfterViews() {
Adapter adapter = new Adapter();
list.setAdapter(adapter);
list.setOnItemClickListener(adapter);
TextView text = (TextView) footerView.findViewById(R.id.text);
text.setText("Test");
list.addFooterView(footerView);
}
I have no problem except slowing. Why ? How can I improve that ?
Thx in advance
Add it before setting adapter.
list.addFooterView(footerView);
And then
list.setAdapter(adapter);
This was a tricky problem for me. After spending quite a while working on it I realized that I was giving a parent parameter to the .inflate(view,parent,boolean) method instead of leaving it as null because I handle it with the getListView().addFooterView() method.
Error:
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, this.getListView(), true);
this.getListView().addFooterView(footerView);
Fix:
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
this.getListView().addFooterView(footerView);
First, you should probably create the footer view in the onCreateView, where you get a LayoutInflater inflate and a ViewGroup container.
Then, you'd call
inflater.inflate(R.layout.footer_view, container, false);
And the rest as per vipul mittal solution.
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);
I have a list view that is built using a simpleAdapter, each row is defined by a Layout containing a TableRow and a few TextView.
What should I do to know on which TextView (in the layout) was clicked on?
I am able to get the listview row (position) if I use a OnItemClickListener on the Listview, however it dosen't tell me on which TextView on that row was clicked on.
I would like to take different action depending on which TextView a user click on in a listview
listview
-----------------------
TextView1 : TextView2 | <--Row1
-----------------------
TextView1 : TextView2 | <--Row2
-----------------------
TextView1 | TextView2 | <--Row3
-----------------------
I would like to be able to tell, that row 2's TextView2 was click on.
Hopefully I am explaining myself well enough.
Thanks you
you can use below code in your List Activity
setListAdapter(new ArrayAdapter<String>(this, R.layout.your name, String Name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
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();
}
});
}
If the click is being handled by onItemClick, then you cannot know from this which View WITHIN that View was clicked. An OnItemClickListener just handles clicking a list item. What you might try is using a custom adapter in which you grab hold of all the Views in the row in getView() and set onClickListeners on them there. Or you can define the onClickListeners directly in your View class if possible.
you can handle the click of different view's in Adapter.The Adapter must be customized
I needed to do something similar to set a tag on elements within the list view, so I could find which button was clicked
myAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item,
new String[] {"ItemID", "ItemText", "ItemImage"},
new int[] {R.id.item_id, R.id.item_text, R.id.item_image})
{
#Override
public View getView (int row, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.item_id).setTag(row);
view.findViewById(R.id.item_text).setTag(row);
view.findViewById(R.id.item_image).setTag(row);
return view;
}
};
I added an onClick event for each item in the layout
<TextView
...
android:clickable="true"
android:onClick="myClickEvent"
...
/>
And then in my onClick events I had something like
public void myClickEvent(View v) {
Object o = v.getTag();
if (o instanceof Integer) {
int row = (Integer) o;
// do something
}
You can either use one onClick event per item in the view to determine which item was clicked, or you can have one onClick event for all and modify the tag to contain both the item and the row.