开发者

GridView with images from xml file in the web

I'm trying to show some images which URLs are in an XML file that you can find it on the web. I've tried the gridview and it worked, I've tried to show an image from the XML file and it worked, but when I put both together, it doesn't work!! I don't know the reason. Any help please?

The code is:

package com.examples.xmlparser;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class XMLParser extends Activity {


 private List<String> listPicName;
 public List<Drawable> pictures;

       @Override
       public void onCreate(Bundle icicle) {
               super.onCreate(icicle);
               setContentView(R.layout.gridview);

               ImageView imgView =(ImageView)findViewById(R.id.ImageView01);

               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) ;

               try {
                       /* Create a URL we want to load some xml-data from. */
                       URL url = new URL("http://94.23.207.167/websiteIphone/Admin/XML/Santa.xml");

                       /* Get a SAXParser from the SAXPArserFactory. */
                       SAXParserFactory spf = SAXParserFactory.newInstance();
                       SAXParser sp = spf.newSAXParser();

                       /* Get the XMLReader of the SAXParser we created. */
                       XMLReader xr = sp.getXMLReader();
                       /* Create a new ContentHandler and apply it to the XML-Reader*/
                       ExampleHandler myExampleHandler = new ExampleHandler();
                       xr.setContentHandler(myExampleHandler);

                       /* Parse the xml-data from our URL. */
                       xr.parse(new InputSource(url.openStream()));
                       /* Parsing has finished. */

                       /* Our ExampleHandler now provides the parsed data to us. */
                       ParsedExampleDataSet parsedExampleDataSet =  myExampleHandler.getParsedData();

                       listPicName=parsedExampleDataSet.getlistPicName();
                       /* Set the result to be displayed in our GUI. */
                //       tv.setText(parsedExampleDataSet.toString());

                       for (int j=1;j<listPicName.size();j++){
                           Toast.makeText(XMLParser.this, "" + listPicName.get(j), Toast.LENGTH_SHORT).show();

                           Drawable d=LoadImageFromWebOperations(j);
                        Log.w("ANTES","ANTES");

                           pictures.add(j, d);
            开发者_JAVA技巧               Log.w("DESPUES","DESPUES");


                       }


                     //  imgView.setImageDrawable(LoadImageFromWebOperations(10));
                       GridView gridview = (GridView) findViewById(R.id.gridview);
                       gridview.setAdapter(new ImageAdapter(this));

               } catch (Exception e) {
                       /* Display any Error to the GUI. */
                     //  tv.setText("Error: " + e.getMessage());
               }
               /* Display the TextView. */
              // this.setContentView(tv);
       }









       public class ImageAdapter extends BaseAdapter {
         private Context mContext;

         public ImageAdapter(Context c) {
             mContext = c;
         }

         public int getCount() {
             return 0; //mThumbIds.length;
         }

         public Object getItem(int position) {
             return null;
         }

         public long getItemId(int position) {
             return 0;
         }


         // create a new ImageView for each item referenced by the Adapter
         public View getView(int position, View convertView, ViewGroup parent) {
             ImageView imageView;
                if (convertView == null) {  // if it's not recycled, initialize some attributes
                    imageView = new ImageView(mContext);
                    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(8, 8, 8, 8);
                } else {
                    imageView = (ImageView) convertView;
                }
                //imageView.setImageResource(mThumbIds[position]);
                //Drawable d=LoadImageFromWebOperations(position);
                imageView.setImageDrawable(pictures.get(position));
                //Drawable d=LoadImageFromWebOperations(position);
                //imageView.setBackgroundDrawable(d); 
             return imageView;
         }


     }




       private Drawable LoadImageFromWebOperations(int position)
    {
        String path=("http://94.23.207.167/websiteIphone/Admin/content/Santa/Santa/3_1.jpg");
     //   path=path.concat(listPicName.get(position));
     //   path=path.concat(".jpg");
           Toast.makeText(XMLParser.this, "" +path, Toast.LENGTH_LONG).show();
           Log.w("MIERDA","MIERDA");

         try{
             InputStream is = (InputStream) new URL(path).getContent();
             Drawable d = Drawable.createFromStream(is, "src name");
             Log.w("CREADO","CREADO");
             return d;
         }catch (Exception e) {
             System.out.println("Exc="+e);
             return null;
         }
  }


}

The XML file is:

GridView xmlns:android="http://schemas.android.com/apk/res/android" 

    android:id="@+id/gridview"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:columnWidth="90dp"

    android:numColumns="auto_fit"

    android:verticalSpacing="10dp"

    android:horizontalSpacing="10dp"

    android:stretchMode="columnWidth"

    android:gravity="center"

/>


I think, you have to replace your code :

public int getCount() {
             return 0; //mThumbIds.length;
         }

with this code :

public int getCount() { 
    return pictures.size(); 

        } 

Because, as the size of the pictures(List) is zero in your code, the remaining part of your code will not get executed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