关于ListView的用法

实现效果图



Activity文件:

package com.lujianfei.AdPlatform;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.SAXParserFactory;

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

import com.lujianfei.utils.HttpDownloader;
import com.lujianfei.utils.WebConfig;


import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class AdPlatformActivity extends Activity {
	ListView mListView;  
    View mView;  

	
	ContentHandler mHandler=null;
	ArrayList<Ad> AdList=null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mView = LayoutInflater.from(this).inflate(R.layout.ad,null);  
        mListView = (ListView) findViewById(R.id.list);  

        
        ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
        HashMap<String,Object> map=null;
       
        ParseXml();
        
        for(int i=0;i<AdList.size();i++)
        {
        map=new HashMap<String,Object>();
        map.put("ItemImage",getBitmap(WebConfig.Img_url+AdList.get(i).getImg_url()));
        map.put("ItemTitle",AdList.get(i).getTitle());
        list.add(map);
        }
        
        
     
        
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.ad,new String[]{"ItemImage","ItemTitle"},new int[]{R.id.ItemImage,R.id.ItemTitle});
        adapter.setViewBinder(new ViewBinder(){

      	  public boolean setViewValue(View view, Object data, 
      	          String textRepresentation) { 
      	        //判断是否为我们要处理的对象 
      	        if(view instanceof ImageView && data instanceof Bitmap){ 
      	          ImageView iv = (ImageView) view; 
      	          iv.setImageBitmap((Bitmap) data); 
      	          return true; 
      	        }else 
      	        return false; 
      	      } 

	
      });
        mListView.setAdapter(adapter);  
        
        
    }
 
    private void ParseXml()
    {
    	   try{
		        //创建一个SAXParserFactory
		        SAXParserFactory factory = SAXParserFactory.newInstance();
		        XMLReader reader = factory.newSAXParser().getXMLReader();
		        mHandler=new SAXHandler();
		        //为XMLParser设置内容处理器
		        reader.setContentHandler(mHandler);
		        //开始解析文件
			    HttpDownloader mdownloader=new HttpDownloader();
			    String resultStr =mdownloader.download(WebConfig.Video_url);
			    Log.e("MYTEST",resultStr);
		        reader.parse(new InputSource(new StringReader(resultStr)));
		    }catch(Exception e){
		      e.printStackTrace(); 
		    }
    	   AdList=((SAXHandler)mHandler).GetAdList();
    	
    }
   
         public Bitmap getBitmap(String imageUrl){  
        	         Bitmap mBitmap = null;  
        	        try {  
        	            URL url = new URL(imageUrl);  
        	            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        	            InputStream is = conn.getInputStream();  
        	            mBitmap = BitmapFactory.decodeStream(is);  
        	               
        	         } catch (MalformedURLException e) {  
        	             e.printStackTrace();  
        	        } catch (IOException e) {  
        	             e.printStackTrace();  
        	       }  
        	        

        	         return mBitmap;  
         }  


}


SAXHandler文件

package com.lujianfei.AdPlatform;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class SAXHandler extends DefaultHandler{   
    private ArrayList<Ad> AdList = new ArrayList<Ad>(); 
    private Ad ad=null;
    private Ad tempAd=new Ad();
    private String tagName;
    
    
    
    @Override   
    public void startDocument() throws SAXException {   
       //super.startDocument();   

    }   

    @Override   
    public void endDocument() throws SAXException {   
       // super.endDocument();   
    }   

    @Override   
    public void startElement(String uri, String localName, String qName,   
            Attributes attributes) throws SAXException {
    	tagName = localName;
     
//        Log.e("startTag",localName);   
//        Log.e("MYTEST","startElement");
    }   

    @Override   
    public void endElement(String uri, String localName, String qName)   
            throws SAXException {   
    	tagName = localName;
    	if(localName.equals("video")){
    		ad = new Ad();
    		ad.setId(tempAd.getId());
    		ad.setTitle(tempAd.getTitle());
    		ad.setFileName(tempAd.getFileName());
    		ad.setImg_url(tempAd.getImg_url());
    		AdList.add(ad);
  	    }
//    	Log.e("MYTEST","localName:"+localName);   
//    	Log.e("MYTEST","endElement");
//        Log.e("endTag",localName);   
    }   

    @Override   
    public void characters(char[] ch, int start, int length)   
            throws SAXException {   
    		
    	  if(tagName.equals("id"))
    		  tempAd.setId(Integer.parseInt(new String(ch,start,length)));
  	      else if(tagName.equals("title"))
  	    	  tempAd.setTitle(new String(ch,start,length));  
  	      else if(tagName.equals("filename"))
  	    	  tempAd.setFileName(new String(ch,start,length));
  	      else if(tagName.equals("img_url"))
  	    	  tempAd.setImg_url(new String(ch,start,length));
    	  //Log.e("MYTEST",ad.getTitle());  
    }   
       
    public ArrayList<Ad> GetAdList()   
    {   
        return AdList;   
    }   

       

}   

Ad.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip"
  >
  <ImageView  
        android:id="@+id/ItemImage"  
        android:layout_height="wrap_content"  
        android:layout_width="wrap_content"  
        android:layout_centerHorizontal="true"
          
        />  
   <TextView 
      android:id="@+id/ItemTitle"
      android:layout_width="fill_parent"
      android:layout_height="30dip"
      android:gravity="right"
      android:textSize="10pt"
      />
</LinearLayout>

main.xml

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

     <ListView android:id="@+id/list"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:orientation="vertical" 
          android:drawSelectorOnTop="false"
          android:scrollbars="vertical"
          />
</LinearLayout>

(注意:别忘了给Manifest.xml加上<uses-permission android:name="android.permission.INTERNET" />, 因为里面涉及到Internet的访问)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值