Extending BaseAdapter, passing variables - android

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.

Related

Best strategy to create a ListView of forms in Android?

I need to create a dynamic list of forms. The user would be able to input data into each element of the list (which would contain an editable text box and other elements).
What is the best approach? How would I access the inputted information in each list element? Would each list element have to be its own Fragment?
1) Decide what elements you need in each list item
2) Create a data class with the member variables to how your data. one data object will be initialized per listview item //note this is not required, but it can help.
3) Create a layout (list_view_item_layout.xml) for your listview items
4) Declare your ListView in your layout.xml, ensure that you give it an android:id.
5) Declare and initialize an array of your data objects (or just an array of data) that holds all the data objects that you want displayed in your list
5) Create your MyListViewAdapter.class that extends BaseAdapter. this class will inflate your list_view_item_layout and populate the views with the data that is in your data array
6) call ListView#setAdapter(); passing in myListViewAdfapter, context, R.id.document_list_view //I htink that is correct
Thats about all there is to it.
Look for tutorials for custom list view adapters, there are a billion + tutorials

Passed by reference or copy ArrayList

when I share an array list from the Main Activity to an Custom View for example will it be passed by reference or copy, my doubt is if the MainActivity add new elements to this list that was passed the Custom View will get the change. I want to build an application that the main activity will be processing a lot of data and putting this in an array, this array will be passed to a custom view that through canvas will draw a graph with the information inside of the list that was passed, so that is why this information is so important. Any kind of help will be precious, Cheers.
It is passed by reference yes. All objects are. You can read all about it HERE

Populating ListView from Database without Using SimpleCursorAdapter

I have a seperate DatabaseHandler("db") Class which extends SQLiteOpenHelper BUT NOT ACTIVITY which means this is a seperate class file which is not related to any activity. If you ask why i have such a class not in activity because i like keeping my files organized.
So i have listview in a fragment and i have a "DatabaseHandler.getAllRows()" function in that class which returns a "List" Object. Then i want to use this list to populate my listview.
So when this class is a seperate class which is not related to any activity, i can't use simplecursorAdapter since it wants a context in paramteres part when creating with new(i tried to send parameter as context but didnt work) so i need to use something else....
I can change return type, i can use another thing instead of listview.. just give me a good advice for how to populate listview or show table rows NICELY.
If you want a real simple solution, you can store your rows as an array, and use ArrayAdapter to display them. You can override the getView to display the results in your preferred way.
If this solution is not good enough, I think you will have to implement your own Adapter, which might be a good idea anyway.

StableArrayAdapter vs ArrayAdapter

I am looking at this ListView Tutorial:
ListView Tutorial
and I was wondering how much better is to create my own ArrayAdapter, rather than just using and ArrayAdapter.
In the Tutorial it defines a "StableArrayAdapter", what exactly does this means? If I use a regular ArrayAdapter, could it be dangerous for some reason?
The two previous answers are absolutely right, but just to address more directly your question and in case someone else has the same doubt than you; a regular ArrayAdapter is not dangerous at all, the only "problem" is that it might not fulfill your needs, in which case you will have to create your own adapter, as the author of the tutorial did by creating what he called StableArrayAdapter in the end of the ListViewExampleActivity class.
Don't get lost by the name, which I guess comes from the fact that the overwritten method "hasStableIds" always returns true, it doesn't mean that the regular ArrayAdapter creates problems.
ArrayAdapter: It is merely a way to provide data to a ListView. It is also a BaseAdapter that is backed by an array of objects.
CustomAdapter: If if your ListView is a normal and simple ListView (wherein you are having one TextView per item in the list), then the use of ArrayAdapter would be apt.
But it is recommended you to create your own CustomAdapter which extends an ArrayAdapter that you can use for providing data to your ListView. This way you can easily extend your ListView to include more that one TextView or even ImageView (to show images).
CursorAdapter: Cursor Adapter is used when you have Data in a Cursor (typically when you are retrieving data from a database. The Cursor must include a column named "_id" or this class will not work.
If you are using a simple ListView, like merely a TextView per item, then just use the standard ArrayAdapter, on the other hand, if you want a custom item in the list, as in a combinations of views within each item in the ListView, then extend the ArrayAdapter and implement it to your needs.
StableArrayAdapter is merely an extended version of ArrayAdapter, but in StableArrayAdapter they have overridden the method hasStableIds() of BaseAdapter to return true.
You can check this in the following links:
StableArrayAdapter -
Override hasStableIds to return true
ArrayAdapter -
Has not Override hasStableIds but extended BaseAdapter
BaseAdapter -
Has hasStableIds but returning false
Now Question is What is the use of StableIds
This Indicates whether the item ids are stable across changes to the underlying data. If True then same id always refers to the same object. for more info

Binding a class to a view/viewgroup in Android?

In Android I have a class contains 2 properties. I've created many objects from it and put them into an ArrayList, then using ArrayAdapter to bind the List to a TableView as you see. Once the list is changed, the view will be updated.
The question is, how can I bind my properties with these 2 TextViews in a table-row (a so-called ViewGroup in Android)...? I've tried to take data from the list into 2 String-array: dates and values, but that's odd...
In the getView() method of your adapter, use setTag(), passing in the object you want to attach. You can then, for example, retrieve the object in onListItemClick() (assuming you're using a ListView) by calling Log logItem = (Log) view.getTag().

Categories

Resources