Я хочу изменить изображение на основе моего ответа JSON - PullRequest
0 голосов
/ 04 июня 2018

Это скриншот приложения, я создаю приложение погоды, используя API open-weather-map. Например, если ответ солнечный, я хочу показать изображение солнечной погоды из моей папки для рисования.Я также искал этот вопрос, но не нашел ответа.

Ответы [ 2 ]

0 голосов
/ 04 июня 2018

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

Оставить комментарий, если возникнут проблемы.

0 голосов
/ 04 июня 2018
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
...