I'm curious if I can create a footer for my custom listview in my arrayAdapter class?
What I'm hoping to get out of this footer are a subtotal, tax, and total textview that will automatically sum and do "math" on my listview's "Order" when new items are applied to it.
Would doing it in the arrayAdapter be the best approach? Or should I do it inside the fragment?
I was trying to do it inside the fragment but was running into difficulty with getting data from the row's of the listview.
Just to clarify; the listview is inside its own fragment, it will be pretty alone aside from the textviews I mentioned above.
If in the fragment, which part of the lifecycle should I put it in? I haven't set up onClickListeners just yet, but I feel putting everything in onCreateView() would be incorrect since I will be adding things dynamically.
Edit: The problem I was having while doing it in my fragment was that the getListviewChildren was returning empty; I'm assuming cause I was using it in my onCreateView?
Instead of implementing it yourself in the adapter you can use ListView's own implementaion for a footer view. just call addFooterView on the list and pass it the view you want as a footer.
ListView listView = .. //create your list, or inflate it from xml...
View footerView = ... //create your view, or inflate it from xml...
listView.addFooterView(footerView);
listView.setAdapter(myAdapter);
2 things you should know though: It is best to call this method before using setAdapter on the list, because on all but the latest version of android calling it after will not have any affect. Another thing is that doing so is implemented by wrapping your adapter inside another adapter, so after using this you cannot use getAdapter() to get your original adapter.
For more info go to: http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29
Related
I have a View in a custom layout of a ListView, now I want to assign an onClickListener to this View (in a Fragment). But if I try
getActivity().findViewById(R.id.rowleft).setOnClickListener( ...
it throws a NullPointerException. Why is this? In the layout file the View has the ID "rowleft" Does the id change when it is in a ListView? Or how do I see which row triggered the event?
What I want is two Views in one Item to know if the user clicked on the left or the right side.
Thank you
Set up your OnClickListeners in your adapter getView() where you're setting up the listview item view anyway.
You can override getView() in an ArrayAdapter. Call super.getView() to get the view to call findViewById() on for the purpose of setting click listeners.
Don't call findViewById() on the activity. Even when the listview items are part of the activity hierarchy, it's not really practical to find views with the same id from different listview items.
ListView.getChildAt(x).findViewById(R.id.rowleft).setOnClickListener( ...
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?
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.
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.
I have a listview with 4 identical rows. Inside those rows, I have a RelativeLayout which contains a TextView (id : R.id.notif). In my Activity, I use my own ArrayAdapter.
I would like to be able to modify the text of the third row. I tried this but it isn't working.
((TextView)listview.getAdapter().getView(2, null, listview).findViewById(R.id.notif)).setText("50");
Thank you.
Do not use adapter.getView() for that! This method is used internally for the adapter to create the view that gets displayed in the list! The correct way to do this is to modify the underlying data and to refresh the list with adapter.notifyDataSetChanged(). Do not try to access views in the list directly, you don't know if they are visible at the moment or scrolled outside the view.
I have a ListView declared inside a layout. When I addFooterView(v) it does NOT display. What are the possible reasons for this? Is there some setting in ListView or my Adapter to make this display immediately? It does eventually display but only after reinitializing the view, adapter and all.
Update: It seems that resetting the adapter finally makes it appear .... Obviously not something that I should have to do everytime I call addFooterView(v).
The view containing the ListView is inside a View maintained by PageViewer so not sure if that effects this or not.
Add the footer view before you set the adapter is the only thing we can replicate with when we are not able to see your code.
I would like to add Top Margin on the first item in the ListView. Is that possible?
Or, how can I reference first list item in the List View?
I tried
lv = (ListView)getListView();
View myRowItem = lv.getChildAt(0);
Probably, it doesn't work, because at the moment I executed it, list items didn't exist yet. I assume I should try above code in something like onPostRender event. Is there something like that in Java Android?
You would have to create your own adapter, and put that condition in the getView method. However, I'd like to know why you would want to add top margin just to the first item... wouldn't it behaves the same if you put that top margin to the ListView itself?
In order to get the items, write your code wherever you set the list adapter.
By the way, in order to set the margin, just set android:PaddingTop of the ListView on the layout file (XML file).
The best place to customize ListItems is its Adapter. Create a customized adapter for your ListView and use the getView method to decide what to do with every ListItem. See an example here: How to repaint listview items when changing orientation