这是该应用程序的屏幕截图,我正在使用开放式映射API制作天气应用程序,例如,如果响应是晴天,我想从我的可绘制文件夹中显示阳光明媚的天气图像。我还搜索了这个问题,但没有找到答案。
imageview= (ImageView)findViewById(R.id.imageView);
String request=new StringRequest(Request.Method.GET, url, new
Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray =jsonObject.getJSONArray("users");
String re = "";
wlist=new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject jsonObject1 =jsonArray.getJSONObject(i);
re = jsonObject1.getString("re");
if(re.equals("sunny")
{
imageview.setImageDrawable(getResources().getDrawable(R.drawable.sunnyImage);
}
Model m = new Model(re);
userlist.add(user);
}
// Initialize adapter
wlist.setAdapter(yourAdapter);
//set adapter on your view
You can try this function
private Drawable getDrawable(String source)
{
Drawable drawable;
int sourceId =
getApplicationContext()
.getResources()
.getIdentifier(source, "drawable", getPackageName());
drawable = getApplicationContext().getResources().getDrawable(dourceId);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
function implementation
/*If you can get the JSON response same as the name of the image or set the name of the image as per your response this will work very efficiently*/
imageView.setImageDrawable(getDrawable("sunny"));//this will return a drawable whose name is sunny
发表评论,如果有问题。