1

我非常需要我們的幫助。Android ExpandableList - 圖像加載程序 - 簡單的適配器

我需要創建一個expandableListView並把更多的圖像加載到URL。

現在我有這樣的:

我把文本文件中的XML,但我不知道在圖像如何表現我的形象DINAMIC。

這是代碼:

Main.java 

public class Main extends ExpandableListActivity { 


    static final String KEY_DISCOUNTED_PRICE = "discounted_price"; 
    static final String KEY_PRICE = "price"; 
    static final String KEY_DISCOUNT = "discount"; 
    static final String KEY_THUMB_URL_SELLER = "seller_image_url"; 
    static final String KEY_THUMB_URL = "image_url"; 
    static final String KEY_DESCRIPTION = "description"; 
    static final String KEY_TITLE_OFFERTA = "title_offerta";  
    String contenuto_nodo; 
    ArrayList <String> array_contenuto_nodo =new ArrayList<String>(); 
    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 




    static final String shades[][] = { 
// Shades of grey 
     { 
     "lightgrey","#D3D3D3", 
     "dimgray","#696969", 
     "sgi gray 92","#EAEAEA" 
     }, 
// Shades of blue 
     { 
     "dodgerblue 2","#1C86EE", 
     "steelblue 2","#5CACEE", 
     "powderblue","#B0E0E6" 
     }, 
// Shades of yellow 
     { 
     "yellow 1","#FFFF00", 
     "gold 1","#FFD700", 
     "darkgoldenrod 1"," #FFB90F" 
     }, 
// Shades of red 
     { 
     "indianred 1","#FF6A6A", 
     "firebrick 1","#FF3030", 
     "maroon","#800000" 
     } 
    }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 


