Method mapping not using an interface method - android

How does Android's View Objects call a method inside my activity? For example, when you generate a view (like a button) dynamically, you have to add an click listener and then your activity will implement click listener and then the button object will have a reference to your activity through the interface.
My question though is when you create buttons in the XML file, you can specify an attribute "onClick" and then give a custom method name such as public void button1pushed(View v) how does the button object reference my activity if my activity doesn't implement anything?

Related

Android, accesing a textview from a surfaceview

I have an activity, this activity name is Game, it's xml file is composed by two views control, first of them it's a TextView called Texto and the second one is a SurfaceView created by me called Juego.
The Juego view has an onTouchListener event, and i want to send a text to the control called Texto everytime the user clicks on the control Juego.
I have all the "structure" created but i can't "communicate" from Juego to Texto, every thing i try i get an error.
Thx for help in advance,
Create a listener such as
onViewClick(String text). Create instance of this on your SurfaceView and in constructor get this listener as parameter. Now call this listener wherever you want to update the textView.
In you activity class, implement onViewClick listener and in method do:
public void onViewClick(String text){
TextTo.setText(text);
}

How Fragment implement interfaceListener android

I have one activity which contains buttons if I click any button according to that in my fragment layout changes should happen.
for this I created a interface it has one method public void onclick();
fragment implementing interface it got onclick method. I created reference to interface in activity I am calling this method from activity but not calling onclick in fragment plz help

Calling an empty function from Android layout file crashes App

I have a checkbox in the Android layout file. When the checkbox is clicked i call an empty function in the activity class. This causes the app to stop working. Why is this?
I am assuming that you are using the "onclick" attribute in your XML.
If you are receiving a MethodNotFound exception, it could be one of two things:
You have a typo in either your Activity's method name or your XML's method name, or...
The visibility of your method in your Activity is not public.
When specifying onclick values in XML, the method should look like this in your activity:
public void myOnClickMethod(View v) { ... }

Calling activity method from extended view class in Android

This is a problem I didn't forsee when designing the structure of my applicaton.
Basically i have 3 classes.
ProfileActivity //The main activity for the application.
ProfileView //Which extends Linear Layout so that I can tinker with my own view. Note that this is just part of the whole activity and not the main layout
ProfileData //This is just a class for storing data.
The activity contains multiple ProfileViews which each contain one profileData.
Heres where I'm stuck. My profile View has an on click method assigned which needs to call a populate method in the activity. Unfortunately ````
//I'm inside the activity
public void populateProfileDataForm(ProfileData pd)
{
//edit some text fields and other widgets from here
}
Is there any way to call the activity method from the profileView class?
If not, then the error is in my design and can anyone point me towards a better solution for binding data, view and activitys?
When you create a view, you always need a context, and always pass a activity to it.
So try use below code:
If(m_context instanceof ProfileActivity)
{
ProfileActivity activity = (ProfileActivity)m_context;
// Then call the method in the activity.
}
Or write an interface onProfileViewClickListener such as view.onclickListener. then the ProfileActivity implements it and set it to the ProfileView.
What about if you assign an OnClickListener or OnTouchListener in your ProfileActivity Class and in this Listener you cann call the populate Method. In the OnTouchListener you can get the Position of the TouchEvent so you can assign it to a specific Profile.

Event handle on customized ListView

public class ActivityForShow extends Activity
{
//have a ListView layout
ListView.setAdapter(SingleRowAdapter);
}
public class SingleRowAdapter extends ArrayAdapter
{
//every row has a CheckBox ,a TextView and a Button
}
Now I want to handle CheckBox and Button events,do some background work and show result in a dialog.
But how to getRefences of Checkbox and Button in ActivityForShow?
findViewById() will cause Exception.
I don't think you want to do that in the ActivityForShow class. Instead, register all event handlers inside of your SingleRowAdapter#getItem(int) method (you will have have to override this). In that method you create the view for the given row, so you know what the row is (position) and you can register event handlers for the CheckBox, Button, etc
It sounds like you want to handle the onCheckedChanged event or onClick event in ActivityForShow.
If so, have ActivityForShow create a OnCheckedChangeListener instance and OnClickListener instance (e.g. as local anonymous inner classes) and have ActivityForShow give them to SingleRowAdapter (e.g. as constructor params) to attach to the checkbox and button widgets as their callbacks.
Alternatively, define your own custom handler interface(s) to be called when the button is clicked or the checkbox is checked. Have ActivityForShow to instantiate an instance of the custom interface(s) with code to process the event. Have ActivityForShow give them to SingleRowAdapter. Finally, have SingleRowAdapter create event handlers to attach to the checkboxes and buttons - these event handlers simply call your own custom handlers.
Hope the following link may help you, event on an item of ListView custom row layout.
It's obvious it gives error with finviewbyid method because it is used for when reference by xml ele. You can use this link for study:
http://developer.android.com/reference/android/widget/ArrayAdapter.html

Categories

Resources