Event handle on customized ListView - android

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

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);
}

Method mapping not using an interface method

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?

Object with function that will delete itself

I have a class called LabelButton with an onClickListener. In my Activity class, when the user clicks a button it creates a LabelButton and a String. Then adds the LabelButton to the Layout.
Inside the onClickListener attached to the LabelButton, it calls setVisibility(View.GONE), which takes care of the LabelButton, but I also need to delete that String that was also created earlier.
Is this possible?

start a new activity from button click within listview (using simplecursoradapter)

I have a listview which I am populating with a custom SimpleCursorAdapter, each row contains a button which, when clicked should open a new activity and pass the ID of the original data object so I can display the related image on screen.
I am having problems implementing the onclick event for the button and I understand you can only use startActivity() within an activity - is this correct? if so, is there a workaround as my cursoradapter code is in it's own class which extends SimpleCursorAdapter (ie. not in an activity!)
Just one more question if I may? - how can I pass the dataobject ID (ImageID) of the button clicked to the new activity?
set click listener over Button in getView() method in your adapter and use startActivity from there.

How to simulate a button click using code?

How can I trigger a button click event using code in Android? I want to trigger the button click programmatically when some other event occurs.
Same Problem I am Facing
public void onDateSelectedButtonClick(View v){
/*Something Alarm Management
http://www.java2s.com/Code/Android/Core-Class/Alarmdemo.htm
copied code from this site*/
}
Button code:
<Button
android:onClick="onDateSelectedButtonClick"
android:text="Set notification for this date" />
But I want to call that function OnLoadLayout without OnClickEvent
there is a better way.
View.performClick();
http://developer.android.com/reference/android/view/View.html#performClick()
this should answer all your problems. every View inherits this function, including Button, Spinner, etc.
Just to clarify, View does not have a static performClick() method. You must call performClick() on an instance of View.
For example, you can't just call
View.performClick();
Instead, do something like:
View myView = findViewById(R.id.myview);
myView.performClick();
Just to clarify what moonlightcheese stated:
To trigger a button click event through code in Android
provide the following:
buttonName.performClick();
you can do it this way
private Button btn;
btn = (Button)findViewById(R.id.button2);
btn.performClick();
Just write this simple line of code :-
button.performClick();
where button is the reference variable of Button class and defined as follows:-
private Button buttonToday ;
buttonToday = (Button) findViewById(R.id.buttonToday);
That's it.
Android's callOnClick() (added in API 15) can sometimes be a better choice in my experience than performClick(). If a user has selection sounds enabled, then performClick() could cause the user to hear two continuous selection sounds that are somewhat layered on top of each other which can be jarring. (One selection sound for the user's first button click, and then another for the other button's OnClickListener that you're calling via code.)
Starting with API15, you can use also callOnClick() that directly call attached view OnClickListener. Unlike performClick(), this only calls the listener, and does not do any associated clicking actions like reporting an accessibility event.
If you do not use the sender argument, why not refactor the button handler implementation to separate function, and call it from wherever you want (from the button handler and from the other place).
Anyway, it is a better and cleaner design - a code that needs to be called on button handler AND from some other places deserves to be refactored to own function. Plus it will help you separate UI handling from application logic code. You will also have a nice name to the function, not just onDateSelectedButtonClick().

Categories

Resources