I'm sending an bean Object from the first to the second View. Other times I use ArrayAdapter when I had an Array with data to show but this time I only have an bean Object that I want to show. What type of Adapter I must to use ?
You do not need to use any adapter, just pass the object itself. You can use the passed object in the activity directly, for setting the contents of the UI controls, for instance. You need adapters when you have a list of items. More information on this is provided on the class documentation of Adapter here: http://developer.android.com/reference/android/widget/Adapter.html
Related
In RecyclerViewFragment Class, there is MovieData, and the Adapter Class stores MovieData.getMovieList( ).
My question is, when Event Handler be triggered,
Add / Delete data directly in RecyclerViewFragment Class
Define add( ) / delete( ) function in Adapter Class and call adapter.add( ) / adapter.delete( ) in RecyclerViewFragment Class
Which way is the better design? Who has the responsibility to take care of data change?
I would suggest to have RecyclerViewFragment containing the data. And create an adapter passing the reference to the data. For example:
mAdapter = new CustomAdapter(mMovieData.getMovieList());
Then in the RecyclerViewFragment just perform the needed actions such as add / remove such as:
mMovieData.getMovieList().add(index);
Think this is the most common way todo it. You do not want to replicate the getMovieList data twice in adapter and RecyclerViewFragment. This will get confusing.
If data change is happening after adapter has been initialized, then it should be handled inside adapter. Adapter also has a nifty function notifyDataSetChanged() which will update item positions inside the adapter when data has changed.
The recyclerview is not concerned with the data, this is all handled by the adapter you have created to present the data. Have a look at the functions here to see what options you have in regard to data changes.
Adapter is the responsible class for filling a list with data ,for example if you have a list that need to be filled with data lets say SCORES then the adapter is the one responsible for filling these scores in the list. on the other side, RecyclerView is reposnsilbe for making the adapter fill it with more speed and effecieny, the recyclerView uses views that are previously used to display data by the adapter and place them in cache for later reuse to display the same type object so that it doesn't have to initialize a new object each time the adapter want to fill item in the list which make it faster.
and here is a link that might help you as well :
Hope my answer have helped you.
So, I am running up against a limitation of the Parse SDK here, and I don't know how to proceed.
I have a ListView that I am setting the adapter to from a subclass of the ParseQueryAdapter, which itself extends the BaseAdapter class.
Normally with an adapter, I pass the list along to the Adapter constructor in order to get access to the list I am dealing with, so that I can call getCount, getItem, etc. by accessing the size/index of that ArrayList.
I can't do that with the ParseQueryAdapter because it takes a ParseQueryFactory as part of the constructor, and it doesn't resolve it's items until they are fetched from the Parse app online...
Now the ParseQueryAdapter does have a List of Objects, which is exactly what I need, expect that it is Private... so even though the getItem() and getCount() methods exist, there is no way - AFAIK - to use them, as I have no access to the dataset that is fuelling my adapter...
Further to this, I would normally use getView(), which returns a position alongside my View, and everything would be snappy. Except Parse overrides that and provides a getItemView() method which resolves the Parse Object that is fetched from the cloud and passes it instead of the integer for position...
So, two questions here:
1) Is there another way in which I can access the list that is feeding an adapter ? I can hack a way to pass the resolved query into a list and pass that into the adapter later, by listening to addOnQueryLoadListener(), but that seems bad design and might take a while, handcuffing my UI.
2) Am I being naive in my understanding of how to use get getItem() and getCount() methods ?
When setting adapter for listView, should I just do listView.setAdapter(new MyAdapater()); or should I keep the adapter as instance variable and set it to null when onDestory() ?
The answer depends on the use case.
If you are going to do data manipulation such as rearranging the order of elements or changing the data dynamically in some way, then its "better" to have an instance variable of your adapter. It will safe you from casting your adapter from ListView getAdapter() method, whenever accessing your adapter.
If you are creating a simple list view consisted of for ex. 10 Strings and you dont plan on doing anything with the data set, then you don't need to keep a reference to your adapter.
It is better to maintain adapter as instance variable because each and every time you have to create new adapter instead of that just change the data and you can call notifyDatasetChanged() so that adapter will be refreshed.
When we use ArrayAdapter we pass something like a list to the super class. But base adapter constructor doesn't have any parameters. How does this class find the data set? I have seen in some examples they just define an array and override the functions without specifying the list as the Dataset. So how does the class understand this is the dataset? What if we define more than one list in the derived class?
Edit:
I think I should clarify my question. When we use ArrayAdapter the dataset is specified and the program knows what to iterate and calls getView for each of them. But in BaseAdapter we only define a list and override 4 functions and it works! My question is why does it work?! we didn't specify the dataset we just specify the getView body and it returns a view. I don't understand how the program finds the dataset.
As the documentation explains it well :
Common base class of common implementation for an Adapter
Means you have to do the implementation.
http://developer.android.com/reference/android/widget/BaseAdapter.html
How does this class find the data set?
It doesn't. Your subclass of BaseAdapter manages the data set.
What if we define more than one list in the derived class?
So long as you implement the abstract Adapter methods properly (getCount(), getView(), getItem(), getItemId(), ...), how you manage your own data is up to you.
Well after more considerations on the codes I think this is how base adapter works:
It loops the getView() function with 'position' parameter to be from 0 to what is returned by getCount().
we must override getCount() and send the correct index of the last item of dataSet. Each time getView() is called we can work with the views and any list we want according to the current position.
And I think the main difference between ArrayAdapter and BaseAdapter is that ArrayAdapter finds the last index of the list when we pass it to the super class but in base Adapter we should define the last index. Implementation of getView() is the same and we can use any list we want in getView. The trick was only about position parameter.
I'm not understanding how you pass variables to these adapter classes. Most of the examples I've seen, you pass a context (Activity, usually). How exactly do you pass the variables, the data that you want your view to display?
BaseAdapter is used to connect a data source to view. Each item in the row needs to be connected to the property of the object. Take a look # this blog post - http://dup2.in/2011/08/08/android-custom-listview-baseadapter-tutorial/
The simple listview example is having only one textview in each row which corresponds to a single String element in arraylist.