0

I have a custom adapter derived from ArrayAdapter that overrides getView() that we'll call CustomObjectAdapter. I manually add Views created by the adapter to a LinearLayout like below:

m_adapter = new CustomObjectAdapter(this.getApplicationContext(), 
                                    R.layout.custom_object_layout, 
                                    m_customObjectArrayList);
m_linearLayout = (LinearLayout)findViewById(R.id.custom_object_list_linear_layout);

for (int i = 0; i < m_adapter.getCount(); i++) {
    // Create the view for the custom object
    View view = m_adapter.getView(i, null, null);
    m_linearLayout.addView(view);
}

I populate a LinearLayout with Views instead of using a ListView, because I it's contained within a Fragment in which I want the entire area to be scrollable--rather than just the area within the ListView. I found this solution in this StackOverflow question: Using a ListAdapter to fill a LinearLayout inside a ScrollView layout.

In the same class that these Views get populated, the data used in the adapter's getView() method is updated and then I call m_adapter.notifyDataSetChanged(). The getView() function is not getting called subsequently, however. In another area of the code I am updating a adapter that's hooked up to a ListView in the same manner (instead of a LinearLayout), and it is updating appropriately. Does anyone know what I'm doing wrong?

Community
  • 1
  • 1
jokeefe
  • 1,836
  • 4
  • 22
  • 27
  • An alternative rather than a solution, but you might consider the `addHeaderView` and `addFooterView` in [`ListView`](http://developer.android.com/reference/android/widget/ListView.html) in the future when you want non-list items to also be scrollable. – blahdiblah Nov 15 '13 at 04:27
  • @blahdiblah Thank you--I coincidentally began refactoring my code exactly as you describe using the `addHeaderView` method immediately after posting this question, and it works perfectly now! If you add your above comment to your answer, I'll accept it as the solution. – jokeefe Nov 15 '13 at 04:46

2 Answers2

1

LinearLayouts aren't built to use adapters. The reason getView isn't being called is because there's no view connected to the adapter to tell it that new views need to be generated.

In your code there's no direct connection between the adapter and the view as there is when you call setAdapter on a ListView. As far as the LinearLayout's concerned, the views in it could be coming from anywhere. Similarly, the adapter can't really do anything with the news that its dataset changed because it's not connected to anything that asks it to generate views.

Views are regenerated with the ListView because it registers a DataSetObserver with the adapter and then requests new views from the adapter when notified of changes via that observer.

There's no comparable mechanism for a LinearLayout, so your only real option is to empty the views out of it and put them in again manually like you do initially.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
1

An alternative rather than a solution, but you could use the addHeaderView and addFooterView methods in ListView to make non-list items also be scrollable.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • Thank you! This solution was exactly what I was looking for. I also appreciated the explanation you provided in your other answer! – jokeefe Nov 15 '13 at 04:58