I've been learning GWT the past couple months and found out that the Mvp is one of the best ways to design your project. I've read google's tutorial MVP part 1
and in their tutorial they put the clickHandlers ( for example) in the presenter.
Now I had problems with that when constructing many view class that has many buttons with the same HTML id, and then the user interacts with these buttons... so if I have one button for every view, total 6 button. and the user clicks on one of them, the button will work 6 times for the same object...
So I read and found out that it is better to put the handlers on the view class and create an event to the presenter.
So that what I did :
View Class :
rb0.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(1);
System.out.print("rate 1");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(2);
System.out.print("rate 2");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(3);
System.out.print("rate 3");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(4);
System.out.print("rate 4");
}
});
rb1.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
selectHandler.onEvent(5);
System.out.print("rate 5");
}
});
Presenter class : (event handler)
private void bind() {
.
.
.
DoEvent selectHandler = new DoEvent(){
public void onEvent(int select) {
fetchRating(select, user.getUserId());
}
};
display.setSelectHandler(selectHandler);
The call for the Presenter with it's view, it is called from the MainPagePresenter class :
presenter = new AssetViewPresenter(rpcService,eventBus,new AssetView(),result.get(i));
now my problem is that when I click the buttons from the view nothing happens... like the presenter and the view are not connected, what could be the problem ?
Sorry it was a stupid bug... my button are rb0, rb1 ,rb2 ... and I copied the click handlers with the same button id !!! YUP I'm ashamed of myself
Related
I have this code for a login view:
#SuppressWarnings("serial")
#Route(AppConst.LOGIN_PAGE)
#PageTitle("Login")
#Viewport(AppConst.VIEWPORT)
public final class LoginView extends LoginOverlay
implements BeforeEnterObserver, ComponentEventListener<AbstractLogin.LoginEvent>{
public LoginView() {
LoginI18n i18n = LoginI18n.createDefault();
i18n.setHeader(new LoginI18n.Header());
i18n.getHeader().setTitle("App");
i18n.getHeader().setDescription("");
i18n.setAdditionalInformation(null);
i18n.setForm(new LoginI18n.Form());
i18n.getForm().setSubmit("Sign in");
i18n.getForm().setTitle("Sign in");
i18n.getForm().setUsername("Email");
i18n.getForm().setPassword("Password");
setI18n(i18n);
setForgotPasswordButtonVisible(false);
addLoginListener(this);
}
#Override
protected void onAttach(AttachEvent attachEvent) {
System.out.println("on attach called");
}
#Override
public void onComponentEvent(LoginEvent event) {
// do something
}
#Override
public void beforeEnter(BeforeEnterEvent event) {
setOpened(true);
}
}
When I run it the onAttach() method is called twice, I read on attach called two times on output.
The view is instantiated from another view via a beforeEnter(BeforeEnterEvent event) using event.forwardTo(LoginView.class).
Why ? is this a bug?
Thanks for the help.
listener = new RevMobAdsListener() {
#Override
public void onRevMobAdClicked() {
Log.i("[RevMob]", "Advertisement Clicked!");
revmob.openAdLink(application, APPLICATION_ID, this);
return;
}
#Override
public void onRevMobAdDismiss() {
Log.i("[RevMob]", "Advertisement Closed!");
fullscreenAd.hide();
}
#Override
public void onRevMobAdDisplayed() {
Log.i("[RevMob]", "Advertisement Displayed!");
}
#Override
public void onRevMobAdNotReceived(String message) {
Log.i("[RevMob]", "No Advertisement Available!");
}
#Override
public void onRevMobAdReceived() {
Log.i("[RevMob]", "Advertisement Pulled from network!");
}
#Override
public void onRevMobSessionIsStarted() {}
#Override
public void onRevMobSessionNotStarted(String arg0) {}
};
So what the problem is, is that once I click on the advertisement it continuously opens tabs in my browser.
LogCat spams the following debug messages. (In order)
Advertisment Pulled from network!
Advertisement Displayed!
Reporting impression using testing mode: with_ads
Advertisement Clicked!
^The above is apmmed on repeat, everytime it opens a new tab
Look at your first listener (onRevMobAdClicked): when you click the first ad, this listener is called, and it calls an adlink.
By default (from RevMob SDK), when called (openAdLink), the link calls automatically onRevMobAdReceived, onRevMobAdDisplayed and onRevMobAdClicked listeners.
Then, this adlink is opened and it fires this same listener (onRevMobAdClicked), calling again the adlink, which will fire the listener, which will call again the adlink, ..........
You have to change the way you call the link.
Also, you don't need to call fullscreen.hide() in onRevMobAdDismiss. When the fullscreen is dismissed by the user, this event is fired.
Hope that helps!
I searched on Google and StackOverflow but I did not find really what I need. I want to disable a button runtime in a click event but it does not work. I have 2 buttons whose names are ButtonA and ButtonB
My code:
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setEnabled(false);
ButtonA.invalidate();
ButtonB.setEnabled(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setEnabled(false);
ButtonB.invalidate();
ButtonA.setEnabled(true);
ButtonA.invalidate();
}
});
I use onclick listener events in my list adapter class
If I refresh the screen it works but I want to do it runtime. When I click the button I want to see it will be disabled.
How can I fix this?
You can use clickable() method for this.
ButtonA.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonA.setClickable(false);
ButtonA.invalidate();
ButtonB.setClickable(true);
ButtonB.invalidate();
}
});
ButtonB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
ButtonB.setClickable(false);
ButtonB.invalidate();
ButtonA.setClickable(true);
ButtonA.invalidate();
}
});
Or you can try this:
Button b=(Button) findViewById(R.id.btnB);
b.setEnabled(true);
The thing is :
it should be defined by final,
like this;
final Button ButtonA,ButtonB;
I need to create 2 generic button called yes and no with 2 return 0 if no 1 if yes. I see the onclick method is void and not return int, how can i do?
YesButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
??? result ??
}
});
That's not the way to do it. Not knowing your specific requirement makes it a little hard, but I'll venture a suggestion. First define a controller/mediator/whachamacallit with the operations that the view can perform:
public interface MyListener
{
void onYesClick();
void onNoClick();
}
(Could be a concrete class also, but yes and no clicking seems very generic, so we could reuse that elsewhere)
In your view class you would then have
public class MyView
{
private MyListener listener;
private Button yesButton = new Button( "yessir!" );
private Button noButton = new Button( "no way!" );
public MyView( MyListener listener ) { this.listener = listener; }
yesButton.addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
listener.onYesClick(); // similarly .onNoClick() for the "No" button
}
} );
// etc
...
}
Hope that helps you a bit further.
For example I want to execute something when user clicks on a button. Which do I use? The documentation didn't appear to make it very clear
UPDATE
A quick test shows that Widget Selected is triggered but not Default Selected.
In TasksView.main()
TasksView view = new TasksView(shell, SWT.None);
TasksController controller = new TasksController(view);
In TasksController
public class TasksController extends ControllerAbstract {
protected TasksView view;
public TasksController(TasksView view) {
this.view = view;
view.addTaskListener(new AddTaskListener());
}
protected class AddTaskListener implements SelectionListener {
#Override
public void widgetDefaultSelected(SelectionEvent arg0) {
System.out.println("Default Selected");
}
#Override
public void widgetSelected(SelectionEvent arg0) {
System.out.println("Widget Selected");
}
}
}
btw, Did I do MVC correctly?
Use widgetSelected. In fact, all the better is to simply extend SelectionAdapter and only override the widgetSelected method and completely ignore widgetDefaultSelected.
SelectionListener.widgetDefaultSelected(e) has a toolkit dependent behavior. I usually just invoke SelectionListener.widgetSelected(...). (Note that this is not the default in SelectionAdapter.widgetDefaultSelected(e) - you will have to do this yourself.