onScroll of Android's ListView is not called - android-listview

I'm new to Android and Java.
I created a custom ListView as follows:
public class CustomEndlessListView extends ListView implements
OnScrollListener, android.widget.AdapterView.OnItemClickListener, IReceiver {
and then I clicked the 'Add unimplemented methods' option on Eclipse.
This result in the following method:
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.d(TAG,"onScroll !!!!!");
}
with no #Override annotation.
If I manually add the #Override annotation I get :
Multiple markers at this line
- implements android.widget.AbsListView.OnScrollListener.onScroll
- The method onScroll(AbsListView, int, int, int) of type CustomEndlessListView must override a superclass method
When I run the application (without the #Override annotation) this method (onScroll) is never called. Any ideas?
Thanks in advance.

You must register for scroll events by calling the setOnScrollListener. Since you have implemented the OnScrollListener interface in your ListView subclass (which is not the only possibility) you can try something like the following:
public class MyActivity extends ListActivity {
#override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_layout);
CustomEndlessListView lv = new CustomEndlessListView(this);
lv.setOnScrollListener(lv);
setListAdapter(lv);
}
}
Alternately you might register for the events in your CustomEndlessListView constructor:
public CustomEndlessListView(Context context) {
// ....
setOnScrollListener(this);
// ....
}

Related

Setting a WorkbenchWindowControlContribution as a SelectionProvider

I added a control in the toolbar which extends WorkbenchWindowControlContribution and implements ISelectionProvider. This control contains a Combo and when I change the selection I want to notify another view. Because it's not a ViewPart I can't call getSite().setSelectionProvider directly. I tried to set it like this:
PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite().setSelectionProvider(MyControl.this);
});
If I add this to the protected Control createControl(Composite parent) method the getActivePart would always be null. I also tried registering it when the Combo's value changes for the first time, but that way my other View wasn't notified about the change event.
Here is the ISelectionProvider methods:
#Override
public void addSelectionChangedListener(ISelectionChangedListener listener) {
listeners.add(listener);
}
#Override
public ISelection getSelection() {
return new StructuredSelection(comboBox.getText());
}
#Override
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
listeners.remove(listener);
}
#Override
public void setSelection(ISelection selection) {
for (ISelectionChangedListener listener : listeners) {
listener.selectionChanged(new SelectionChangedEvent(this, selection));
}
}
The View that should be getting notified implements ISelectionListener. I register it in the public void createPartControl(Composite parent) with getSite().getPage().addSelectionListener(this);
And here is the override of the SelectionChanged too:
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (part instanceof MyControl) {
String selectedProtocol = ((StructuredSelection) selection).getFirstElement().toString();
}
}
Can it be done?
Thanks!

GWT's Editor Framework and GWTP

