2013-02-20 84 views
3

我正在使用SimpleAdapter在JSON解析數據的哈希映射中顯示ListView。一切都很完美。如何在ListView中有效添加圖片(或將圖片添加到Map)?將圖像添加到哈希映射不會顯示圖像,也不會拋出錯誤或異常!用SimpleAdapter使用哈希映射在ListView中顯示圖像 - Android

這裏是我的代碼..

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.event_list); 
      // Hashmap for ListView 
      inboxList = new ArrayList<HashMap<String, String>>(); 

      // Loading INBOX in Background Thread 
      new LoadInbox().execute(); 
     } 

     /** 
     * Background Async Task to Load all INBOX messages by making HTTP Request 
     * */ 
     class LoadInbox extends AsyncTask<String, String, String> { 

      /** 
      * Before starting background thread Show Progress Dialog 
      * */ 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(AllEvents.this); 
       pDialog.setMessage("Loading Events ..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      /** 
      * getting Inbox JSON 
      * */ 
      protected String doInBackground(String... args) { 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 

       // getting JSON string from URL 
       JSONObject json = jsonParser.makeHttpRequest(INBOX_URL, "GET", 
         params); 

       // Check your log cat for JSON reponse 
       Log.d("Inbox JSON: ", json.toString()); 

       try { 
        inbox = json.getJSONArray(TAG_MESSAGES); 
        // looping through All messages 
        for (int i = 0; i < inbox.length(); i++) { 
         JSONObject c = inbox.getJSONObject(i); 

         // Storing each json item in variable 

         String from = c.getString(TAG_FROM); 
         String subject = c.getString(TAG_SUBJECT); 
         String date = c.getString(TAG_DATE); 

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

         picture = String.valueOf(R.drawable.ic_launcher); 
         // adding each child node to HashMap key => value 

         map.put(TAG_PIC, picture); 
         map.put(TAG_FROM, from); 
         map.put(TAG_SUBJECT, subject); 
         map.put(TAG_DATE, date); 





         // adding HashList to ArrayList 
         inboxList.add(map); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

       return null; 
      } 

      /** 
      * After completing background task Dismiss the progress dialog 
      * **/ 
      protected void onPostExecute(String file_url) { 
       // dismiss the dialog after getting all products 
       pDialog.dismiss(); 
       // updating UI from Background Thread 
       runOnUiThread(new Runnable() { 
        public void run() { 
         /** 
         * Updating parsed JSON data into ListView 
         * */ 
         SimpleAdapter adapter = new SimpleAdapter(
           AllEvents.this, inboxList, 
           R.layout.event_list_item, new String[] { TAG_PIC, TAG_FROM, TAG_SUBJECT, TAG_DATE, }, 
           new int[] { R.id.imageView1,R.id.from, R.id.subject, R.id.date }); 
         // updating listview 



         setListAdapter(adapter); 
        } 
       }); 

      } 

     } 

和佈局 'event_list_item.xml' 由SimpleAdapter使用如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- From Label --> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:contentDescription="hello" 
      android:paddingTop="10dp" 
      android:paddingRight="10dp" 
      android:paddingBottom="10dp" 
      android:layout_toLeftOf="@+id/subject" 
     /> 


    <TextView 
     android:id="@+id/from" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="8dip" 
     android:paddingLeft="8dip" 
     android:paddingBottom="4dip" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    <!-- Mail Subject --> 
    <TextView android:id="@+id/subject" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:paddingLeft="8dip" 
     android:paddingBottom="6dip" 
     android:textSize="15sp" 
     android:layout_below="@id/from"/> 

    <!-- Mail date --> 
    <TextView android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:padding="8dip"/> 



</RelativeLayout> 

我該怎麼辦獲得lazy-列表像查看在一個簡單的適配器?

+0

讓你'map' - '的HashMap <字符串,對象>''代替的HashMap '和'圖片' - 'int'而不是'字符串... ... http://stackoverflow.com/questions/6305899/custom-listview-android#6306901 – Selvin 2013-02-20 15:44:54

+0

我試過塞爾文,但它不是有效的! :( – Vijay 2013-02-20 16:52:15

+0

已解決 - 我的XML佈局錯誤,我使用了線性佈局,它工作:) – Vijay 2013-02-21 05:40:13

回答

0

的圖像不是一個字符串,那麼由String對象一樣,替換字符串:

ArrayList<HashMap<String, object>> inboxList = new ArrayList<HashMap<String, object>>(); 
    HashMap<String, Object> map = new HashMap<String, Object>();