2014年6月4日 星期三

ListView與其他控件共存的問題(例如Button)

今天遇到了ListView中存在其他控件時,造成ListView原本可以對item的點擊事件,變得沒有用處了,所以就上網找到了這篇文章,照著修改部分code就可以達到我的要求,還不賴~
非常感謝網路上的能人,讓我能夠解決我在開發程式時的各種難題。

[轉]
这两天在捣鼓ListView widget,为了在ListView中加入Button这类的有 “点击” 事件的widget,请教了不少高手,感谢LandMark对我的认真讲解,下面把解决过程描述一下。
ListView 和 其它能触发点击事件的widget无法一起正常工作的原因是加入其它widget后,ListView的itemclick事件将无法触发,被其它widget的click事件屏蔽。
  • 首先,说明一下,ListView中每一行包括以下三项:
   一个ImageView, 一个TextView,一个ImageButton,依次排开。
以下是layout的内容,分为两部分:
  • res/layout/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:padding= "10dip" android:orientation= "vertical" >

    < ListView android:id= "@id/android:list" android:layout_width= "fill_parent"
        android:layout_height= "fill_parent" / >
< / LinearLayout>
因为继承了ListActivity,所以ListView 的id设置为"@id/android:list"是必须的
  • res/layout/lvitem.xml
注意:
< RelativeLayout>中
android:descendantFocusability= "blocksDescendants"
< ImageButton>中
android:focusable = "false"
这两项的设置很关键,如果不设置,将导致ListView的ItemClick事件将无法触发,该事件被ImageButton的click事件屏蔽了。 
< ? xml version = "1.0" encoding = "utf-8" ? >
< RelativeLayout
  xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= "fill_parent"
  android:layout_height= "wrap_content"
  android:padding= "5dip"
  android:descendantFocusability= "blocksDescendants" >

  < ImageView
      android:id= "@+id/ItemImage"
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:padding= "5dip"
  / >


  < !--
      把按钮背景设置为透明:     android:background= "#00000000"
      把按钮背景设置为半透明:     android:background= "#e0000000"
      -->
  < ImageButton
     android:id= "@+id/ItemCloseWin"

     android:layout_alignParentRight= "true"
     android:layout_alignTop= "@+id/ItemWinName"
      android:layout_alignBottom= "@+id/ItemWinName"
      android:layout_width= "wrap_content"
      android:layout_height= "wrap_content"

      android:background= "#e0000000"
      android:gravity= "left|center_vertical"
      android:focusable= "false"
      android:src= "@android:drawable/ic_menu_close_clear_cancel"
  / >

  < TextView
      android:id= "@+id/ItemWinName"

      android:layout_toRightOf= "@+id/ItemImage"
      android:layout_toLeftOf= "@+id/ItemCloseWin"
      android:layout_alignTop= "@+id/ItemImage"
      android:layout_alignBottom= "@+id/ItemImage"
      android:layout_width= "wrap_content"
      android:layout_height= "wrap_content"

      android:gravity= "left|center_vertical"
      android:textSize= "20dip"
      android:text= "title"
  / >


< / RelativeLayout>
  • 接下来,我们看看继承ListActivity的实现
lvWithButtonExt中,为了能处理ImageButton的click事件,我继承了BaseAdapter类,并重新实现了getView()接口,在其中加入了Button的clicklistener,详见 lvButtonAdapter类的实现。
public class lvWithButtonExt extends ListActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super . onCreate( savedInstanceState) ;
        setContentView( R. layout . main) ;

        // 关联Layout中的ListView
        ListView vncListView = ( ListView ) findViewById( android. R. id . list );

        // 生成动态数组,加入数据 
        ArrayList < HashMap < String , Object > > remoteWindowItem = new ArrayList< HashMap < String , Object > > ( ) ;
        for ( int i= 0; i< 10; i+ + )
        {
            HashMap < String , Object > map = new HashMap < String , Object > ( );
            map . put ( "ItemImage" , R. drawable. firefox) ; //图像资源的ID 
            map . put ( "ItemWinName" , "Window ID " + i) ;
            map . put ( "ItemCloseWin" , android. R. drawable.ic_menu_close_clear_cancel) ;
            remoteWindowItem. add ( map ) ;
        }

      // 生成适配器的Item和动态数组对应的元素 
        lvButtonAdapter listItemAdapter = new lvButtonAdapter(
            this ,
            remoteWindowItem, //数据源 
            R. layout . lvitem, //ListItem的XML实现 
            //动态数组与ImageItem对应的子项 
            new String [ ] { "ItemImage" , "ItemWinName" , "ItemCloseWin" } ,
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID 
            new int [ ] { R. id . ItemImage, R. id . ItemWinName, R. id .ItemCloseWin}
        ) ;

        vncListView. setAdapter( listItemAdapter) ;
    }

    @Override
    protected void onListItemClick( ListView l, View v, int position , long id ) {
        // TODO Auto-generated method stub
        super . onListItemClick( l, v, position , id ) ;
        l. getItemAtPosition( position ) ;
    }
}
  • 接下来,我们看看lvButtonAdapter 的实现
