- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是新手/正在学习 Java 和 Android,并尝试制作一个简单的天气应用程序来告诉我当前位置的天气。我已经掌握了基础知识,并且正在使用 OpenWeatherMap API - 一切都很好。
我正在尝试执行以下操作:- 当API返回给我天气信息时,例如“碎云”或“大雨”,我想将ImageView设置为一张图片,例如碎云或大雨。
我似乎无法让 ImageView 获取图像(位于我的 Drawable 文件夹中) - 我尝试过使用 setImageResource、setImageDrawable 等。有什么指点吗?我错过了什么?
这是我的代码(重要的部分):
public class DownloadHtml extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");
String tempInfo = jsonObject.getJSONObject("main").getString("temp");
String windInfo = jsonObject.getJSONObject("wind").getString("deg");
JSONArray weatherArr = new JSONArray(weatherInfo);
for (int i = 0; i < weatherArr.length(); i++) {
TextView weatherTextView = findViewById(R.id.weatherTextView);
weatherTextView.setText("");
JSONObject jsonPart = weatherArr.getJSONObject(i);
String description = jsonPart.getString("description");
weatherTextView.setText(description);
iv = findViewById(R.id.imageView2);
if (description == "clear sky") iv.setImageResource(R.drawable.clear_skies);
else if (description == "few clouds")
iv.setImageResource(R.drawable.few_clouds);
else if (description == "scattered clouds")
iv.setImageResource(R.drawable.scattered_clouds);
else if (description == "broken clouds")
iv.setImageResource(R.drawable.broken_clouds);
else if (description == "shower rain")
iv.setImageResource(R.drawable.shower_rain);
else if (description == "rain") iv.setImageResource(R.drawable.heavy_rain);
else if (description == "thunderstorm")
iv.setImageResource(R.drawable.thunderstorm);
else if (description == "snow") iv.setImageResource(R.drawable.snow);
else if (description == "mist") iv.setImageResource(R.drawable.mist);
Log.i("description", jsonPart.getString("description"));
if (description != null) {
locationManager.removeUpdates(locationListener);
}
}
for (int f = 0; f < tempInfo.length(); f++) {
TextView weatherTextView3 = findViewById(R.id.weatherTextView3);
weatherTextView3.setText("");
String tempString = tempInfo + " degrees celcius.\n";
weatherTextView3.setText(tempString);
Log.i("temp: ", tempInfo);
if (tempString != null) {
locationManager.removeUpdates(locationListener);
f = 500;
}
}
for (int j = 0; j < windInfo.length(); j++) {
TextView weatherTextView2 = findViewById(R.id.weatherTextView2);
weatherTextView2.setText("");
// assign text to wind direction
double windDegrees = Double.valueOf(windInfo);
if ((windDegrees > 0) && (windDegrees < 90)) {
String windString = "A North East Wind.\n";
weatherTextView2.setText(windString);
Log.i("Wind: ", windInfo);
}
if ((windDegrees >= 90) && (windDegrees < 180)) {
String windString = "A South East Wind.\n";
weatherTextView2.append(windString);
Log.i("Wind: ", windInfo);
}
if ((windDegrees >= 180) && (windDegrees < 270)) {
String windString = "A South West Wind.\n";
weatherTextView2.append(windString);
Log.i("Wind: ", windInfo);
}
if ((windDegrees >= 270) && (windDegrees < 360)) {
String windString = "A North West Wind.\n";
weatherTextView2.append(windString);
Log.i("Wind: ", windInfo);
}
if (windInfo != null) {
locationManager.removeUpdates(locationListener);
j = 500;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
最佳答案
您只是不小心使用了 == 来比较字符串:)
使用description.equals("clear sky")
代替description == "clear sky"
关于java - 我可以在 onPostexecute 中使用 setImageResource 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48510764/
我正在尝试设置一个图像,并在短暂的停顿(如闪烁)后将其更改为不同的图像;并且它应该在应用程序启动后自动启动。 public class MainActivity extends ActionBarAc
我需要更改 ImageView 中的图像,然后获取宽度。问题是,它给了我最后一张图片的宽度。 headpic.setImageResource(head[x]); int width = headpi
我有以下代码: public class PainLogger extends Activity implements OnClickListener, OnItemClickListener{
我正在尝试制作我的第一个应用程序 (yahtzee)。当您单击滚动按钮时,我想切换骰子的图像,现在每当我调用 setImageResource 时我都会崩溃。 主要 Activity : public
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
每当我尝试运行该行时,我的程序就会崩溃: d1.setImageResource(R.drawable.clubs9); 我想不通为什么。它似乎与我使用类似代码行的另一个程序相同。我试过添加 getR
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set
我正在制作一款棋盘游戏,我正在为棋盘使用 10x10 GridView。我制作了一个扩展 BaseAdapter 的 ImageAdapter 类,它包含一个整数图标数组(9 个补丁文件),这些用于显
我有一个 imageView,我想显示您当前所在国家/地区的小图标。我可以获得国家代码,但问题是我无法动态更改 imageView 资源。我的图片文件都是小写的(例如:国家代码=US,图片文件=us)
我想根据我的字符串更改 imageview src,我有这样的东西: ImageView imageView1 = (ImageView)findViewById(R.id.imageView1);
我是 java/android 新手,正在制作一个测试应用程序。它有图像按钮,单击时会暂时切换到不同的图像。使用 裁剪原件 android:adjustViewBounds="true" androi
我试图在我的应用程序中显示一个图像,每次按下按钮时该图像都会发生变化。应显示的图像的名称位于我的对象中。我可以通过以下方式获取图像的名称 String nameOfImage = myObhect.g
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个 imageView,我想显示您当前所在国家/地区的小图标。我可以获取国家/地区代码,但问题是我无法动态更改 imageView 资源。我的图像文件都是小写的(例如:国家/地区代码=US,图像
我尝试设置IV_status.setImageResource(R.drawable.red) 但它似乎不起作用,每当调用上面的代码时,整个应用程序都会运行 会崩溃。我也尝试过setBackgroun
我无法找到如何将图片设置为 setImageResource()。目前我可以像这样从 res/drawable 轻松添加: 在 ListViewAdapter 中: imageView = (Imag
我需要有关使用 setImageResource 方法设置随机图像的帮助。在可绘制文件夹中,我有一个名为 photo0.jpg、photo1.jpg...photo99.jpg 的 jpeg 文件。以
我正在尝试从我的 Assets 文件夹中获取图像并将它们设置到我的变量中,称为insectImage。这是我所做的: InsectRecyclerAdapter.InsectHolder insect
我正在构建另一个更大的应用程序,在某些时候我需要动态更改图像,并且我的应用程序崩溃了。因此,为了测试功能,我制作了另一个非常简单的应用程序,它只显示带有代码的图片: package com.examp
我想基于变量以编程方式为 ImageButton 设置 ImageResource。 例如:如果 size=5,我想将 setImageResource 设置为 R.drawable.five
我是一名优秀的程序员,十分优秀!