1

我有如下代码:

lst.getSelectionModel().selectedItemProperty().addListener(
(ob, ov, nv) -> { .... do something})

第一个是 ListView。它在桌面测试时运行良好,但选择侦听器在部署到 Android 移动设备时似乎不起作用。目标是从列表中选择项目,然后显示项目详细信息视图。我应该采取不同的方法吗?

任何帮助表示赞赏。

谢谢

编辑:正如建议的那样,我查看了 logcat 输出。

对 REST 服务的初始调用以获取数据并测试日志记录是否正常工作:

10-27 12:38:35.699 25889-25940/edu.uconn.chatter I/System.out: **************************** Test to see if log is working.
10-27 12:38:35.709 25889-26656/edu.uconn.chatter I/RestDataSource: Created Rest Connection:
                                                                    Method: GET
                                                                    Request URL: http://192.168.10.77:8080/search/test
                                                                    Form Params: {}
                                                                    ContentType: null
                                                                    Consumer Credentials: null / null
10-27 12:38:35.709 25889-26656/edu.uconn.chatter I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
10-27 12:38:35.709 25889-26656/edu.uconn.chatter I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

这确实会返回出现在列表中的数据。以下输出来自尝试触摸列表中的一行。相应的方法中有调试语句,但没有打印出来。我认为这是因为它们没有被调用:

10-27 12:41:21.169 25889-25889/edu.uconn.chatter D/ViewRootImpl: ViewPostImeInputStage processPointer 0
10-27 12:41:21.169 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.169 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.169 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.179 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.179 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.179 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.209 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.229 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.229 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.229 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.229 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.229 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.249 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.249 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.249 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.259 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.259 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.259 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.279 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.279 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.279 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.279 25889-25889/edu.uconn.chatter D/ViewRootImpl: ViewPostImeInputStage processPointer 1
10-27 12:41:21.279 25889-25889/edu.uconn.chatter E/FXEntity: call native MultitouchEvent, density = 4.0, touchXs0 = 401
10-27 12:41:21.279 25889-25940/edu.uconn.chatter I/GLASS: Call InternalSurfaceView_onMultiTouchEventNative
10-27 12:41:21.279 25889-25940/edu.uconn.chatter I/GLASS: Glass will pass multitouchevent to monocle with count = 1
10-27 12:41:21.279 25889-25940/edu.uconn.chatter I/System.out: don't add points, primary = -1

谢谢

编辑2:

我同意它应该工作相同。可能是我正在做的事情导致了这个问题。如果你有一个工作样本,你可以发布吗?就设备而言,它是三星 Edge 6,Android 版本 6.0.1。这是该类的代码:

package edu.uconn.chatter.views;

import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import com.gluonhq.connect.ConnectState;
import com.gluonhq.connect.GluonObservableList;
import edu.uconn.chatter.App;
import edu.uconn.chatter.model.BeneNameBO;
import edu.uconn.chatter.model.CurrentBene;
import edu.uconn.chatter.services.BeneLookupService;
import edu.uconn.chatter.views.dlgs.ErrorDlg;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import javax.inject.Inject;
import java.net.URL;
import java.util.ResourceBundle;

public class LookupPresenter implements Initializable {

    @FXML
    private View lookup;

    @FXML
    private TextField searchParam;

    @FXML
    private ListView<BeneNameBO> beneList;

    @FXML
    private Button search;

    @Inject
    private CurrentBene currentBene;

    @Inject
    private BeneLookupService beneLookupService;

    @Inject
    private ErrorDlg errorDlgView;

    public LookupPresenter() {
        super();
    }

    // After constructor
    public void initialize(URL location, ResourceBundle resources) {
        // Screen Transition
        lookup.setShowTransitionFactory(BounceInRightTransition::new);

        // Set cell factory for benelist
        beneList.setCellFactory(lv -> new ListCell<BeneNameBO>() {
            @Override
            public void updateItem(BeneNameBO item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    setText(item.getLastName() + ", " + item.getFirstName());
                } else {
                    setText(null);
                }
            }
        });
        beneList.setPlaceholder(new Label("Make a selection"));

        // Need a query parameter
        search.disableProperty().bind(Bindings.createBooleanBinding(() -> {
            return searchParam.textProperty().isEmpty().get();
        }, searchParam.textProperty()));

        // Set up view
        lookup.showingProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue) {
                AppBar appBar = MobileApplication.getInstance().getAppBar();

                // Return to bene form view
                appBar.setNavIcon(MaterialDesignIcon.ARROW_BACK.button(e ->
                        MobileApplication.getInstance().switchView(App.BENEFORM_VIEW)));
                appBar.setTitleText("Look up");

                // Allow to logout from this view
                appBar.getActionItems().add(MaterialDesignIcon.POWER.button(e ->
                        MobileApplication.getInstance().switchView(App.PRIMARY_VIEW)));
            }
        });
    }

    @FXML
    private void onSearch(ActionEvent click) {
        System.out.println("**************************** Test to see if log is working.");

        GluonObservableList<BeneNameBO> benes =
                beneLookupService.findBenes(searchParam.getText());

        // Error handler
        benes.exceptionProperty().addListener((observable, oldValue, newValue) -> {
            //showErrorDlg(newValue.getMessage());
            errorDlgView.showError(newValue.getMessage());
        });

        // Populate list
        benes.stateProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == ConnectState.SUCCEEDED) {
                if (benes != null && benes.size() > 0) {
                    beneList.setItems(benes);

                    Platform.runLater(() -> {
                        beneList.getSelectionModel().selectedItemProperty().addListener(
                                (obs, ov, nv) -> {
                                    System.out.println("****************************** In list click event handler:");
                                    if (nv != null) {
                                        setCurrentBeneId((BeneNameBO) nv);
                                    } else {
                                        setCurrentBeneId((BeneNameBO) ov);
                                    }

                                    MobileApplication.getInstance().switchView(App.BENEFORM_VIEW);
                                });
                    });
                } else {
                    final Label label = new Label("Nothing found, try again.");
                    beneList.setPlaceholder(label);
                }
            }
        });
    }

    private void setCurrentBeneId(BeneNameBO beneNameBO) {
        System.out.println("******************************Process call for ID:" + beneNameBO.getBeneId());
        currentBene.clearCurrent();
        currentBene.activeBene().get().setBeneId(beneNameBO.getBeneId());
    }
}

