2013-05-09 35 views
0

我使用以下代碼從JSON格式的URL中更新數據並更新ListView。代碼運行完美。在xml佈局中,我有兩個TextView和一個ImageView。將圖像動態更改爲JSON中的ListView

如何更新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> 

回答

0

如果圖像在您的可繪製文件夾中,則需要先使用標題在可繪製文件夾中查找圖像。確保文件的標題與從服務器返回的標題標記相同。

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

然後,只需找到的ImageView,並設置圖像

ImageView picture = (ImageView)findViewById(R.id.device_row_ImageViewIcon); 
picture.setImageResource(imageId); 
+0

感謝。它工作完美。 – user2341575 2013-05-10 03:31:48