为了响应按钮的点击事件,首先要记录按钮的位置,然后为按钮设置clicklistener。
在重新实现的getView()接口中,我使用了lvButtonListener监听类,在构造函数中,记录行号,以便在OnClick接口中能准确的定位按钮所在的位置,进而对相应的行进行处理。
public class lvButtonAdapter extends BaseAdapter {
    private class buttonViewHolder {
        ImageView appIcon;
        TextView appName;
        ImageButton buttonClose;
    }

    private ArrayList < HashMap < String , Object > > mAppList;
    private LayoutInflater mInflater;
    private Context mContext;
    private String [ ] keyString;
    private int [ ] valueViewID;
    private buttonViewHolder holder;

    public lvButtonAdapter( Context c, ArrayList < HashMap < String , Object > >appList, int resource,
            String [ ] from , int [ ] to) {
        mAppList = appList;
        mContext = c;
        mInflater = ( LayoutInflater) mContext. getSystemService( Context .LAYOUT_INFLATER_SERVICE) ;
        keyString = new String [ from . length ] ;
        valueViewID = new int [ to. length ] ;
        System . arraycopy ( from , 0, keyString, 0, from . length ) ;
        System . arraycopy ( to, 0, valueViewID, 0, to. length ) ;
    }

    @Override
    public int getCount ( ) {
        return mAppList. size ( ) ;
    }

    @Override
    public Object getItem ( int position ) {
        return mAppList. get ( position ) ;
    }

    @Override
    public long getItemId( int position ) {
        return position ;
    }

    public void removeItem ( int position ) {
        mAppList. remove ( position ) ;
        this . notifyDataSetChanged( ) ;
    }

    @Override
    public View getView ( int position , View convertView, ViewGroup parent ) {
        if ( convertView ! = null ) {
            holder = ( buttonViewHolder) convertView. getTag ( ) ;
        } else {
            convertView = mInflater. inflate ( R. layout . lvitem, null ) ;
            holder = new buttonViewHolder( ) ;
            holder. appIcon = ( ImageView ) convertView. findViewById( valueViewID[ 0]) ;
            holder. appName = ( TextView) convertView. findViewById( valueViewID[ 1] );
            holder. buttonClose = ( ImageButton) convertView. findViewById(valueViewID[ 2] ) ;
            convertView. setTag( holder) ;
        }

        HashMap < String , Object > appInfo = mAppList. get ( position ) ;
        if ( appInfo ! = null ) {
            String aname = ( String ) appInfo. get ( keyString[ 1] ) ;
            int mid = ( Integer ) appInfo. get ( keyString[ 0] ) ;
            int bid = ( Integer ) appInfo. get ( keyString[ 2] ) ;
            holder. appName. setText ( aname) ;
            holder. appIcon. setImageDrawable( holder. appIcon. getResources ( ) .getDrawable( mid) ) ;
            holder. buttonClose. setImageDrawable( holder. buttonClose. getResources () . getDrawable( bid) ) ;
            holder. buttonClose. setOnClickListener( new lvButtonListener( position ) );
        }
        return convertView;
    }

    class lvButtonListener implements OnClickListener {
        private int position ;

        lvButtonListener( int pos) {
            position = pos;
        }

        @Override
        public void onClick( View v) {
            int vid= v. getId ( ) ;
            if ( vid = = holder. buttonClose. getId ( ) )
                removeItem ( position ) ;
        }
    }
}

////////////////////////////////////////
备注1; 对于Android开发来说处理一些界面需要和Adapter适配器打交道,虽然Android自带了一些比如ArrayAdapter但是大多数情况下无法满足我们需要,所以就要从BaseAdapter派生一个类满足我们特殊的需要。
  首先我们可能重写getView(),通过LayoutInflater的inflate方法映射一个自己定义的Layout布局xml加载或从xxxView中创建。这些大家可能滚瓜烂熟了但是仍然很多Android 开发者对于BaseAdapter中notifyDataSetChanged()方法不是很理解,notifyDataSetChanged方法通过一个外部的方法控制如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容。
