0

我有以下代码从 JSON 格式的 URL 中检索数据并更新 ListView。代码运行良好。在 xml 布局中,我有两个 TextView 和一个 ImageView。

如何动态更新 ImageView?

我没有从 URL 获取图像,我的图像存储在我的项目中(res/drawable 文件夹)。var TAG_ICON 具有我的图像的名称,这与我的项目中的图像的名称完全相同。

例子:

来自 JSON 的响应:TAG_ICON = lamp01

图片名称:lamp01.png

这是我的主要课程:

public class DeviceList extends ListActivity  {

        private static String url = "http://192.168.10.2/myhome/get_all_devices.php";

        // Hashmap ListView
        ArrayList<HashMap<String, String>> deviceList = new ArrayList<HashMap<String, String>>();

        // class JSON
        JSONParser jParser = new JSONParser();

        // Criar JSON Nodes
        private static final String TAG_DEVICES = "devices";
        private static final String TAG_ID = "id";
        private static final String TAG_NAME = "name";
        private static final String TAG_DESCRIPTION = "description";
        private static final String TAG_ICON = "icon";

        // array JSONArray
        JSONArray devices = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.device_list);

            // Loat ListView
            new LoadDevices().execute();
            ListView lv = getListView();
        }

        // class LoadDevices
        class LoadDevices extends AsyncTask<String, String, String>{
            @Override       
            protected void onPreExecute(){          
                super.onPreExecute();                   
            }

            protected String doInBackground(String... args) {   

                // JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(url);

                try {
                // add to array
                devices = json.getJSONArray(TAG_DEVICES);

                // Looping trough all results
                for(int i = 0; i < devices.length(); i++){
                    JSONObject c = devices.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String description = c.getString(TAG_DESCRIPTION);
                    String icon = c.getString(TAG_ICON);


                    // creating a HashMap
                    HashMap<String, String> map = new HashMap<String, String>();     

                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);                    
                    map.put(TAG_DESCRIPTION, description);
                    map.put(TAG_ICON, icon);


                    deviceList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
                return null;
            }

            protected void onPostExecute(String file_url){      

                runOnUiThread(new Runnable() {              
                    public void run() {
                        // update JSON ListView

                        ListAdapter adapter = new SimpleAdapter(DeviceList.this, deviceList,R.layout.device_row,
                                new String[]{
                                    TAG_NAME,
                                    TAG_DESCRIPTION,
                                }, 
                                new int[] {
                                    R.id.device_row_TextViewName,
                                    R.id.device_row_TextViewDescription,
                                });

                        // update listView


                        setListAdapter(adapter);
                    }
                });
            }
        }   

    }

这是我的单行 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/device_row_RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >


    <LinearLayout
        android:id="@+id/device_row_LinearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip" >
        <ImageView
            android:id="@+id/device_row_ImageViewIcon"
            android:contentDescription="@string/app_name"
            android:layout_width="60dip"
            android:layout_height="60dip"
            android:src="@drawable/lamp03" />
    </LinearLayout>



    <TextView
        android:id="@+id/device_row_TextViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/device_row_RelativeLayout"
        android:layout_marginTop="5dip"
        android:layout_marginLeft="75dip"
        android:layout_toRightOf="@+id/device_row_ImageViewIcon"
        android:text="Lâmpada do Quarto"
        android:textColor="#4169E1"
        android:textSize="20dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/device_row_TextViewDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/device_row_TextViewName"
        android:layout_marginBottom="5dip"
        android:layout_alignLeft="@+id/device_row_TextViewName"
        android:paddingTop="1dip"
        android:layout_marginRight="15dip"
        android:layout_centerHorizontal="true"
        android:text="Usado para controlar a lâmpada do quarto."
        android:textColor="#343434"
        android:textSize="13dip" />

</RelativeLayout>
4

1 回答 1

0

如果图像在您的 drawable 文件夹中,您需要首先使用标题在您的 drawable 文件夹中查找图像。确保文件的标题与您从服务器返回的标题标签相同。

int imageId = getResources().getIdentifier("yourpackagename:drawable/" + TAG_TITLE, null, null);

然后只需找到 ImageView 并设置图像

ImageView picture = (ImageView)findViewById(R.id.device_row_ImageViewIcon);
picture.setImageResource(imageId);
于 2013-05-09T17:31:56.293 回答