Who calls the getView() method of custom ArrayAdapter class - java

While using custom ArrayAdapter in android, the getView() of the custom ArrayAdapter is called by whom? I have searched this question a number of times... found it in StackOverflow but the answer which has been marked as satisfactory by the user is:
for the developer it is not necessary to know that who calls the
getView() of the ArrayAdapter class".
Please, I humbly request that a more meaningful answer will be appreciated!

getView() is called by a subclass of AdapterView. So, if you attach the Adapter to a ListView, ListView inherits from AbsListView, and AbsListView will be calling getView() to get the rows to display in the list.

Related

Update private ListFragment from parent activity

I have a FragmentActivity which creates a ListFragment. Inside the ListFragment is a custom adapter. When I would try to make the adapter public, it wouldn't let me. So, I don't really have access to the adapter or I'm not sure how to get it. I add some data to the listview statically with a public arraylist of objects that I declare early in the lifecycle. However, other data I want to retrieve with AsyncTask from a Database. I get this data in the parent activity and I know right when it is finished fetching the data. So,
Can I update the listView within the ListFragment with access to the arraylist, but not access to the adapter? If so, can I do it from the activity or do I need to somehow call a method in listFragment? If I need to call a method in listFragment, how do I access that method from the activity? If this is not possible without access to the adapter, how do I get access to the adapter without the public keyword? Would it be better to move my AsycTask to my ListFragment?
I know it sounds like a lot, but I'm just wondering how to refresh the listview with those cicumstances. Help is greatly appreciated.
Call to method:
ListFragment fragment = (ListFragment) getSupportFragmentManager().findFragmentByTag("fragtag");
fragment.updateData();
Method that refreshes:
public void updateData(){
((ArrayAdapter)this.getListAdapter()).notifyDataSetChanged();
}
It is in my subclass of ListFragment, but not in any methods.
**Edit:**Now I get the following error:
Error parsing data java.lang.ClassCastException: com.dynamic_internet_solutions.scsinfo.app.SpeakerListFragment$1SpeakersListAdapter cannot be cast to android.widget.ArrayAdapter
**Edit - Solved: ** I did
ListFragment fragment = (ListFragment) getSupportFragmentManager().findFragmentByTag("fragtag");
BaseAdapter adapter = (BaseAdapter) fragment.getListAdapter();
adapter.notifyDataSetChanged();
after I set the tag of my fragment on initialization.
You always have access to the ListAdapter of a ListFragment using the getListAdapter() method. You can easily grab the list adapter and modify the data in it. I'm not sure why you are having trouble making your adapter public. Are you declaring it inline, or as a separate class? What is the error you are getting from the compiler?
As an aside, you might want to read my two articles on the View Holder pattern and making reusable list adapters to make your ListAdapter more awesome.

how to know in the main layout that a ListView has been changed?

Suppose that I have a main layout that has a ListView. This ListView has a custom cursor adapter. When my list change, is there anyway to know, in the main activity, if my list has been changed?. Inside my custom cursor I call to this.notifyDataSetChanged();. Is there any method that can I use to know when notifyDataSetChanged() has been called?.
You need to create a DataSetObserver and register it with your adapter by calling registerDataSetObserver().
p.s. I strongly suggest that you familiarize yourself with the Android API Reference. This is a great tool to help you find the classes and methods which are available in the Android API.

List Adapter and getView function explanation

I'm thoroughly confused about the life cycle of list view. More specifically, what does the list adapter do exactly? Does it just provide data to the given view? And when/where does the getView() function gets called? And what purpose does this getView() function provide? From just looking at the code, it looks like getView() is "assigning" data to the view to be displayed. I'd like to be able to use list views without having to memorize, do this and then this in order for it to work. I'd much rather understand it so I can use it properly. Someone please help me understand all of this.
Also, if someone can explain to me.. what's the difference between BaseAdapter and ArrayAdapter? and any other kind of adapters that comes with Android.
What I have understood is your adapter constructor instantiated by activity and then on activity launch the getView() method is called. the {#param position, view, viewGroup}
position: it refers to the position of the view as given by adapter. Please Note it is different from the position in {OnItemClick(AdapterView adapter, View v, int position,long id)} here position is the list item position. The {position} in {getView()} changes after particular object in the list are displayed again for eg. when you scroll.
view: the view here is the view you want to be presented through getView(). It can be a particular XML layout for each row. So this states clearly that getView is called to plot every row. this view needs to be valid one or another layout (LinearLayout by default) will be selected to maintain uniqueness.
viewgroup: as you might know and as name says will be the container of your #param:view
any other point is appreciated.
getView() fills in the data into the item's view with the given index. The view which is given as a parameter may be a pre-inflated view. If it is not, you have to infalte it yourself.
An ArrayAdapter simply calls setText on the given view with the result of toString() of the object with the respective index from the array. If you override it, you can do more complex stuff, like assigning a picture or filling in more TextViews.
I recommend the following tutorial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi list adaper provides view for listview.
when user scrolls listview at that time getview is called.
getview is used to populate your view with data hence the name adapter.
The Adapter does all the "rember to do this" for you. If you change a list view's backing data structure through the adapter's methods (e.g. "add()") it will fire all the datachanged and update events you'll need for the list view to show the new state of the data.

Android -- how to use onlistitemclick() function when am using a custom list view

Normally when an item is clicked in an listview, onlistitemclick() would get called.Since I am using the get view function to show three text views and an image in each row of the list. so the onlistitemclick() function is not getting called. anyone knows a work around for this??
Thanks in advance.
Sriram.V
I guess you should set setOnItemClickListener to your listview irrespective of your getview() customisation

Android - CursorAdapter-ish Subclass, for ListView

I currently use a bunch of subclassed CursorAdapters with custom layouts throughout my app, populating them with a Cursor returned by an SQLite query, then allowing them to populate my ListView, after setting them with
setListAdapter(new SearchAdapter(this, searchCursor));
Is there anyway I can get the same behavior but instead of passing in a Cursor pass in an
ArrayList<String[]>
Would there be a different class to subclass? (Obviously) Or, should I convert the ArrayList to a Cursor somehow? Or, is there a different method I am missing?
Thanks!
you can do it with an array Adapter (also you can subclass Base adapter) and implement your own way of displaying objects in a list. there are plenty of tutorials on google on how to do that.
Just search ArrayAdapter, Base adapter. :D
Hope this helps,
Take care.

Categories