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
Related
I have implemented a firebase list adapter to load a list of items (List A). My data structure is setup such that within List A, each item also contains a reference id to some information located somewhere else (isNice) in the database. Likewise:
ListA
- ObjA
- title : "hi"
- id : "ObjAid"
isNice
- ObjAid : "true"
I'm currently using another database operation to look up the id in the "isNice" child, passing the "ObjAid", the list position, and then a resultReceiver to get the result back. My problem is, when the resultReceiver get a resultData (the value "true"), I have no idea how modify the data within firebase list at the specific position.
My past experience is to use load my data into my own ArrayList and create a custom adapter for the listView, in that case, I could easily update the populated view as extra information is loaded. I would like to avoid setting up my own ArrayList to store the data for the time being, favoring the simplicity of the FirebaseListAdapter. All tips are appreciated, thx :)
After some trial and error, I've instead gone with the RecyclerView + FirebaseRecyclerViewAdapter approach. Using recycler is better for the long run too, in my use case the list could get to 3K+. Using RecyclerView.findViewHolderForAdapterPosition() I can reference the specific itemView after I get the data from the result receiver likewise:
// Reference the Item's view at the specific position
myViewHolder itemViewHolder = recyclerView.findViewHolderForAdapterPosition(position);
// Reference and set the views and here......
After working on an app for a while I realize I use
adapter.clear()
and
arraylist.clear()
I can see both are working just fine, I would like to know the difference between the two!
Both are called before I start and asyncTask that updates my list with information from my server!
You should not be clearing the ArrayList directly. The ArrayAdapter makes absolutely no guarantees that it maintains the same referenced list given to it. In fact it will change when you perform a search with it's filter. Which would make arrayList.clear() fail.
Rule of thumb, if you ever need to mutate or retrieve the associating data...do it directly from the adapter. Not the list you used to construct it.
Adapter = it contains copies of diff views,arrays
aaraylist holds the data which we want to display in our view.
ex: arraylist<HashMap<String,String>> ah= new ArrayList<HashMap<String,String>>();
the above list contains hashmap
if i clear the arraylist there will be no data to show on listview or gridview so it will be empty
if i clear adapter than it will destroy the copies of array and views so the output will be same
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().
I have a set of View instances that each represent some different data object (derived from Java.Lang.Object). I associate each view with its data object by setting the view's tag reference. The views can generate context menus, and in the onCreateContextMenu function I can get the source view and then get the data object from its tag.
My problem is that I can't find a way to associate the data object with the created menu or menu item such that I can get the data item in onContextItemSelected().
How do I propagate the data item to onContextItemSelected()?
UPDATE
From the link posted by #asktomsk it looks like what I want to do is only possible (without a lot of subclassing) if the originating view is a ListView. Having also read this on the android developer site I suspect that long-tough context menus are probably not advisable anyway, and that I should find a different mechanism.
Check the MenuItem.getMenuInfo() data. It contains information about context menu caller.
Some explanation you may found here:
Identifying the view selected in a ContextMenu (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.