1

So I have this:

...<!--Stuff above-->
<ListView 
     android:id="@+id/reminders_list"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"></ListView>
...<!--More stuff below-->

My problem is when I try to add more rows it does not work because the adapter doesn't add more views if there is no more space. At least in this instance. This means I need to resize it dynamically. Question is how to do this? If something besides resizing is possible I would rather do that. Thanks in advance.

Andy
  • 10,553
  • 21
  • 75
  • 125
  • 1
    You can use custom List Adapter and Pass List of Elements to it. – Anil Jadhav Jul 10 '12 at 04:48
  • Hmm, I actually never thought of that. Let me try that. I will get back to you on it – Andy Jul 10 '12 at 04:51
  • Use Custom adapter class and extend it by BaseAdapter,you will get 4 functions in that one of them will be getCount(),pass the value u want you – AkashG Jul 10 '12 at 04:52
  • @AkashG Thats actually exactly what I am using. But the issue I am having is exactly because of that. When there is nothing under the ListView this is not a problem. But apparently when there is something, it won't resize it. What makes it worse is trying to add another row does not work due to no space. SO the ListViews scroll feature is also useless. Idk why it does this, but thats been my experience. – Andy Jul 10 '12 at 04:54
  • see the size of the listview cant be fixed.let me know what u are passing in adapter? – AkashG Jul 10 '12 at 04:57
  • Damn, @Anil I also realized what you said is the same thing AkashG said. I already have that. But thats my problem. When I refresh the adapter, it does not make it taller to fit it. – Andy Jul 10 '12 at 04:57
  • If you have time, I think it's worth watching this ‒ [Google I/O 2010 - The world of ListView](http://www.youtube.com/watch?v=wDBM6wVEO70)… –  Jul 10 '12 at 05:03
  • Thank you @HaiBison Venky also provided the link. I will definitely watch it right now. But in either case, do you have an idea why I am getting this problem? – Andy Jul 10 '12 at 05:05
  • Sorry I didn't read Venky's answer carefully. I think after adding items to the list's adapter, you should call [notifyDataSetChanged()](http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()) to update the UI (as Dongie said). –  Jul 10 '12 at 05:11
  • I do use `notifyDataSetChanged()` @HaiBison and its fine. I really appreciate anyone giving me resources to help me continue learning Android development. – Andy Jul 10 '12 at 05:14
  • If there is no more space, how do you think you are going to add views? Lists don't work that way. They stay the size you originally set them. Your best bet (IMO) is to use a linearlayout and set the list layout_height to "0dp" and use a layout_weight="1". That will cause the list to take up all available space left after all other elements have been drawn. If you don't have enough elements in the list to fill that space, you'll have some empty space. When you have enough to fill it you'll have as many views visible as is possible. – Barak Jul 10 '12 at 06:15
  • Thank you for the tip @Barak I was trying to set the `layout_weight` but was failing. But I think I found the way I'm going to do it which isn't necessary to mess with the height of the view. – Andy Jul 10 '12 at 06:53

2 Answers2

5

Problem is with your android:layout_height="wrap_content"

Setting height as wrap_content will shows only first three rows rest will be ignored. Try to make it as android:layout_height="fill_parent"

As Romain Guy (Google Engineer workd on UI toolkit) Said in his post

By setting the width to wrap_contentyou are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.

So if you set the layout width or layout height of your ListView to wrap_content the ListView will try to measure every single view that is attached to it - which is definitely not what you want.

Keep in mind: avoid setting wrap_content for ListViews or GridViews at all times, for more details see this Google I/O video talking about the world of listview

Dynamically Adding rows to List View Link_1

Dynamically Adding rows to List View Link_2

Dynamically Adding rows to List View Link_3

Dynamically Adding rows to List View Link_4

Community
  • 1
  • 1
Venky
  • 11,049
  • 5
  • 49
  • 66
  • I did `match_parent` as `fill_parent` is deprecated, but it doesn't work. Same issue. Like its a weird thing. What happens is I have an initial list which has 1 row. That sets the size. When I click to add another row, it fails because there is no space. I thought it'd just fit it, but it does not do that. ANy idea why it does that?! lol. But thanks for the vid. Will definitely watch it. Maybe that has some answers – Andy Jul 10 '12 at 05:02
  • Did you checked your Adapter size after adding , so that you can know only view is not coming or data itself not adding – Venky Jul 10 '12 at 05:06
  • Actually yes I do know. I don't know the size but I was logging when it was running, and it just ignores the addition of another row completely. It runs and replaces whatever was in position 0, so it does run, just wont add anything to position 1. But when you actually set the height, like say `80dp`, and then do the same thing, position 0 and 1 can be seen, but not position 2. From that trial and error, I have gathered that `ListView` just wont add more rows, other than what it already has space for in the initial creation. – Andy Jul 10 '12 at 05:12
  • If you have less amount of data why don't you use dynamically creating Table layout or some what scroll view.. – Venky Jul 10 '12 at 05:15
  • Hmm, you are right! Thanks for all of this. Its much appreciated. I will get back to you on something else I choose. – Andy Jul 10 '12 at 05:25
  • Ok so I looked at the video, and he said for small data sets avoid using it. So what I am trying to do is be able to add and remove `Spinners` dynamically. How would you accomplish that? – Andy Jul 10 '12 at 05:46
  • [Dynamically adding tablerow into tablelayout.. Make use of this code for adding any widgets you need](http://stackoverflow.com/questions/7683154/android-softkeyboard-enter-the-numeric-value-into-edittext-very-slow/7685947#7685947) – Venky Jul 10 '12 at 06:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13646/discussion-between-venky-and-andy) – Venky Jul 10 '12 at 06:09
  • Hmm, and could that also allow me to remove them as well? I need to make sure when adding `Spinner`s that they are always added on the bottom. – Andy Jul 10 '12 at 06:10
2

When adding elements to the List Adapter, you need to call its notifyDataSetChanged() member function to which notifies any attached observers (such as a ListView), that the data has changed, and will cause it to refresh.

Dongie Agnir
  • 612
  • 4
  • 11