     TaskHomePageLista task = new TaskHomePageLista(); 
     task.execute(); 
     try {Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     // ImageLoader class instance 

     SimpleExpandableListAdapter expListAdapter = 
      new SimpleExpandableListAdapter(
       this, 
       createGroupList(), // groupData describes the first-level entries 
       R.layout.list_row, // Layout for the first-level entries 
       new String[] { KEY_PRICE, KEY_DISCOUNT, KEY_DESCRIPTION, KEY_DISCOUNTED_PRICE, KEY_TITLE_OFFERTA}, // Key in the groupData maps to display 
       new int[] { R.id.price, R.id.discount, R.id.description, R.id.discounted_price, R.id.title_offerta},  // Data under "colorName" key goes into this TextView 

       createChildList(), // childData describes second-level entries 
       R.layout.child_row, // Layout for second-level entries 
       new String[] { "shadeName", "rgb" }, // Keys in childData maps to display 
       new int[] { R.id.childname, R.id.rgb } 
      ); 


     setListAdapter(expListAdapter); 


     // Imageview to show 


    } 

/** 
    * Creates the group list out of the colors[] array according to 
    * the structure required by SimpleExpandableListAdapter. The resulting 
    * List contains Maps. Each Map contains one entry with key "colorName" and 
    * value of an entry in the colors[] array. 
    */ 
    private List createGroupList() { 


     return (List)songsList; 
    } 

/** 
    * Creates the child list out of the shades[] array according to the 
    * structure required by SimpleExpandableListAdapter. The resulting List 
    * contains one list for each group. Each such second-level group contains 
    * Maps. Each such Map contains two keys: "shadeName" is the name of the 
    * shade and "rgb" is the RGB value for the shade. 
    */ 
    private List createChildList() { 
    ArrayList result = new ArrayList(); 
    for(int i = 0 ; i < shades.length ; ++i) { 
// Second-level lists 
     ArrayList secList = new ArrayList(); 
     for(int n = 0 ; n < shades[i].length ; n += 2) { 
     HashMap child = new HashMap(); 
     child.put("shadeName", shades[i][n]); 
     child.put("rgb", shades[i][n+1]); 
     secList.add(child); 
     } 
     result.add(secList); 
    } 
    return result; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    class TaskHomePageLista extends AsyncTask<String, Void, Void> { 




     protected void onPreExecute(Void... params) { 

     } 

     protected Void doInBackground(String... args) { 

      try { 

       // passo i parametri di mail e password alle variabili 


       //setto la pagina 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpContext localContext = new BasicHttpContext(); 
       HttpPost httpPost = new HttpPost("http://www.miosito.HomePageXml.aspx"); 
       // aggiungo alla lista le variabili 

       // chiamo la pagina che mi da una risposta. 

       HttpResponse response = httpClient.execute(httpPost, localContext); 

       InputStream in = response.getEntity().getContent(); 

       DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
         .newDocumentBuilder(); 
       Document doc = builder.parse(in); 
       String[] nomi = new String[6]; 
       nomi[0] = "price"; 
       nomi[1] = "image_url"; 
       nomi[2] = "seller_image_url"; 
       nomi[3] = "discount"; 
       nomi[4] = "discounted_price"; 
       nomi[5] = "description"; 




       if (doc != null) { 
        // dichiaro n3 per la lunghezza dell'item principale di quello che devo selezionare. 

        NodeList n3 = doc.getElementsByTagName("promo"); 
         NodeList n2 = doc.getElementsByTagName("home"); 
        for (int i = 0; i < n3.getLength(); i++) { 

         HashMap<String, String> map = new HashMap<String, String>(); 
         for(int num=0; num < nomi.length; num++){ 
         n2 = doc.getElementsByTagName(nomi[num]); 

          if (n2.getLength() > 0) { 
          Node node = n2.item(i); 

          contenuto_nodo = node.getTextContent(); 
          array_contenuto_nodo.add(contenuto_nodo+"///"); 


           if(nomi[num].equals("price")){ 
           map.put(KEY_PRICE, "Valore" + Html.fromHtml("<BR>" + node.getTextContent() + " €")); 

           } 

           if(nomi[num].equals("image_url")){ 
           map.put(KEY_THUMB_URL, node.getTextContent()); 
           } 

//        if(nomi[num].equals("seller_image_url")){ 
//        map.put(KEY_THUMB_URL_SELLER, node.getTextContent()); 
//        } 

           if(nomi[num].equals("discount")){ 
           map.put(KEY_DISCOUNT, "Sconto" + Html.fromHtml("<BR>" + node.getTextContent() + " %")); 
           } 

           if(nomi[num].equals("discounted_price")){ 
           map.put(KEY_DISCOUNTED_PRICE, node.getTextContent()+ " €"); 
           } 
           if(nomi[num].equals("description")){ 
           map.put(KEY_DESCRIPTION, node.getTextContent()); 
           } 

           System.out.println("sto elaborando"); 

           } 

          n2 = doc.getElementsByTagName("home"); 
          } 
         n2 = doc.getElementsByTagName("promo"); 
         if (n2.getLength() > 0) { 
          Node node = n2.item(i); 
           node = node.getFirstChild(); 
          map.put(KEY_TITLE_OFFERTA, node.getTextContent()); 
         } 


         songsList.add(map); 


        } 


       System.out.println("array_titolo_categoria_home is: " + array_contenuto_nodo); 

//    controllo_login_tempo = "finito"; 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 



      return null; 

     } 

     protected void onPostExecute(Void... params) { 

     } 

     } 
    } 

在KEY_THUMB_URL AsynkTask後我有圖像的URL。我需要使用圖像加載器在哈特展示。但我不知道如何在SimpleExpandableListAdapter中實現圖像加載器。請幫助我是非常重要的!

我想我可以用這個:

int loader = R.drawable.loader; 

// Imageview to show 
ImageView image = (ImageView) findViewById(R.id.image); 

// Image url 
String image_url = "http://api.androidhive.info/images/sample.jpg"; 

// ImageLoader class instance 
ImageLoader imgLoader = new ImageLoader(getApplicationContext()); 

// whenever you want to load an image from url 
// call DisplayImage function 
// url - image url to load 
// loader - loader image, will be displayed before getting image 
// image - ImageView 
imgLoader.DisplayImage(image_url, loader, image); 

,但我不知道在那裏我必須把這個代碼...請幫助!

看這個圖像屏幕 http://www.magicfly.it/app/screen.jpg

+0

我太多掙扎存在....想到什麼Ø/P + – 2013-05-10 09:54:41

回答

0

最後,我找到了解決辦法。

「SimpleAdapter的外部客戶端可以使用此類將值綁定到視圖。您應該使用此類將值綁定到SimpleAdapter不直接支持的視圖,或者更改SimpleAdapter支持的視圖的綁定方式「。

所以ImageView。期望的綁定值是資源ID或字符串,並調用setViewImage(ImageView,int)或setViewImage(ImageView,String)。

公共類MyViewBinder實現ViewBinder {

@Override 
    public boolean setViewValue(View view, Object data, 

    String textRepresentation) { 

     if ((view instanceof ImageView) & (data instanceof Bitmap)) { 

      ImageView iv = (ImageView) view; 

      Bitmap bm = (Bitmap) data; 

      iv.setImageBitmap(bm); 

      return true; 

     } 
     return false; 
    } 

} 

HashMap<String, Object> man = new HashMap<String,Object>(); 
        Bitmap bitmap = BitmapFactory.decodeStream(aURL.openStream()); 
        modles.put(samURL,bitmap); 

然後

adapter.setViewBinder(new MyViewBinder()); //its below declaration of adapter 

來源:click here

相關問題