android volley: Updating listview on item click - android-listview

I want :
my listview to be updated when click on refreh button (action bar)
to be able to scroll while the update task is running.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
setRefreshActionButtonState(true);
this.startRequest();
....
return true;
}
return super.onOptionsItemSelected(item);
}
and this is how startRequest () looks:
private void startRequest() {
articleList.clear();
articleList.addAll(articleList);
//volley request
...
adapter.notifyDataSetChanged();
}
The first point (updating listview) is ok when I click on refresh, but how can I resolve the 2nd point ?

articleList.clear();
articleList.addAll(articleList);
//volley request
...
adapter.notifyDataSetChanged();
Do this only after you get your response successfully in you Volley's complete listener. Till then do not notify the adapter or clear the data.

Related

What is the right usage for the SingleSelectionModel?

we would like to link from a CellTable to a property editor page. We use the SingleSelectionModel to get notified, when a user clicks on an item.
It is initialized like this:
private final SingleSelectionModel<Device> selectionModel = new SingleSelectionModel<Device>();
We then assign the selection change handler:
selectionModel.addSelectionChangeHandler(this);
Our selection change handler looks like this:
#Override
public void onSelectionChange(SelectionChangeEvent event) {
Log.debug("DevicesPresenter: SelectionChangeEvent caught.");
Device selectedDevice = selectionModel.getSelectedObject();
if (selectedDevice != null) {
selectionModel.clear();
if (selectionModel.getSelectedObject() != null){
Log.debug("DevicesPresenter: selected item is " + selectionModel.getSelectedObject());
}
else{
Log.debug("DevicesPresenter: selected item is null");
}
deviceEditorDialog.setCurrentDevice(selectedDevice.getUuid());
// get the container data for this device
clientModelProvider.fetchContainersForDevice(selectedDevice.getUuid());
PlaceRequest request = new PlaceRequest.Builder()
.nameToken(NameTokens.deviceInfo)
.with("uuid", selectedDevice.getUuid())
.build();
Log.debug("Navigating to " + request.toString());
placeManager.revealPlace(request);
}
}
Now there are two issues: There always seem to be two SelectionChangeEvents at once and i really cannot see why. The other thing is: How is the right way do handle selection of items and the related clearing of the selection model? Do we do that the right way?
Thanks!
If you only want to get notified of "clicks" without keeping the "clicked" item selected, use a NoSelectionModel instead; no need to clear the selection model as soon as something is selected.
As for your other issue with being called twice, double-check that you haven't added your selection handler twice (if you can unit-test your DevicesPresenter, introspect the handlers inside the selection model for example)
In your line selectionModel.addSelectionChangeHandler(this); what does this refer?
Here my code how I use SingleSelectionModel
public class MyClass{
private final SingleSelectionModel<CountryDto> selectionModel = new SingleSelectionModel<CountryDto>();
...
public MyClass(){
cellTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
#Override
public void onSelectionChange(SelectionChangeEvent event) {
CountryDto selected = selectionModel
.getSelectedObject();
if (selected != null) {
Window.alert("Selected country "+selected.getTitle());
}
}
});
}
}

Calling refresh on another grid after removal

I currently have firstGrid that has some records, I have set a warning on removal message so a dialog box pops up when I click the delete button. How do I make it so secondGrid refresh when I confirm the delete on firstGrid?
firstGrid.setWarnOnRemoval(true);
firstGrid.setWarnOnRemovalMessage("Delete?");
SmartGwt doesn't support a customized behavior for this operation. You should program it by yourself.
Just create a new ListGridField and refresh your second grid in the CallBack after the remove operation. Your first approach could be the following:
ListGridField removeListGridField = new ListGridField("removeButton", 20);
removeListGridField.setType(ListGridFieldType.ICON);
removeListGridField.setCellIcon("[SKIN]actions/remove.png");
removeListGridField.setCanEdit(false);
removeListGridField.setCanFilter(false);
removeListGridField.setCanGroupBy(false);
removeListGridField.setCanSort(false);
removeListGridField.setCanDragResize(false);
removeListGridField.setCanFreeze(false);
removeListGridField.setCanHide(false);
removeListGridField.addRecordClickHandler(new RecordClickHandler()
{
#Override
public void onRecordClick(RecordClickEvent event)
{
if (event.getRecord() == null) // local record
{
discardEdits(event.getRecordNum(), 0);
yourGrid.fetchData();
}
else
removeData(event.getRecord(), new DSCallback()
{
#Override
public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest)
{
yourGrid.fetchData();
}
});
}
});

JavaFx how to hide popup when mouse is clicked on owner window?