http://blogimg.chinaunix.net/blog/upfile2/101203142001.jpg

2014年5月28日 星期三

列出裝置內已安裝的package名稱

為了要讓NFC啟動裝置內已安裝的app,必須要先知道該app的package名稱,才可以在Tag上設定AAR的參數。那就先利用listView來看看下面的code能否達到此功能吧~!

[轉自] http://nkeegamedev.blogspot.tw/2012/09/android-apps-package.html

PackageManager pmPack;
pmPack = getPackageManager();List packinfo = pmPack.getInstalledPackages(PackageManager.GET_ACTIVITIES);String[] pak = new String[packinfo.size()];for ( int i=0; i < packinfo.size(); i++) {          PackageInfo p = (PackageInfo) packinfo.get(i);          pak[i] = p.packageName;}ListView lv = (ListView) findViewById(R.id.listView1);ArrayAdapter lstadapter = new ArrayAdapter(         MainActivity.this,android.R.layout.simple_list_item_1, pak);lv.setAdapter(lstadapter);


[轉自] http://www.cnblogs.com/mainroadlee/archive/2011/05/23/android_get_installed_app.html

Android获取已安装应用信息(图标,名称,版本号,包)



Android 菜市场上有一款应用较 ShareApp,可以显示,管理,分享Android手机上安装的应用。
但比较不爽的是,它把很多系统自带的应用程序也都显示了出来。这些程序往往是无法卸载的,更无法分享,让“已安装应用程序”的列表显的非常凌乱。
我在手机上运行了一下,结果Gtalk,DRMService这些系统应用都显示出来了..比较不爽。
于是自己写了个程序,看看能不能只显示用户自己安装的程序。
程序大概分成三个部分:
1.获取手机已安装的所有应用package的信息(其中包括用户自己安装的,还有系统自带的);
2.滤除系统自带应用;
3.通过列表显示出应用程序的图标(icon),和其他文字信息(应用名称,包名称package name,版本号等等)
首先,我们定义一个数据结构,来保存应用程序信息(icon,name,packageName,versionName,versionCode,等)
复制代码
publicclass AppInfo {public String appName="";public String packageName="";public String versionName="";publicint versionCode=0;public Drawable appIcon=null;publicvoid print()
{
Log.v(
"app","Name:"+appName+" Package:"+packageName);
Log.v(
"app","Name:"+appName+" versionName:"+versionName);
Log.v(
"app","Name:"+appName+" versionCode:"+versionCode);
}

}
复制代码
然后我们通过PackageManager 来获取已安装的应用包信息。
复制代码
     ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据
     List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo 
= packages.get(i);
AppInfo tmpInfo 
=new AppInfo();
tmpInfo.appName 
= packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName 
= packageInfo.packageName;
tmpInfo.versionName 
= packageInfo.versionName;
tmpInfo.versionCode 
= packageInfo.versionCode;
tmpInfo.appIcon 
= packageInfo.applicationInfo.loadIcon(getPackageManager());
appList.add(tmpInfo);

}
//好啦 这下手机上安装的应用数据都存在appList里了。
复制代码
那么如何判断一个应用是否为系统应用呢?
复制代码
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)
{
//非系统应用
}else
{
//系统应用        
}
复制代码
所以如果只需要获取到非系统应用信息,代码如下:
复制代码
ArrayList<AppInfo> appList = new ArrayList<AppInfo>(); //用来存储获取的应用信息数据
List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packages.size();i++) {
PackageInfo packageInfo 
= packages.get(i);
AppInfo tmpInfo 
=new AppInfo();
tmpInfo.appName 
= packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
tmpInfo.packageName 
= packageInfo.packageName;
tmpInfo.versionName 
= packageInfo.versionName;
tmpInfo.versionCode 
= packageInfo.versionCode;
tmpInfo.appIcon 
= packageInfo.applicationInfo.loadIcon(getPackageManager());//Only display the non-system app info
if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)
{
appList.add(tmpInfo);//如果非系统应用,则添加至appList
}

}
复制代码
最后,我们可以自定义一个app_row.xml的layout 文件,用来显示appList中的数据
是不是看着利索多了~~~(我在每行里只显示了icon 和 appName,你也可以修改app_row.xml 和 AppAdapter 部分后显示更多的信息)
大家可以参考我的源码:

参考:
1. [Android分享] Android 得到已安装的应用程序信息 
该文中描述了如何获取Android 已安装应用信息,但关于系统应用的判断部分存在错误。
2. Android中级篇之区分系统程序和安装程序

2013年2月6日 星期三

利用Yahoo天氣API,開發Android天氣查詢App

轉自:
Android-er: Search WOEID and query Yahoo Weather

因為Google停止了iGoogle的服務,也停止了Google Weather Api的服務,因為沒有好用的Google提供的天氣Api只好找尋其他的天氣服務。

首先利用Yahoo提供的API將城市名轉換為WOEID,再利用WOEID取得Yahoo Weather API中所提供的天氣訊息,詳情請參考連結。

不過目前對於web的存取及解析還不太了解,這些等之後有更進一步的研究之後,再來好好回過頭來分析這支程式在寫啥吧

另外改天在把修改後的source code貼上~~~

Search WOEID and query Yahoo Weather

User enter location to be searched for WOEID, then clicked the WOEID to start another activity querying Yahoo for the weather.

And also, the code to read from internet have been moved to AsyncTask to prevent fromNetworkOnMainThreadException.

Main activity, MainActivity.java.
package com.example.androidwoeidweather;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
 
 //Example for "New York"
 //http://query.yahooapis.com/v1/public/yql?q=select*from geo.places where text="New York"&format=xml
 final String yahooPlaceApisBase = "http://query.yahooapis.com/v1/public/yql?q=select*from%20geo.places%20where%20text=";
 final String yahooapisFormat = "&format=xml";
 String yahooPlaceAPIsQuery;

 EditText place;
 Button search;
 ListView listviewWOEID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        place = (EditText)findViewById(R.id.place);
        search = (Button)findViewById(R.id.search);
        listviewWOEID = (ListView)findViewById(R.id.woeidlist);

        search.setOnClickListener(searchOnClickListener);
    }
    
    Button.OnClickListener searchOnClickListener
    = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   if(place.getText().toString().equals("")){
    Toast.makeText(getBaseContext(),
      "Enter place!",
      Toast.LENGTH_LONG).show();
   }else{
    new MyQueryYahooPlaceTask().execute();
   }
  }
    };
    
    private class MyQueryYahooPlaceTask extends AsyncTask<Void, Void, Void>{

     ArrayList<String> l;
     
  @Override
  protected Void doInBackground(Void... arg0) {
   l = QueryYahooPlaceAPIs();
   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   ArrayAdapter<String> aa = new ArrayAdapter<String>(
     getBaseContext(), android.R.layout.simple_list_item_1, l);
   listviewWOEID.setAdapter(aa);
   
   listviewWOEID.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
     
     String selWoeid = parent.getItemAtPosition(position).toString();
     
     /*
     Toast.makeText(getApplicationContext(), 
       selWoeid, 
       Toast.LENGTH_LONG).show();
     */
     
     Intent intent = new Intent();
              intent.setClass(MainActivity.this, GetWeather.class);
              Bundle bundle = new Bundle();
              bundle.putCharSequence("SEL_WOEID", selWoeid);
              intent.putExtras(bundle);
              startActivity(intent);
    }});
   
   super.onPostExecute(result);
  }
     
    }
    
    private ArrayList<String> QueryYahooPlaceAPIs(){
     String uriPlace = Uri.encode(place.getText().toString());
     
     yahooPlaceAPIsQuery = yahooPlaceApisBase
       + "%22" + uriPlace + "%22"
       + yahooapisFormat;
     
     String woeidString = QueryYahooWeather(yahooPlaceAPIsQuery);
     Document woeidDoc = convertStringToDocument(woeidString);
     return  parseWOEID(woeidDoc);
    }
    
    private ArrayList<String> parseWOEID(Document srcDoc){
     ArrayList<String> listWOEID = new ArrayList<String>();
     
     NodeList nodeListDescription = srcDoc.getElementsByTagName("woeid");
     if(nodeListDescription.getLength()>=0){
      for(int i=0; i<nodeListDescription.getLength(); i++){
       listWOEID.add(nodeListDescription.item(i).getTextContent()); 
      } 
     }else{
      listWOEID.clear(); 
     }
     
     return listWOEID;
    }
    
    private Document convertStringToDocument(String src){
     Document dest = null;
     
     DocumentBuilderFactory dbFactory =
       DocumentBuilderFactory.newInstance();
     DocumentBuilder parser;
     
     try {
      parser = dbFactory.newDocumentBuilder();
      dest = parser.parse(new ByteArrayInputStream(src.getBytes())); 
     } catch (ParserConfigurationException e1) {
      e1.printStackTrace(); 
     } catch (SAXException e) {
      e.printStackTrace(); 
     } catch (IOException e) {
      e.printStackTrace(); 
     }
     
     return dest; 
    }
    
    private String QueryYahooWeather(String queryString){
     String qResult = "";
     
     HttpClient httpClient = new DefaultHttpClient();
     HttpGet httpGet = new HttpGet(queryString);
     
     try {
      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
      
      if (httpEntity != null){
       InputStream inputStream = httpEntity.getContent();
       Reader in = new InputStreamReader(inputStream);
       BufferedReader bufferedreader = new BufferedReader(in);
       StringBuilder stringBuilder = new StringBuilder();
       
       String stringReadLine = null;
       
       while ((stringReadLine = bufferedreader.readLine()) != null) {
        stringBuilder.append(stringReadLine + "\n"); 
       }
       
       qResult = stringBuilder.toString(); 
      } 
     } catch (ClientProtocolException e) {
      e.printStackTrace();; 
     } catch (IOException e) {
      e.printStackTrace(); 
     }
     
     return qResult; 
    }

}


