codenameone - 仅在选择特定选项卡时解析数据
问题描述
我在屏幕中使用了标签。每个选项卡都有自己的 json 数据可供读取。所以我在每个选项卡中都使用了 ConnectionRequest 方法。因此发生的事情是所有 connectionRequest 方法同时运行并且应用程序非常慢。我需要的是在选择相应的选项卡时解析特定的 json 数据,以便只解析所需的 json。因此,我使用了 addSelectionListener ,以便在打开特定选项卡时才读取用于解析 json 数据的 connectionRequest。但是,当第一次读取屏幕时,会显示黑屏。如果选择了该选项卡,则它可以工作。通过将代码保留在 addSelectionListener 之外,可以使每个选项卡的所有 connectionRequests 立即运行,这是我不想要的。我怎么解决这个问题?
从特定屏幕支持时,我也需要返回到特定选项卡。然后那里也看到了这个问题。ConnectionRequest 不运行。
Tabs tabs = new Tabs(Component.BOTTOM);
tabs.addTab("Home", calendarIcon, homeContainer);
tabs.addTab("Book", calendarIcon3, quickBookingContainer);
tabs.addTab("Servicing", calendarIcon1, servicingContainer);
tabs.addTab("History", calendarIcon2, serviceHistoryContainer);
add(BorderLayout.CENTER, tabs);
tabs.addSelectionListener((int oldSelected, int newSelected) -> {
if (newSelected == 0) {
//connectionRequest for parsing json data
homeContainerRequest(homeContainer, res);
} else if (newSelected == 1) {
quickBookingContainerRequest(quickBookingContainer, res);
} else if (newSelected == 2) {
serviceRequest(serviceContainer, res);
} else if (newSelected == 3) {
serviceHistoryRequest(serviceHistoryContainer, res);
}
});
public void homeContainerRequest(Container homeContainer, Resources res){
ConnectionRequest con = new ConnectionRequest(url, false) {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
Map parser = jp.parseJSON(new InputStreamReader(input));
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
};
con.setFailSilently(true);
con.addRequestHeader("accept", "application/json");
NetworkManager.getInstance().addToQueue(con);
}
public void quickBookingContainerRequest(Container quickBookingContainer, Resources res){
ConnectionRequest con = new ConnectionRequest(url1, false) {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
Map parser = jp.parseJSON(new InputStreamReader(input));
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
};
con.setFailSilently(true);
con.addRequestHeader("accept", "application/json");
NetworkManager.getInstance().addToQueue(con);
}
public void serviceHistoryRequest(Container serviceHistoryContainer, Resources res){
ConnectionRequest con = new ConnectionRequest(url2, false) {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
Map parser = jp.parseJSON(new InputStreamReader(input));
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
}
};
con.setFailSilently(true);
con.addRequestHeader("accept", "application/json");
NetworkManager.getInstance().addToQueue(con);
}
解决方案
第一个选项卡已被选中,因此没有选择事件。你需要为此做一个特殊情况。请注意,选项卡选择发生了很多,因此您需要缓存结果。
推荐阅读
- react-native - 使用 redux 调度操作后如何导航到另一个屏幕
- c++ - 问题是将数据写入特定单元格并且文件已损坏
- javascript - if 语句中的语法问题
- javascript - Javascript/Jquery RPG 游戏的战斗系统(计时器)
- javascript - JavaScript 范围规则:如何在回调中使用 clearInterval()
- ruby-on-rails - 连接被拒绝:Postgres 中的操作超时
- linked-list - 二和 Leetcode Rust 解决方案?
- scala - Akka http 将标头添加到带有正文的 POST 请求中
- python - 具有 Decimal 数据类型和 json.dumps() 的嵌套对象
- ruby-on-rails - 为什么 dockerized Rails 应用程序会引发“PG::ConnectionBad”?