再一次感谢你的帮助。

编辑3:这种方法有效。这是代码,以防其他人遇到类似问题。

package edu.uconn.chatter.widgets;

import com.gluonhq.charm.glisten.control.ListTile;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import edu.uconn.chatter.model.BeneNameBO;
import javafx.scene.control.ListCell;

import java.util.function.Consumer;

/**
 * Created by johndestefano on 10/27/16.
 */
public class BeneListCell extends ListCell<BeneNameBO> {
    private ListTile tile;
    private BeneNameBO beneNameBO;

    public BeneListCell(Consumer<BeneNameBO> processEvt) {
        tile = new ListTile();
        tile.setOnMouseClicked(e -> {
            System.out.println("********************* Item clicked" + tile.textProperty().size());
            processEvt.accept(beneNameBO);
        });
        setText(null);
    }

    @Override
    protected void updateItem(BeneNameBO item, boolean empty) {
        super.updateItem(item, empty);
        this.beneNameBO = item;

        if (item != null && !empty) {
            tile.textProperty().setAll(item.getLastName());
            tile.setPrimaryGraphic(MaterialDesignIcon.DESCRIPTION.graphic());
            tile.setSecondaryGraphic(MaterialDesignIcon.STAR.graphic("-fx-fill: green"));
            setGraphic(tile);
        } else {
            setGraphic(null);
        }
    }
}

和主持人:

package edu.uconn.chatter.views;

import com.gluonhq.charm.glisten.animation.BounceInRightTransition;
import com.gluonhq.charm.glisten.application.MobileApplication;
import com.gluonhq.charm.glisten.control.AppBar;
import com.gluonhq.charm.glisten.mvc.View;
import com.gluonhq.charm.glisten.visual.MaterialDesignIcon;
import com.gluonhq.connect.ConnectState;
import com.gluonhq.connect.GluonObservableList;
import edu.uconn.chatter.App;
import edu.uconn.chatter.model.BeneNameBO;
import edu.uconn.chatter.model.CurrentBene;
import edu.uconn.chatter.services.BeneLookupService;
import edu.uconn.chatter.views.dlgs.ErrorDlg;
import edu.uconn.chatter.widgets.BeneListCell;
import javafx.beans.binding.Bindings;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import javax.inject.Inject;
import java.net.URL;
import java.util.ResourceBundle;

public class LookupPresenter implements Initializable {

    @FXML
    private View lookup;

    @FXML
    private TextField searchParam;

    @FXML
    private ListView<BeneNameBO> beneList;

    @FXML
    private Button search;

    @Inject
    private CurrentBene currentBene;

    @Inject
    private BeneLookupService beneLookupService;

    @Inject
    private ErrorDlg errorDlgView;

    public LookupPresenter() {
        super();
    }

    // After constructor
    public void initialize(URL location, ResourceBundle resources) {
        // Screen Transition
        lookup.setShowTransitionFactory(BounceInRightTransition::new);

        // Set cell factory for benelist
        beneList.setCellFactory(lv -> new BeneListCell(bn -> {
            System.out.println("******************************Process call for ID:" + bn.getBeneId());
            currentBene.clearCurrent();
            currentBene.activeBene().get().setBeneId(bn.getBeneId());
            MobileApplication.getInstance().switchView(App.BENEFORM_VIEW);
        }));
        beneList.setPlaceholder(new Label("Make a selection"));

        // Need a query parameter
        search.disableProperty().bind(Bindings.createBooleanBinding(() -> {
            return searchParam.textProperty().isEmpty().get();
        }, searchParam.textProperty()));

        // Set up view
        lookup.showingProperty().addListener((obs, oldValue, newValue) -> {
            if (newValue) {
                AppBar appBar = MobileApplication.getInstance().getAppBar();

                // Return to bene form view
                appBar.setNavIcon(MaterialDesignIcon.ARROW_BACK.button(e ->
                        MobileApplication.getInstance().switchView(App.BENEFORM_VIEW)));
                appBar.setTitleText("Look up");

                // Allow to logout from this view
                appBar.getActionItems().add(MaterialDesignIcon.POWER.button(e ->
                        MobileApplication.getInstance().switchView(App.PRIMARY_VIEW)));
            }
        });
    }

    @FXML
    private void onSearch(ActionEvent click) {
        System.out.println("**************************** Test to see if log is working.");

        GluonObservableList<BeneNameBO> benes =
                beneLookupService.findBenes(searchParam.getText());

        // Error handler
        benes.exceptionProperty().addListener((observable, oldValue, newValue) -> {
            //showErrorDlg(newValue.getMessage());
            errorDlgView.showError(newValue.getMessage());
        });

        // Populate list
        benes.stateProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == ConnectState.SUCCEEDED) {
                if (benes != null && benes.size() > 0) {
                    beneList.setItems(benes);
                } else {
                    final Label label = new Label("Nothing found, try again.");
                    beneList.setPlaceholder(label);
                }
            }
        });
    }
}

谢谢您的帮助。

4

0 回答 0