So this one is probably very simple, but I'm having a bit of trouble figuring it out.
So I have a custom View, which is now running in an activity along with a standard TextView widget.
What I want to know is whether there is a way to update the TextView from my custom View class
Define a listener interface in the custom view. Define a setXxxListener() method in the view. Override the listener in the activity class. From the custom view, call the listener when needed. In the activity's listener implementation, update the TextView.
Such is the Java way.
In general, view classes should not make assumptions about other views running along. The object that manages both views - in your case the activity - should coordinate the data exchange between them.
Related
I understand the idea and usage of Viewholder pattern, but still I have one question:
Assume we have one TextView in the viewholder, and 10 items to display ("item0, item1....").
If I call findViewById once, as I understand I have one object of that TextView.
So at first call to getView I inflate the view, find the reference and set text "item0".
At second call I get same TextView and set text "item1" to the same TextView.
Why item 0 text doesn't change?
Is there any cloning in the background?
Is there any cloning in the background?
Android preallocate a number of views that are enough to fill the screen of the device where you are running the app ( a pool of views ), identical from the content perspective but differently from the reference perspective
Assuming that you implement your ViewHolder inside an adapter class and you use the holder in the getView() method, the only thing that is for sure, is that the TextView in your case , describe a slot of the parent structure (e.g. ListView). Once you have defined the slot in an xml, that is inflated from your adapter, there is no cloning or something like that.
According to Google Documentation the holder idea is described as :
Your code might call findViewById() frequently during the scrolling of
ListView, which can slow down performance. Even when the Adapter
returns an inflated view for recycling, you still need to look up the
elements and update them. A way around repeated use of findViewById()
is to use the "view holder" design pattern.
A ViewHolder object stores each of the component views inside the tag
field of the Layout, so you can immediately access them without the
need to look them up repeatedly. First, you need to create a class to
hold your exact set of views.
There is not cloning , only reusability of the view
I would like to know, from where we can know about what listener a View can implement. For Ex. there is no meaning in implementing onItemSelectedListener in custom Edit Text view class.
You should refer to the documentation (View, for example), there's a Nested Classes section that describes a list of interfaces that every View provides. For example, View class the OnClickListener interface, means every subclass of View would also provide it. And AdapterView (which is a parent of a ListView) provides the OnItemSelectedListener, means ListView will provide both OnClickListener and OnItemSelectedListener. Hope this helps.
You could take a look at the documentation, under the Inherited Methods.
I have a serious problem with a custom view i use in an expandableListView. I use an adapter that extends BaseExpandableListAdapter. The custom view changes its state depending on if it is being consulted or modifying. The state change involves animation and show/hide of ui parts.
The problem is that even thought i let only two views, when i click on the second view i order to make it change its state, the adapter calls the getViewGroup fo the two views but systematically inverting the corresponding model data ids.. .witch makes the ui to animate again...
I precise that i don't use the viewholder pattern since the custom view has its own internal references to the controls to be updated.
Any idea would be appreciated?
i found out the solution, everytime the model changes i called notifyDataChanged() which makes the entire listview to be redrawn
I want to do something like mylistview.setElementsofView(0).getElementById.setColor("black");
currently the only way I know of doing this is setting up a custom list view adapter, setting up constructors, do conditional checks to see if view is populated, then do stuff in that view
this seems very convoluted, when other parts of the listview are so easily accessible.
is there some inherited function I can access in listview to allow me to access the elements of a particular view without a custom adapter?
Thanks for any insight
The short answer to your question:
is there some inherited function I can access in listview to allow me to access the elements of a particular view without a custom adapter?
unfortunately is no.
Why do you think setting up a custom adapter is so convoluted? Just make the customized adapter class a nested class within your activity. Most likely, you'd only be looking at overriding the getView() method. In the end, you'll spend a lot less time doing this than looking for a "simple" solution.
What about
myListView.getChild(index).setColor("black");
I have a scenario below, and was wondering if anyone had a good solution. Basically, I have a class which manages a custom ViewFlipper object. One of these views within the ViewFlipper instance consists of a custom ScrollView with custom rows (which reside in mRows an ArrayList of the rows). In the custom ScrollView class I have the loop...
for (CustomRow : mRows)
{
row.setOnTouchListener(this);
}
In the onTouch listener I want to be able to notify the top level view (the object that holds the reference to the ViewFlipper instance). What is the most efficient way to do this?