Layout of the main activity, activity_main.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <EditText
        android:id="@+id/place"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Search WOEID by place" />
    <ListView
        android:id="@+id/woeidlist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


The activity to query weather from Yahoo, GetWeather.java.
package com.example.androidwoeidweather;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class GetWeather extends Activity {
 
 TextView weather;
 
 class MyWeather{
  
  String description;
  String city;
  String region;
  String country;

  String windChill;
  String windDirection;
  String windSpeed;

  String sunrise;
  String sunset;

  String conditiontext;
  String conditiondate;
  
  public String toString(){
   
   String s;
   
   s = description + " -\n\n" + "city: " + city + "\n"
     + "region: " + region + "\n"
     + "country: " + country + "\n\n"
     + "Wind\n"
     + "chill: " + windChill + "\n"
     + "direction: " + windDirection + "\n"
     + "speed: " + windSpeed + "\n\n"
     + "Sunrise: " + sunrise + "\n"
     + "Sunset: " + sunset + "\n\n"
     + "Condition: " + conditiontext + "\n"
     + conditiondate +"\n";
   
   return s;
  }
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_weather);
  weather = (TextView)findViewById(R.id.weather);
  
  Bundle bundle = this.getIntent().getExtras();
        String sel_woeid = (String)bundle.getCharSequence("SEL_WOEID");
        
        new MyQueryYahooWeatherTask(sel_woeid).execute();
        
        Toast.makeText(getApplicationContext(), 
          sel_woeid, 
    Toast.LENGTH_LONG).show();
 }
 
 private class MyQueryYahooWeatherTask extends AsyncTask<Void, Void, Void>{

  String woeid;
  String weatherResult;
  String weatherString;
  
  MyQueryYahooWeatherTask(String w){
   woeid = w;
  }
  
  @Override
  protected Void doInBackground(Void... arg0) {
   weatherString = QueryYahooWeather();
   Document weatherDoc = convertStringToDocument(weatherString);
   
   if(weatherDoc != null){
    weatherResult = parseWeather(weatherDoc).toString();
   }else{
    weatherResult = "Cannot convertStringToDocument!";
   }
   
   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   weather.setText(weatherResult);
   super.onPostExecute(result);
  }
  
  private String QueryYahooWeather(){
   String qResult = "";
     String queryString = "http://weather.yahooapis.com/forecastrss?w=" + woeid;
     
     HttpClient httpClient = new DefaultHttpClient();
     HttpGet httpGet = new HttpGet(queryString);
     
     try {
      HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
      
      if (httpEntity != null){
       InputStream inputStream = httpEntity.getContent();
       Reader in = new InputStreamReader(inputStream);
       BufferedReader bufferedreader = new BufferedReader(in);
       StringBuilder stringBuilder = new StringBuilder();
       
       String stringReadLine = null;
       
       while ((stringReadLine = bufferedreader.readLine()) != null) {
        stringBuilder.append(stringReadLine + "\n");  
       }
       
       qResult = stringBuilder.toString();  
      }  
     } catch (ClientProtocolException e) {
      e.printStackTrace(); 
     } catch (IOException e) {
      e.printStackTrace(); 
     }
     return qResult;   
  }
  
  private Document convertStringToDocument(String src){
   Document dest = null;
   
   DocumentBuilderFactory dbFactory =
     DocumentBuilderFactory.newInstance();
   DocumentBuilder parser;

   try {
    parser = dbFactory.newDocumentBuilder();
    dest = parser.parse(new ByteArrayInputStream(src.getBytes())); 
   } catch (ParserConfigurationException e1) {
    e1.printStackTrace(); 
   } catch (SAXException e) {
    e.printStackTrace(); 
   } catch (IOException e) {
    e.printStackTrace(); 
   }
   
   return dest; 
  }
  
  private MyWeather parseWeather(Document srcDoc){
   
   MyWeather myWeather = new MyWeather();
   
   //<description>Yahoo! Weather for New York, NY</description>
   NodeList descNodelist = srcDoc.getElementsByTagName("description");
   if(descNodelist != null && descNodelist.getLength() > 0){
    myWeather.description = descNodelist.item(0).getTextContent();
   }else{
    myWeather.description = "EMPTY";
   }

   //<yweather:location city="New York" region="NY" country="United States"/>
   NodeList locationNodeList = srcDoc.getElementsByTagName("yweather:location");
   if(locationNodeList != null && locationNodeList.getLength() > 0){
    Node locationNode = locationNodeList.item(0);
    NamedNodeMap locNamedNodeMap = locationNode.getAttributes();
    
    myWeather.city = locNamedNodeMap.getNamedItem("city").getNodeValue().toString();
    myWeather.region = locNamedNodeMap.getNamedItem("region").getNodeValue().toString();
    myWeather.country = locNamedNodeMap.getNamedItem("country").getNodeValue().toString();
   }else{
    myWeather.city = "EMPTY";
    myWeather.region = "EMPTY";
    myWeather.country = "EMPTY";
   }
   
   //<yweather:wind chill="60" direction="0" speed="0"/>
   NodeList windNodeList = srcDoc.getElementsByTagName("yweather:wind");
   if(windNodeList != null && windNodeList.getLength() > 0){
    Node windNode = windNodeList.item(0);
    NamedNodeMap windNamedNodeMap = windNode.getAttributes();
    
    myWeather.windChill = windNamedNodeMap.getNamedItem("chill").getNodeValue().toString();
    myWeather.windDirection = windNamedNodeMap.getNamedItem("direction").getNodeValue().toString();
    myWeather.windSpeed = windNamedNodeMap.getNamedItem("speed").getNodeValue().toString();
   }else{
    myWeather.windChill = "EMPTY";
    myWeather.windDirection = "EMPTY";
    myWeather.windSpeed = "EMPTY";
   }
   
   //<yweather:astronomy sunrise="6:52 am" sunset="7:10 pm"/>
   NodeList astNodeList = srcDoc.getElementsByTagName("yweather:astronomy");
   if(astNodeList != null && astNodeList.getLength() > 0){
    Node astNode = astNodeList.item(0);
    NamedNodeMap astNamedNodeMap = astNode.getAttributes();
    
    myWeather.sunrise = astNamedNodeMap.getNamedItem("sunrise").getNodeValue().toString();
    myWeather.sunset = astNamedNodeMap.getNamedItem("sunset").getNodeValue().toString();
   }else{
    myWeather.sunrise = "EMPTY";
    myWeather.sunset = "EMPTY";
   }
   
   //<yweather:condition text="Fair" code="33" temp="60" date="Fri, 23 Mar 2012 8:49 pm EDT"/>
   NodeList conditionNodeList = srcDoc.getElementsByTagName("yweather:condition");
   if(conditionNodeList != null && conditionNodeList.getLength() > 0){
    Node conditionNode = conditionNodeList.item(0);
    NamedNodeMap conditionNamedNodeMap = conditionNode.getAttributes();
    
    myWeather.conditiontext = conditionNamedNodeMap.getNamedItem("text").getNodeValue().toString();
    myWeather.conditiondate = conditionNamedNodeMap.getNamedItem("date").getNodeValue().toString();
   }else{
    myWeather.conditiontext = "EMPTY";
    myWeather.conditiondate = "EMPTY";
   }
   
   return myWeather; 
  }
  
 }

}


Layout of GetWeather, layout_weather.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" >
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/weather"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>


Modify AndroidManifest.xml to add activity ".GetWeather" and permission of "android.permission.INTERNET".
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidwoeidweather"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GetWeather">
        </activity>
    </application>

</manifest>