[android-developers] Re: position value in getView seems too big
I am calling notifyDataSetChanged(). If I override it in my adapter, to add a log trace, this does get hit and the trace happens . . . *// just overriding notifyDataSetChanged() to instrument it @Overridepublic void notifyDataSetChanged() { Log.e (notifyDataSetChanged()..., listItems.size()= + String.valueOf(listItems.size()) + lv.getCount()= + String.valueOf(lv.getCount()) ); //!! debugging super.notifyDataSetChanged();} * The dataset - listItems - returns a size of 6. But *getView(*) gets called with position values up to 12 (there are 12 items visible on the screen because that's how many items there *used to be* in the dataset,) So why is getView being called with a position value greater that the (current) largest index in its dataset? Another way to think of this question is how does getView know the largest value of position it can have? (BTW I solved the ListView.getCount() problem by calling setListAdapter() on the ListActivity after updating the datasource. As a result ListView's *getCount()* now accurately reflect the the number of list items. That seems to have no affect on this bug. On Thursday, May 15, 2014 2:09:57 PM UTC-4, plnelson wrote: I'm getting a crash in my adapter's *getView()* routine because it's being called with a position value of 6 and my datasource only has 6 items in it. So I assumed that the position parameter should be in a range of [0]-[5]? What determines the range of values in *getView(*)'s position parameter? Details: the XML ... ListView android:id=@android:id/list android:layout_height=match_parent android:layout_width=match_parent android:cacheColorHint=@color/colGrey android:background=@color/colGrey android:clickable=true android:fastScrollEnabled=true android:choiceMode=none/ ...In MyListActivity, which is a ListActivity . . . public static ListView lv; // my ListView in the code ... during *onCreate()* . . . setContentView(R.layout.mylist); lv = getListView(); create the adapter and bind it . . . mylistadapter = new MyListAdapter(MyListActivity.this); setListAdapter(mylistadapter); // bind the adapter ...the data source is an ArrayList called listItems. during the course of running the program its size varies and it may have been 15 earlier in program execution . . . public static ArrayListStringlistItems=new ArrayListString(); ... in my adapter, which is a BaseAdapter, my ovveride of getCount() looks like this . . . @Override public int getCount() { return listItems.size(); } ... when I call *getCount()* in *getView()* it returns 6, which is the number of items in the data source, but if I call lv.*getView(*) it returns 15. (any idea where this 15 is coming from?) Could that be why the adapter is calling *getView()* with index too big? Thanks in advance! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: position value in getView seems too big
The ListAdapter's max value of the 'position' parameter of its getView(...) method is determined by the ListAdatper's getCount() method. It will never be larger than the value returned by getCount(): 0 = position getCount() I have never seen position being larger or equal than getCount()'s return value, unless the ListAdapter is in an inconsistent state. The ListAdapter can be in an inconsistent state when its underlying data is changed without notifying the ListAdapter (notifyDataSetChanged()). E.g static ListString someListItems = ... ... ... ListAdapter adapter = new MyListAdapter(); listView.setAdapter(adapter); ... ... public MyListAdapter extends . { ... public int getCount() { return someListItems.size(); } ... } ... Say some piece of code has access to the reference named 'someListItems', either code in the same class or somewhere else in another class: someListItems.add(newItem); someListItems.remove(removeIndex); // If we don't call the ListAdapter's notifyDataSetChanged() now, we can get very strange results. // The ListAdapter is in an inconsistent state, because its getCount() will return a different unexpected value and this // value could now be less than the known available 'position' values (as known by the ListView) to be used for calls to getView(...). On Monday, May 19, 2014 12:14:43 PM UTC-4, plnelson wrote: I am calling notifyDataSetChanged(). If I override it in my adapter, to add a log trace, this does get hit and the trace happens . . . *// just overriding notifyDataSetChanged() to instrument it @Overridepublic void notifyDataSetChanged() { Log.e (notifyDataSetChanged()..., listItems.size()= + String.valueOf(listItems.size()) + lv.getCount()= + String.valueOf(lv.getCount()) ); //!! debugging super.notifyDataSetChanged();} * The dataset - listItems - returns a size of 6. But *getView(*) gets called with position values up to 12 (there are 12 items visible on the screen because that's how many items there *used to be* in the dataset,) So why is getView being called with a position value greater that the (current) largest index in its dataset? Another way to think of this question is how does getView know the largest value of position it can have? (BTW I solved the ListView.getCount() problem by calling setListAdapter() on the ListActivity after updating the datasource. As a result ListView's *getCount()* now accurately reflect the the number of list items. That seems to have no affect on this bug. On Thursday, May 15, 2014 2:09:57 PM UTC-4, plnelson wrote: I'm getting a crash in my adapter's *getView()* routine because it's being called with a position value of 6 and my datasource only has 6 items in it. So I assumed that the position parameter should be in a range of [0]-[5]? What determines the range of values in *getView(*)'s position parameter? Details: the XML ... ListView android:id=@android:id/list android:layout_height=match_parent android:layout_width=match_parent android:cacheColorHint=@color/colGrey android:background=@color/colGrey android:clickable=true android:fastScrollEnabled=true android:choiceMode=none/ ...In MyListActivity, which is a ListActivity . . . public static ListView lv; // my ListView in the code ... during *onCreate()* . . . setContentView(R.layout.mylist); lv = getListView(); create the adapter and bind it . . . mylistadapter = new MyListAdapter(MyListActivity.this); setListAdapter(mylistadapter); // bind the adapter ...the data source is an ArrayList called listItems. during the course of running the program its size varies and it may have been 15 earlier in program execution . . . public static ArrayListStringlistItems=new ArrayListString(); ... in my adapter, which is a BaseAdapter, my ovveride of getCount() looks like this . . . @Override public int getCount() { return listItems.size(); } ... when I call *getCount()* in *getView()* it returns 6, which is the number of items in the data source, but if I call lv.*getView(*) it returns 15. (any idea where this 15 is coming from?) Could that be why the adapter is calling *getView()* with index too big? Thanks in advance! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop
[android-developers] Re: position value in getView seems too big
Sorry - should be lv.get*Count*. And yes, I am calling notifyDataSetChanged. I've also tried calling the ListView's *invalidate()* method, with no change in the behavior. Since posting this I've written a whole separate version of the program where I initially display 12 items ( listItems.size() == 12). That part works fine. I then shrink my list to 6 by ... - clear my listItems * ( listItems.size() == 0 )* - add in 6 new items * ( listItems.size() == 6 )* - call notifydataSetChanged() ... but the ListView's getCount still returns 12!Why?And is this why the adapter's getCount() returns with an index that's too high for it's dataset? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: position value in getView seems too big
That is hard to figure out without you posting the code of your adapter and how you create an instance of your adapter. It is probably a subtle bug... On Friday, May 16, 2014 2:34:19 PM UTC-4, plnelson wrote: Sorry - should be lv.get*Count*. And yes, I am calling notifyDataSetChanged. I've also tried calling the ListView's *invalidate()* method, with no change in the behavior. Since posting this I've written a whole separate version of the program where I initially display 12 items ( listItems.size() == 12). That part works fine. I then shrink my list to 6 by ... - clear my listItems * ( listItems.size() == 0 )* - add in 6 new items * ( listItems.size() == 6 )* - call notifydataSetChanged() ... but the ListView's getCount still returns 12!Why?And is this why the adapter's getCount() returns with an index that's too high for it's dataset? -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: position value in getView seems too big
There's a lot of proprietary code in the adapter; I'll see if I can whittle it down to something simple but meanwhile I'd like to pursue the listview's strange getCount result. Because that's a separate question from the getView()'s position value (maybe) I've started a separate thread focusing on that: https://groups.google.com/forum/#!topic/android-developers/Lx9_3H4RkhM This seems to have stumped them on Stack Overflow and I've never been able to get a clear description of where *exactly* the value in ListView's getCount() actually come from. On Friday, May 16, 2014 2:39:54 PM UTC-4, Streets Of Boston wrote: That is hard to figure out without you posting the code of your adapter and how you create an instance of your adapter. It is probably a subtle bug... -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [android-developers] Re: position value in getView seems too big
The value returned by AdapterView.getCount is cached. https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AdapterView.java#580 It's also updated after you've called notifyDataSetChanged: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/AdapterView.java#794 The preceding call chain is: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/BaseAdapter.java#45 https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/database/DataSetObservable.java#30 I's also updated in a few other places (e.g. ListView.onMeasure), but the right way to notify a list view about changed underlying data in an adapter is notifyDataSetChanged. -- K 2014-05-16 23:03 GMT+04:00 plnelson pna...@gmail.com: There's a lot of proprietary code in the adapter; I'll see if I can whittle it down to something simple but meanwhile I'd like to pursue the listview's strange getCount result. Because that's a separate question from the getView()'s position value (maybe) I've started a separate thread focusing on that: https://groups.google.com/forum/#!topic/android-developers/Lx9_3H4RkhM This seems to have stumped them on Stack Overflow and I've never been able to get a clear description of where *exactly* the value in ListView's getCount() actually come from. On Friday, May 16, 2014 2:39:54 PM UTC-4, Streets Of Boston wrote: That is hard to figure out without you posting the code of your adapter and how you create an instance of your adapter. It is probably a subtle bug... -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[android-developers] Re: position value in getView seems too big
There is no method 'getView' on a ListView class. And i expect i wouldn't return a number However, you do define a *static *field called *listItems*. Do you update the *listItems *field directly without notifying the your *BaseAdapter*? If so, this could be the problem. Notify the *BaseAdapter *by calling 'notifyDataSetChanged()' on it after you make the appropriate changes to its data (*listItems*). Note that you should not make this field (*listItems*) a *static*/class field. Just make it a regular/instance field, fill it with data and send this to your BaseAdapter and let your BaseAdapter handle it. On Thursday, May 15, 2014 2:09:57 PM UTC-4, plnelson wrote: I'm getting a crash in my adapter's *getView()* routine because it's being called with a position value of 6 and my datasource only has 6 items in it. So I assumed that the position parameter should be in a range of [0]-[5]? What determines the range of values in *getView(*)'s position parameter? Details: the XML ... ListView android:id=@android:id/list android:layout_height=match_parent android:layout_width=match_parent android:cacheColorHint=@color/colGrey android:background=@color/colGrey android:clickable=true android:fastScrollEnabled=true android:choiceMode=none/ ...In MyListActivity, which is a ListActivity . . . public static ListView lv; // my ListView in the code ... during *onCreate()* . . . setContentView(R.layout.mylist); lv = getListView(); create the adapter and bind it . . . mylistadapter = new MyListAdapter(MyListActivity.this); setListAdapter(mylistadapter); // bind the adapter ...the data source is an ArrayList called listItems. during the course of running the program its size varies and it may have been 15 earlier in program execution . . . public static ArrayListStringlistItems=new ArrayListString(); ... in my adapter, which is a BaseAdapter, my ovveride of getCount() looks like this . . . @Override public int getCount() { return listItems.size(); } ... when I call *getCount()* in *getView()* it returns 6, which is the number of items in the data source, but if I call lv.*getView(*) it returns 15. (any idea where this 15 is coming from?) Could that be why the adapter is calling *getView()* with index too big? Thanks in advance! -- You received this message because you are subscribed to the Google Groups Android Developers group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups Android Developers group. To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.