接上篇的Adapter的优化 Handler和Thread使用

Handler myHandler new Handler(){
public void handleMessage (Message m){
updateUIHere()
}
};


new Thread(){
     public void run(){
        doStuff();
        Message  m= myHandler.obtainMessage ();
        Bundle b=new Bundle();
        b.putString("key","value");
        m.setData(b);
        myHandler.sendMessage(m);
    }
}.start();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package gt.gt;

import java.net.URL;
import java.util.List;
import java.util.Map;

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.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class RssActivity extends Activity implements OnItemClickListener{
 
 public final String RSS_URL="http://feed.feedsky.com/yeeyan_top";
 public final String tag="RSSReader";
 private RSSFeed feed=null;
 List <Map<String,Object>>  _data;
 
    MyAdapter myAdapter;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        new Thread(){
         public void run(){
           feed=getFeed(RSS_URL);
               
          _data=feed.getAllItemsForListView();
             myHandler.sendEmptyMessage(0);      
         }
        }.start();
      
       
       
    }
   
    Handler myHandler=new Handler(){
     public void handleMessage(Message m){
      
      switch(m.what){
      
      case  0: 
       showListView(); 
       break;
      case 1:
       break;
      }
      
         
     }
    };
   
   
   
    private RSSFeed getFeed(String urlString)
    {
     try{
      URL url=new URL(urlString);
      SAXParserFactory factory=SAXParserFactory.newInstance();
      SAXParser parser=factory.newSAXParser();
      XMLReader xmlreader=parser.getXMLReader();
      
      RSSHandler rssHandler=new RSSHandler();
      xmlreader.setContentHandler(rssHandler);
      InputSource is = new InputSource(url.openStream());
      xmlreader.parse(is);
      return rssHandler.getFeed();
     }
     catch(Exception ee)
     {
      return null;
     }
     
    }
   
    private void showListView()
    {
    ListView itemlist= (ListView)findViewById(R.id.itemlist);
    if(feed==null)
    {
     setTitle("访问的RSS无效");
     return;
    }
   
    myAdapter=new MyAdapter(RssActivity.this,R.layout.row);
    myAdapter.data=_data;
   
   
  //  SimpleAdapter adapter=new SimpleAdapter(this,feed.getAllItemsForListView(),
    //  android.R.layout.simple_list_item_2,new String[]{RSSItem.TITLE,RSSItem.PUBDATE},
     // new int[]{android.R.id.text1,android.R.id.text2});
   
    itemlist.setAdapter(myAdapter);
    itemlist.setOnItemClickListener(this);
    itemlist.setSelection(0);
   
   
    }
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  Intent itemintent=new Intent(this,ShowActivity.class);
  Bundle bundle =new Bundle();
  bundle.putString("title", feed.getItem(arg2).getTitle());
  bundle.putString("link",feed.getItem(arg2).getLink());
  bundle.putString("pubdate",feed.getItem(arg2).getPubdate());
  itemintent.putExtra("android.rssItem", bundle);
  startActivityForResult(itemintent,0);
  
  
 }
   
}

你可能感兴趣的:(thread,exception,优化,ListView,String,url)