building on this answer, i try to integrate the GWT editors into a popup presenter widget. What is the right way to do that?
My view looks like this:
public class DeviceEditorDialogView extends
PopupViewWithUiHandlers<DeviceEditorDialogUiHandlers> implements
DeviceEditorDialogPresenterWidget.MyView {
interface Binder extends UiBinder<PopupPanel, DeviceEditorDialogView> {
}
public interface Driver extends SimpleBeanEditorDriver<DeviceDto, DeviceEditorDialogView> {
}
#Inject
DeviceEditorDialogView(Binder uiBinder, EventBus eventBus) {
super(eventBus);
initWidget(uiBinder.createAndBindUi(this));
}
#Override
public SimpleBeanEditorDriver<DeviceDto, ?> createEditorDriver() {
Driver driver = GWT.create(Driver.class);
driver.initialize(this);
return driver;
}
}
and my presenter looks like this:
public class DeviceEditorDialogPresenterWidget extends PresenterWidget<DeviceEditorDialogPresenterWidget.MyView> implements
DeviceEditorDialogUiHandlers {
#Inject
DeviceEditorDialogPresenterWidget(EventBus eventBus,
MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
/**
* {#link LocalDialogPresenterWidget}'s PopupView.
*/
public interface MyView extends PopupView, DevicesEditView<DeviceDto>, HasUiHandlers<DeviceEditorDialogUiHandlers> {
}
private DeviceDto currentDeviceDTO = null;
private SimpleBeanEditorDriver<DeviceDto, ?> driver;
public DeviceDto getCurrentDeviceDTO() {
return currentDeviceDTO;
}
public void setCurrentDeviceDTO(DeviceDto currentDeviceDTO) {
this.currentDeviceDTO = currentDeviceDTO;
}
#Override
protected void onBind() {
super.onBind();
driver = getView().createEditorDriver();
}
//UiHandler Method: Person person = driver.flush();
}
Is this the right approach? What is missing? Currently nothing happens when i try to use it like this:
#Override
public void showDeviceDialog() {
deviceEditorDialog.setCurrentDeviceDTO(new DeviceDto());
addToPopupSlot(deviceEditorDialog);
}
showDeviceDialog is in the parent presenter and called when clicking a button in that parent Presenter, that instantiates the dialog with private final DeviceEditorDialogPresenterWidget deviceEditorDialog;
Thanks!
Here are a few key points that are missing from your code above:
Your DeviceEditorDialogView should implement Editor<DeviceDto>. This is required in order for the fields of DeviceEditorDialogView to be populated with data from you POJO.
Your DeviceEditorDialogView should have child editors that are mapped to fields in your POJO. For example, given the field deviceDto.modelName (type String), you could have a GWT Label named modelName in your DeviceEditorDialogView. This Label implements Editor<String> and will be populated with the modelName from your DeviceDto when you call driver.edit(deviceDto)
You should call driver.initialize(this) only once, in DeviceEditorDialogView's constructor
You should override onReveal() like this:
#Override
public void onReveal() {
super.onReveal();
driver.edit(currentDeviceDTO); // this will populate your view with the data from your POJO
}
This method will be called when the popup is displayed, just after your DeviceEditorDialogPresenterWidget has been addToPopupSlot

How to list all classes in autocompletion inside annotations in Intellij IDEA?

I have custom class InternalTimerServiceController in my application. I want to use it in another class inside android annotations. And it seems that autocompletion does not work correctly in this case.
I have this interface
public interface InternalTimerServiceControllerContract
{
void doWork();
}
And this class
#EBean
public class InternalTimerServiceController implements InternalTimerServiceControllerContract
{
#Override
public void doWork()
{
// do work
}
}
And this is my Activity
public class MyActivity extends Activity
{
// try uncomment line below and see if autocomplete works properly
//#Bean(Internal)
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
It's a bug, thanks for reporting. I've created a ticket for it: http://youtrack.jetbrains.com/issue/IDEA-98298

What is the proper way to use gwt-bootstrap modal with gwt-platform?

I am constructiong an webapp with Google Web Toolkit using GWT-Platform and GWT-Bootstrap frameworks. Mostly it has been almost flawless until I tried to implement a popup. These frameworks' undestanding of popups seems to be quite different.
GWT-Platform expects a popup widget itself to be an instance of com.google.gwt.user.client.ui.PopupPanel when using the GWTP's RevealRootPopupContentEvent.fire(source, content) or a presenter's addToPopupSlot(child) method.
GWT-Bootstrap's Modal is used like any other widget that is added to the underlying panel but my goal is it to have a separate presenter and view and to possibly fetch it asynchrously with AsyncProvider.
I have tried to make it as a PresenterWidget and using addToSlot(slot, content) to reveal it but it doesn't look quite right. Not all of the styles are applied this way and the close icon (×), doesn't work for example.
I think I am not the first one trying to do something like that so maybe someone has figured out a proper way to make it work.
Thanks!
You have to create a view:
public class MyPopupView extends PopupViewImpl implements MyView {
protected Widget widget;
public interface MyPopupViewUiBinder extends
UiBinder<Widget, MyPopupView> {
}
#UiField(provided = true)
Modal dialogBox;
private MyPresenter presenter;
#Inject
public MyPopupView(final MyPopupViewUiBinder uiBinder,
final EventBus eventBus) {
super(eventBus);
setUpDialog(); // Provides UiField => Before initWidgets
initWidget(uiBinder.createAndBindUi(this));
}
// DialogBox must be overridden to let the presenter handle changes onUnload
private void setUpDialog() {
dialogBox = new Modal() {
#Override
protected void onUnload() {
MyPopupView.this.hide();
}
};
dialogBox.setTitle("Some title");
}
#Override
public void setPresenter(final MyPresenter presenter) {
this.presenter = presenter;
}
#Override
public final void hide() {
dialogBox.hide();
presenter.hide();
}
#Override
public void setAutoHideOnNavigationEventEnabled(final boolean autoHide) {
// TODO Auto-generated method stub
}
#Override
public void setCloseHandler(
final PopupViewCloseHandler popupViewCloseHandler) {
// TODO Auto-generated method stub
}
#Override
public void setPosition(final int left, final int top) {
// TODO Auto-generated method stub
}
#Override
public void show() {
dialogBox.show();
}
#Override
public void center() {
dialogBox.show();
}
#Override
public Widget asWidget() {
return widget;
}
protected final void initWidget(final Widget widget) {
this.widget = widget;
}
}
And a UIBinder file:
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'
xmlns:b='urn:import:com.github.gwtbootstrap.client.ui'>
<b:Modal title="Some Title" ui:field="dialogBox">
<!-- Your content -->
</b:Modal>
</ui:UiBinder>
Your gwtp popup presenter has a view that extends PopUpViewImpl which implements PopupView, and uses a lot of the methods of that interface for displaying the popup (asPopupPanel(), show(), center(), etc).
I'm just starting to get to know gwt-bootstrap (looks great +caalos0), but it seems that Modal doesn't implement PopupView, and therefore cannot be passed to addToPopupSlot in a way it would be displayed automatically by gwtp.
as for the addToSlot() issue, are you using RootLayoutPanel or RootPanel?
it could be the reason for addToSlot not working properly, since the gwt-bootstrap Modal widget is attached to the RootPanel on initialization, this can cause weird layout behavior along with an application using RootLayoutPanel as base.
I would try to extend the Modal component, let it implement PopUpView, add it as a field on the PopUpViewImpl attached to your popup presenter, and override the PopUpViewImpl asPopupPanel() function to return the new extended Modal.
Based on the answer by #dominik I did some improvements, see my Gist. It contains some abstract base classes that can be used for any Modal/PopupView implementation. It's a bit more complex but also cleaner because we don't pass the whole Presenter to the View. The interface for the View to interact with the Presenter when the modal is closed is HasModalUnbind.
You would use these classes as follows. Example presenter:
public class ErrorModalPresenter extends ModalPopupPresenter<ErrorModalPresenter.MyView> {
public interface MyView extends ModalPopupView {
DivElement getErrorMessage();
}
private final ErrorEvent error;
#Inject
public ErrorModalPresenter(final EventBus eventBus,
final MyView view,
#Assisted final ErrorEvent error) {
super(eventBus, view);
this.error = error;
}
#Override
public void unbindModal() {
ErrorDismissEvent.fire(this, this);
}
#Override
protected void onBind() {
super.onBind();
//noinspection ThrowableResultOfMethodCallIgnored
getView().getErrorMessage().setInnerText(error.getCause().getMessage());
}
}
Example view:
public class ErrorModalView extends ModalPopupViewImpl implements ErrorModalPresenter.MyView {
#UiField(provided = true)
Modal errorModal;
#UiField
DivElement errorMessage;
interface Binder extends UiBinder<Widget, ErrorModalView> {}
#Inject
public ErrorModalView(final EventBus eventBus,
final Binder uiBinder) {
super(eventBus);
errorModal = initModal();
initWidget(uiBinder.createAndBindUi(this));
}
#Override
public DivElement getErrorMessage() {
return errorMessage;
}
}
And the UiBinder XML just for the record:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
xmlns:g='urn:import:com.google.gwt.user.client.ui'
xmlns:b='urn:import:com.github.gwtbootstrap.client.ui'>
<b:Modal ui:field='errorModal' title='Error'>
<g:HTML>
<div ui:field='errorMessage'/>
</g:HTML>
<b:ModalFooter>
<b:Button text='Close' dismiss='MODAL'/>
</b:ModalFooter>
</b:Modal>
</ui:UiBinder>
In unbindModal() of ErrorModalPresenter I fire an event which is caught by the parent presenter of ErrorModalPresenter. There the modal presenter is removed from a container and then unbind() is called on the presenter. Of course any other solution is possible in unbindModal().
The base classes assume that modals are one-shot modals that will be removed once they're hidden. This behaviour can be changed in initModal() of ModalPopupViewImpl.
I believe you will have to made some Glue Code to made it works.
I never used GWT-Platform popups, so I dont know exactly how, but I believe you will have to made a new Class extending PopupPresenter, and made what's needed to made it work.
Also, I was thinking about GWT-Platform days ago... and I'm pretty sure that when first release of GWT-Platform is out, I'll create a new project to made these necessary glue codes.
If you need any help with this, please contact me.
Thanks, sorry about the poor gwt-platform support.

Editor not getting ListBox changes

Issue: On load of a form, ParentEditor, a sub-editor, ThisEditor, properly
popluates all its fields including a listbox(dropdown) widget, MyWidget. However,if I select a new option in the listbox and save, it doesn't save the newly selected option; though edits to other widgets are saving fine. It appears that on a driver flush, the editor does not get the value in my listBox. In debug mode, on driver.edit, I can see the TakesValueEditor call setValue(value) on all form widgets including the listbox. But on flush, I can see the TakesValueEditor call its getValue() on other form widgets but
not on my listbox.
The editor hierarchy: ParentEditor > ThisEditor > MyWidget. ParentEditor is the entire form. ThisEditor is a sub-section of the form. MyWidget is a custom listbox in ThisEditor section.
I'm using MVP pattern. Below are sample code snippets of the View and Presenter:
VIEW:
/** ThisEditor is a sub-section of ParentEditor (the Form) and contains a
MyWidget (custom listbox). */
public class ThisEditor extends Composite implements Editor<ThisProxy>, ThisView {
... //rfeDriver interface defined and created here
#UiField
MyWidget my; //getMy and setMy methods in ThisProxy
... //other field declarations
public ThisEditor() {
initWidget(binder.createAndBindUi(this));
}
#Override
public MyView getMy() {
return my;
}
... //other methods
}
/** This is the View interface that MyWidget implements */
public interface MyView extends HasOptions, HasValue<MyProxy>, Focusable {
interface Presenter {
...
}
...
}
public class MyWidget extends Composite implements MyView,
IsEditor<LeafValueEditor<MyProxy>> {
...
#UiField
ListBox listBox; //single-select dropdown
...
public MyWidget() {
initWidget(binder.createAndBindUi(this));
addChangeHandler(); //listen to changes to listBox and setSelectedIndex (?)
}
...
#Override
public int getSelectedIndex() {
return listBox.getSelectedIndex();
}
#Override
public void setSelectedIndex(int index) {
listBox.setSelectedIndex(index);
}
...
/**
* Called by the TakesValueEditor on rfeDriver.edit.
*/
#Override
public MyProxy getValue() {
//ask presenter for the MyProxy object -- presenter calls
//getSelectedIndex() on this widget and returns the object associated
//with the index
return presenter.getValue();
}
/**
* Called by the TakesValueEditor on rfeDriver.flush.
*/
#Override
public void setValue(MyProxy value) {
//pass the value to the presenter to parse and set the index that corresponds
//to this object
presenter.setValue(value);
}
PRESENTER
public class MyPresenter implements MyView.Presenter,
ValueLookupCompleteEventHandler {
...
protected HasOptions view;
private List<MyProxy> myList;
public MyPresenter(ParentPresenter parent) {
//setParent for this child presenter
}
... //methods to set view and create association between view and presenter
#Override
public MyProxy getValue() {
//identify the current selection
String selectedId = view.getValue(view.getSelectedIndex());
if (selectedId != null) {
//iterate myList to find the MyProxy object whose id.equals(selectedId)
for (Iterator<MyProxy> i = myList.iterator(); i.hasNext();) {
MyProxy value = i.next();
if (selectedId.equals(value.getCode().toString())) {
return value;
}
}
}
return null;
}
#Override
public void setValue(MyProxy value) {
//handle null value
String selectedId = value.getCode().toString();
... //verify the value is in myList
//traverse dropdown list and set selected index corresponding to value object
for (int i = 0; i < view.getItemCount(); ++i) {
if (selectedId.equals(view.getValue(i))) {
view.setSelectedIndex(i);
}
}
}
}
I just ran into this same issue. If you look at the source code for the GWT ListBox, you'll se e that it does not implement isEditor. I actually don't think it is a GWT editor component. Hard to understand. I had to code around it.