1

我正在开发这个天气应用程序,但我遇到了一个问题,我无法从数据集中获取特定的是否条件到屏幕上。它总是显示其中一个城市的是否条件,而不是其他城市。如果可能的话,我需要一些帮助,如果你指出我犯的任何错误,这可能是一个很大的帮助。

//The variables
var low = getColumn("Daily Weather", "Low Temperature");
var high = getColumn("Daily Weather", "High Temperature");
var city = getColumn("Daily Weather", "City");
var icon = getColumn("Daily Weather", "Icon");
var condition = getColumn("Daily Weather", "Main Condition");
var forecastNum = getColumn("Daily Weather", "Forecast Number");
var id = 0;
//filtered variables
var todayLow = [];
var todayHigh = [];
var todayCondition = [];
var todayIcon = [];

onEvent("locationDropdown", "change", function( ) {
  if (getText("locationDropdown") == "Anchorage, Alaska") {
    id = 1;
  } else if ((getText("locationDropdown") == "Fairbanks, Alaska")) {
    id = 6;
  } else if ((getText("locationDropdown") == "Denver/Boulder, Colorado")) {
    id = 16;
  } else if ((getText("locationDropdown") == "Chicago, Illinois")) {
    id = 31;
  } else if ((getText("locationDropdown") == "Des Moines, Iowa")) {
    id = 56;
  } else if ((getText("locationDropdown") == "Goodland, Kansas")) {
    id = 66;
  } else if ((getText("locationDropdown") == "Louisville, Kentucky")) {
    id = 86;
  } else {
    id = 96;
  }
});

onEvent("conditionButton", "click", function( ) {
  updateScreen();
  setScreen("screen2");
});
onEvent("conditionButton1", "click", function( ) {
  updateScreen();
  setScreen("screen2");
});
onEvent("tempButton", "click", function( ) {
  updateScreen();
  setScreen("screen3");
});
onEvent("homeButton1", "click", function( ) {
  updateScreen();
  setScreen("screen1");
});
onEvent("homeButton2", "click", function( ) {
  updateScreen();
  setScreen("screen1");
});
onEvent("temperatureButton", "click", function( ) {
  updateScreen();
  setScreen("screen3");
});
function updateScreen() {
  var index = id;
  console.log(id);
  for (var i = 0; i < 8; i++) {
    if (forecastNum[i] == 1) {
      appendItem(todayLow, low[i]);
      appendItem(todayHigh, high[i]);
      appendItem(todayCondition, condition[i]);
      appendItem(todayIcon, icon[i]);
    }
  }
  setText("lowTemp", todayLow[1]);
  setText("highTemp", todayHigh[1]);
  setText("label2", todayCondition[1]);
  setProperty("image2", "image", todayIcon[1]);
  console.log(idNum);
  console.log(index);
  console.log(todayLow);
}
=

4

1 回答 1

0
function updateScreen() {
  var index = id;
  console.log(id);
  for (var i = 0; i < 8; i++) {
    if (forecastNum[i] == 1) {
      appendItem(todayLow, low[i]);
      appendItem(todayHigh, high[i]);
      appendItem(todayCondition, condition[i]);
      appendItem(todayIcon, icon[i]);
    }
  }
  setText("lowTemp", todayLow[1]);
  setText("highTemp", todayHigh[1]);
  setText("label2", todayCondition[1]);
  setProperty("image2", "image", todayIcon[1]);
  console.log(idNum);
  console.log(index);
  console.log(todayLow);
}

如果我错了,请纠正我,但我相信这就是问题所在。每当您想更新屏幕时,它只会将其更新为内部索引 1todayLow todayHigh todayConditiontodayIcon数组的任何内容。您应该找到一种方法来根据用户选择的任何下拉菜单自动更改值,而不是为这些数组添加“1”。此外,您所做的并不是一件坏事,而是这样做:

onEvent("locationDropdown", "change", function( ) {
  if (getText("locationDropdown") == "Anchorage, Alaska") {
    id = 1;
  } else if ((getText("locationDropdown") == "Fairbanks, Alaska")) {
    id = 6;
  } else if ((getText("locationDropdown") == "Denver/Boulder, Colorado")) {
    id = 16;
  } else if ((getText("locationDropdown") == "Chicago, Illinois")) {
    id = 31;
  } else if ((getText("locationDropdown") == "Des Moines, Iowa")) {
    id = 56;
  } else if ((getText("locationDropdown") == "Goodland, Kansas")) {
    id = 66;
  } else if ((getText("locationDropdown") == "Louisville, Kentucky")) {
    id = 86;
  } else {
    id = 96;
  }
});

你也可以这样做:

onEvent("locationDropdown", "change", function( ) {
  switch (getText("locationDropdown")) {
    case "Anchorage, Alaska":
      id = 1;
    break;
    case "Fairbanks, Alaska":
      id = 6;
    break;
    case "Denver/Boulder, Colorado":
      id = 16;
    break;
    case "Chicago Illinois":
      id = 31;
    break;
    case "Des Moines, Iowa":
      id = 56;
    break;
    case "Goodland, Kansas":
      id = 66;
    break;
    case "Louisville, Kentucky":
      id = 86;
    break;
    default:
      id = 96;
    break;
  }
});

switch statement在代码中非常有用,因为它节省了时间,因为您不再需要为getText("locationDropdown")您编写的每个 else if 语句编写。

无论如何,我希望我的回答至少有一点帮助。祝你好运!

于 2021-04-21T13:47:59.377 回答