Ric*_*son 5 java icons android weather
这个问题最初是在大约 20 天前被问到的,我试图根据其城市的响应(来自可绘制文件夹)\n来自天气官方 API 文档中列出的天气状况数量,在我的应用程序上显示天气图标https://openweathermap.org/weather-conditions(您可以随时通过查看编辑历史记录来查看)。\nAPI 提供了 9 种主要天气状况。
\n这仍然是我的目标:
\n首次打开应用程序时,不显示任何图标。
\n如果用户搜索城市并且得到的响应是晴空,则显示晴空图标;
\n否则,如果响应是该城市的少云,则显示少云图标
\n否则,如果响应是该城市的散云,则显示散云图标
\n否则,如果响应是该城市的破碎云,则显示破碎云图标
\n否则,如果响应为该城市的阵雨,则显示阵雨图标
\n否则,如果该城市的响应为 Rain,则显示 Rain 图标
\n否则,如果该城市的响应为雷暴,则显示雷暴图标
\n否则,如果响应为该城市下雪,则显示下雪图标
\n否则,如果响应是该城市的“薄雾”,则显示“薄雾”图标。
\n在 Magdalena Rowicka 的帮助下,我已经能够实现一些目标,但即使在尝试自己修复之后,问题仍然没有完全解决,这就是我重新奖励该帖子的原因。
\n我做的第一件事是使用以下数据集创建一个单独的枚举类:
\npublic enum WeatherIcon {\n Sun, Cloud1, Cloud2, Cloud3, Rain1, Rain2, Thunder, Snow, Mist\n}\nRun Code Online (Sandbox Code Playgroud)\n然后我添加了这段代码final ImageView imageofWeather = rootView.findViewById(R.id.imageView2);在片段上声明文本视图的地方添加了这段代码。
然后我在int drawableResource;// 这里定义默认图标,例如\nR.drawable.default_weather_icon 之后添加viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {。
然后我终于添加了这段代码:
\nswitch (data.getWeather().get(0).getIcon()) { \n case WeatherIcon.Sun:\n drawableResource = R.drawable.sun; //reference to drawable id\n break;\n case WeatherIcon.Cloud1:\n drawableResource = R.drawable.broken_clouds; //reference to drawable id\n break;\n case WeatherIcon.Cloud2:\n drawableResource = R.drawable.few_clouds; //reference to drawable id\n break;\n case WeatherIcon.Cloud3:\n drawableResource = R.drawable.scattered_clouds; //reference to drawable id\n break;\n case WeatherIcon.Rain1:\n drawableResource = R.drawable.small_rain; //reference to drawable id\n break;\n case WeatherIcon.Rain2:\n drawableResource = R.drawable.shower_rain; //reference to drawable id\n break;\n case WeatherIcon.Thunder:\n drawableResource = R.drawable.thunderstorm; //reference to drawable id\n break;\n case WeatherIcon.Snow:\n drawableResource = R.drawable.snow; //reference to drawable id\n break;\n case WeatherIcon.Mist:\n drawableResource = R.drawable.mist; //reference to drawable id\n break;\n\n\n\n imageofWeather.setImageDrawable(drawableResource);\n }\nRun Code Online (Sandbox Code Playgroud)\n在fragment中的if语句下直接访问API并显示天气图标。(相应的 9 个图标当前在第一个片段类的编号线上可见)。
\n当前设置的问题是:
\n\n\n分配给“drawableResource”的值 R.drawable.sun(和其余部分)从未使用过。
\n
和
\n\n\n所需类型:Drawable,提供:int
\n
如果有人可以提供帮助,我一定会很感激。
\n这是我的片段代码:
\npublic class FirstFragment extends Fragment {\n\n private WeatherDataViewModel viewModel;\n\n public FirstFragment() {\n // Required empty public constructor\n }\n\n @Override\n public View onCreateView(LayoutInflater inflater, ViewGroup container,\n Bundle savedInstanceState) {\n // Inflate the layout for this fragment\n View rootView = inflater.inflate(R.layout.fragment_first, container, false);\n // For displaying weather data\n // all field in Java should be type in camelCase, so not current_temp but currentTemp, not Cloud_out but cloudOut\n // Capitalize name is for class not field\n final TextView current_temp = rootView.findViewById(R.id.textView10);\n final TextView current_output = rootView.findViewById(R.id.textView11);\n final TextView rise_time = rootView.findViewById(R.id.textView25);\n final TextView set_time = rootView.findViewById(R.id.textView26);\n final TextView temp_out = rootView.findViewById(R.id.textView28);\n final TextView Press_out = rootView.findViewById(R.id.textView29);\n final TextView Humid_out = rootView.findViewById(R.id.textView30);\n final TextView Ws_out = rootView.findViewById(R.id.textView33);\n final TextView Visi_out = rootView.findViewById(R.id.textView34);\n final TextView Cloud_out = rootView.findViewById(R.id.textView35);\n final ImageView imageofWeather = rootView.findViewById(R.id.imageView2);\n\n\n // Get our ViewModel instance\n viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);\n\n // And whenever the data changes, refresh the UI\n viewModel.getWeatherDataLiveData().observe(getViewLifecycleOwner(), data -> {\n\n int drawableResource; // here define default icon for example R.drawable.default_weather_icon\n\n if (data != null) {\n current_temp.setVisibility(View.VISIBLE);\n current_temp.setText(data.getMain().getTemp() + " \xe2\x84\x83"); // for that you can use strings resource and templates more in https://developer.android.com/guide/topics/resources/string-resource.html#formatting-strings\n current_output.setVisibility(View.VISIBLE);\n current_output.setText(data.getWeather().get(0).getDescription());\n rise_time.setVisibility(View.VISIBLE);\n rise_time.setText(data.getSys().getSunrise() + " ");\n set_time.setVisibility(View.VISIBLE);\n set_time.setText(data.getSys().getSunset() + " ");\n temp_out.setVisibility(View.VISIBLE);\n temp_out.setText(data.getMain().getTemp() + " \xe2\x84\x83");\n Press_out.setVisibility(View.VISIBLE);\n Press_out.setText(data.getMain().getPressure() + " hpa");\n Humid_out.setVisibility(View.VISIBLE);\n Humid_out.setText(data.getMain().getHumidity() + " %");\n Ws_out.setVisibility(View.VISIBLE);\n Ws_out.setText(data.getWind().getSpeed() + " Km/h");\n Visi_out.setVisibility(View.VISIBLE);\n Visi_out.setText(data.getVisibility() + " m");\n Cloud_out.setVisibility(View.VISIBLE);\n Cloud_out.setText(data.getClouds().getAll() + " %");\n\n // get actual weather.\n switch (data.getWeather().get(0).getIcon()) { //or data.getWeather()[0].getIcon() i don\'t remember how it work in Java\n case WeatherIcon.Sun:\n drawableResource = R.drawable.sun; //reference to drawable id\n break;\n case WeatherIcon.Cloud1:\n drawableResource = R.drawable.broken_clouds; //reference to drawable id\n break;\n case WeatherIcon.Cloud2:\n drawableResource = R.drawable.few_clouds; //reference to drawable id\n break;\n case WeatherIcon.Cloud3:\n drawableResource = R.drawable.scattered_clouds; //reference to drawable id\n break;\n case WeatherIcon.Rain1:\n drawableResource = R.drawable.small_rain; //reference to drawable id\n break;\n case WeatherIcon.Rain2:\n drawableResource = R.drawable.shower_rain; //reference to drawable id\n break;\n case WeatherIcon.Thunder:\n drawableResource = R.drawable.thunderstorm; //reference to drawable id\n break;\n case WeatherIcon.Snow:\n drawableResource = R.drawable.snow; //reference to drawable id\n break;\n case WeatherIcon.Mist:\n drawableResource = R.drawable.mist; //reference to drawable id\n break;\n\n\n\n imageofWeather.setImageDrawable(drawableResource);\n }\n\n } else {\n Log.e("TAG", "No City found");\n current_temp.setVisibility(View.GONE);\n current_output.setVisibility(View.GONE);\n rise_time.setVisibility(View.GONE);\n set_time.setVisibility(View.GONE);\n temp_out.setVisibility(View.GONE);\n Press_out.setVisibility(View.GONE);\n Humid_out.setVisibility(View.GONE);\n Ws_out.setVisibility(View.GONE);\n Visi_out.setVisibility(View.GONE);\n Cloud_out.setVisibility(View.GONE);\n Toast.makeText(requireActivity(), "No City found", Toast.LENGTH_SHORT).show();\n }\n });\n\n return rootView;\n }\n\n public void getWeatherData(String name) {\n // The ViewModel controls loading the data, so we just\n // tell it what the new name is - this kicks off loading\n // the data, which will automatically call through to\n // our observe() call when the data load completes\n viewModel.setCityName(name);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
行 imageofWeather.setImageDrawable(drawableResource); 显示此错误:
Run Code Online (Sandbox Code Playgroud)Required type: Drawable, Provided: int
这意味着setImageDrawable()需要(期望)一个 Drawable 参数,但提供的(找到)参数是一个int.
要解决此问题,请使用setImageResource()可绘制int资源而不是Drawable
对于每个 case 语句,我收到以下错误:
Run Code Online (Sandbox Code Playgroud)The value R.drawable.sun(and the rest) assigned to 'drawableResource' is never used.
引发此错误的原因是之前的错误,它认为 的行drawableResource未使用setImageDrawable(),因此它警告您您为其分配了一个值但从未使用过它。
应通过修复第一个警告来修复此警告;尽管如此,我建议enum使用一个接受可绘制资源值的私有构造函数来重写int,它将处理 switch 语句,而不是让片段执行此操作。
新的枚举:
public enum WeatherIcon {
Sun(R.drawable.sun),
Cloud1(R.drawable.broken_clouds),
Cloud2(R.drawable.few_clouds),
Cloud3(R.drawable.scattered_clouds),
Rain1(R.drawable.small_rain),
Rain2(R.drawable.shower_rain),
Thunder(R.drawable.thunderstorm),
Snow(R.drawable.snow),
Mist(R.drawable.mist);
private int drawable;
WeatherIcon(int drawable) {
this.drawable = drawable;
}
public int getDrawable() {
return drawable;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,switch您可以删除它并通过使用枚举getDrawable()方法获取可绘制对象来简化它,而不是片段中的语句,如下所示:
WeatherIcon icon = data.getWeather().get(0).getIcon();
int drawableResource = icon.getDrawable();
imageofWeather.setImageResource(drawableResource);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |