Android可扩展列表 - 图像加载器 - 简单适配器

时间:2013-05-09 11:05:36

标签: android android-imageview expandablelistview expandablelistadapter android-loader

我非常需要我们的帮助。

我需要创建一个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) {

        }

        }
    }

在AsynkTask之后的KEY_THUMB_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

1 个答案:

答案 0 :(得分: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