Hi i have javafx app which has only one stage.On tab key press event of text field, a popup showed on primary stage of application. like below
private void tripNoKeyPressEventAction(KeyEvent event){
if(event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
popup.show(GateIn.primaryStage);
}
}
popup.requestFocus();
popup.focusedProperty().addListener(new ChangeListener<Boolean>
() {
#Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) {
if(t1==false)
{
System.out.println("focus lost");
popup.hide();
}
}
});
I don't click on the popup and don't select anything in popup. I will just click on the stage behind it.I expect popup to be closed but It gives me IllegalArgumentException before executing popup's focusedProperty Listener.
If popup is on a different stage (other than primary stage of aaplication),based on stage focusedProperty() i can hide popup.
How to hide popup in case popup is shown on primary stage?
With FX 8, you can simply do
popup.setAutoHide(true)
You should set a event dispatcher for most top level window then all event will cross it.
In the popup window:
getScene().getWindow().setEventDispatcher((event, tail) -> {
if (event.getEventType() == RedirectedEvent.REDIRECTED) {
// RedirectedEvent is a box that contains original event from other target
RedirectedEvent ev = (RedirectedEvent) event;
if (ev.getOriginalEvent().getEventType() == MouseEvent.MOUSE_PRESSED) {
hide();
}
}else {
// if click in the popup window. handle the event by default
tail.dispatchEvent(event);
}
return null;
});
More information please see javafx.event.EventDispatcher

GWT Delaying an action after an event trigger

I want to have the user click a button, then they will see a "Toast" message popup, which fades away , then the action that the button click performs should happen. What is the best way to do this? Should I trigger a Timer on catching the click event and let it timeout (While the toast is being displayed ) and then call the handling code or is there any built in delay mechanism in event handling I can use?
I don't want my toast to be involved at all in the event handling
If I really follow your requirements the following code should do:
// interface of the "Toast" no matter what the implementation actually is
public interface Toast
{
void open( String message );
void closeFadingAway();
}
// calling code
public class ClientCode
{
private static final int myDelay = 1000; // 1 second in millis
private Toast myToast;
void onMyAction()
{
myToast.open( "Your action is being handled..." );
Scheduler.get().scheduleFixedDelay( new RepeatingCommand()
{
#Override
public boolean execute()
{
myToast.closeFadingAway();
performAction();
return false; // return false to stop the "repeating" = executed only once
}
}, myDelay );
}
void performAction()
{
// do something interesting
}
}
Now, if you actually mean to be able to interrupt the action when the user presses some button in the toast this is a different story.
If you are using a popup panel you could use the addCloseHandler on the popup panel and from here call the method that would have otherwise been called by the button.
popUpPanel.addCloseHandler(new CloseHandler<PopupPanel>(){
#Override
public void onClose(CloseEvent<PopupPanel> event) {
// TODO Do the closing stuff here
}
});
So when the PopupPanel disappears and the close event triggers you can do your magic there.

Suppress control-click context menu, gwt 1.6

My gwt 1.6 application intercepts mouse clicks on hyperlinks, so when a user shift-clicks on links to "authors" they get an Edit... dialog box instead of navigating to the author's page. That's working nicely.
I'd now like to allow the user to control-click to select more than one author, but I can't figure out how to suppress the browser's default popup menu. This code handles shift-clicks correctly, but fails in the hosted browser when I control-click and half-fails in Firefox (handleCtrlClick() gets called, but I still get the browser menu):
public void onModuleLoad() {
Event.addNativePreviewHandler(this);
}
//
// Preview events-- look for shift-clicks on paper/author links, and pops up
// edit dialog boxes.
// And looks for control-click to do multiple selection.
//
public void onPreviewNativeEvent(Event.NativePreviewEvent pe) {
NativeEvent e = pe.getNativeEvent();
switch (Event.getTypeInt(e.getType())) {
case Event.ONCLICK:
if (e.getShiftKey()) { handleShiftClick(e); }
if (e.getCtrlKey()) { handleCtrlClick(e); }
break;
case Event.ONCONTEXTMENU:
if (e.getCtrlKey()) { // THIS IS NOT WORKING...
e.preventDefault();
e.stopPropagation();
}
break;
}
}
A breakpoint set inside the ONCONTEXTMENU case is never called.
IIRC ctrl + click is the correct way to select multiple items not ctrl + right click unless you're using a one button mouse (iMac), in that case I can't help you.
Could you provide more details?
Edit:
Why not override the contextmenu (e.g. disable it) then create your own context menu widget (perhaps based on vertical MenuBar + MenuItems) and display it only on Ctrl + RightClick?
In other words you'd create a MouseHandler somewhat like this (pseudo code):
public void onMouseDown(MouseDownEvent event) {
Widget sender = (Widget) event.getSource();
int button = event.getNativeButton();
if (button == NativeEvent.BUTTON_LEFT) {
if(event.is_ctrl_also)
{
// Add to selection
selection = selection + sender;
}
else
{
// Lose selection and start a new one
selection = sender;
}
}
else if(button == NativeEvent.BUTTON_RIGHT) {
if(event.is_ctrl_also)
{
// show context menu
this.contextmenu.show();
}
else
{
// do something else
}
}
return;
}
I've not encountered the bug with Ctrl-Leftclick firing a ContextMenu event, but I'm sure you could also make a workaround for Firefox only using permutations.
I'm getting closer:
public void onModuleLoad() {
Event.addNativePreviewHandler(this); // Catch shift- or control- clicks on links
addContextMenuEventListener(RootPanel.getBodyElement());
}
protected native void addContextMenuEventListener(Element elem) /-{
elem.oncontextmenu = function(e) {
return false; // TODO: only return false if control key down...
};
}-/;
That disables the right-click menu entirely; I'd really like to disable it ONLY if the control key is pressed...