ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听.

本篇主要功能.

通过xml pull解析得到数据,然后通过自定义的Adapter绑定数据源,ListView绑定适配器,并且实现Item项的点击事件以及子View控件的点击事件.


一.实体类.

Book.java

[java]  view plain copy
  1. package cn.skychi.news;  
  2.   
  3. /** 
  4.  * @package : cn.skychi.news 
  5.  * @description: 实体类. 
  6.  * @author : qc 
  7.  * @version : v1.0 
  8.  * @date : 2012-11-29 下午2:45:49 
  9.  */  
  10.   
  11. public class Book  
  12. {  
  13.     private int id;  
  14.   
  15.     private String name;  
  16.   
  17.     private float price;  
  18.   
  19.     public int getId()  
  20.     {  
  21.         return id;  
  22.     }  
  23.   
  24.     public void setId(int id)  
  25.     {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getName()  
  30.     {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name)  
  35.     {  
  36.         this.name = name;  
  37.     }  
  38.   
  39.     public float getPrice()  
  40.     {  
  41.         return price;  
  42.     }  
  43.   
  44.     public void setPrice(float price)  
  45.     {  
  46.         this.price = price;  
  47.     }  
  48.   
  49.     @Override  
  50.     public String toString()  
  51.     {  
  52.         return this.id + ":" + this.name + ":" + this.price;  
  53.     }  
  54.   
  55. }  

二.xmlPull解析.


[java]  view plain copy
  1. package cn.skychi.parser;  
  2.   
  3. /** 
  4.  * @package : cn.skychi.parser 
  5.  * @description: 
  6.  * @author : qc 
  7.  * @version : v1.0 
  8.  * @date : 2012-11-29 下午2:49:00 
  9.  */  
  10.   
  11. import java.io.InputStream;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14.   
  15. import org.xmlpull.v1.XmlPullParser;  
  16.   
  17. import cn.skychi.news.Book;  
  18.   
  19. import android.content.Context;  
  20. import android.util.Log;  
  21. import android.util.Xml;  
  22.   
  23. public class PullParseService  
  24. {  
  25.     public static List<Book> getBooks(Context context) throws Exception  
  26.     {  
  27.         List<Book> books = null;  
  28.         Book book = null;  
  29.         InputStream inputStream = context.getResources().getAssets()  
  30.                 .open("book.xml");  
  31.         XmlPullParser parser = Xml.newPullParser();  
  32.         parser.setInput(inputStream, "UTF-8");  
  33.   
  34.         int event = parser.getEventType();// 产生第一个事件  
  35.         while (event != XmlPullParser.END_DOCUMENT)  
  36.         {  
  37.             switch (event)  
  38.             {  
  39.             case XmlPullParser.START_DOCUMENT:// 判断当前事件是否是文档开始事件  
  40.                 books = new ArrayList<Book>();// 初始化books集合  
  41.                 break;  
  42.             case XmlPullParser.START_TAG:// 判断当前事件是否是标签元素开始事件  
  43.                 if ("book".equals(parser.getName()))  
  44.                 {// 判断开始标签元素是否是book  
  45.                     book = new Book();  
  46.                     book.setId(Integer.parseInt(parser.getAttributeValue(0)));// 得到book标签的属性值,并设置book的id  
  47.                 }  
  48.                 if (book != null)  
  49.                 {  
  50.                     if ("name".equals(parser.getName()))  
  51.                     {// 判断开始标签元素是否是name  
  52.                         book.setName(parser.nextText());  
  53.                     }  
  54.                     else if ("price".equals(parser.getName()))  
  55.                     {// 判断开始标签元素是否是price  
  56.                         book.setPrice(Float.parseFloat(parser.nextText()));  
  57.                     }  
  58.                     Log.d("TAG""" + book.toString());  
  59.                 }  
  60.                 break;  
  61.             case XmlPullParser.END_TAG:// 判断当前事件是否是标签元素结束事件  
  62.                 if ("book".equals(parser.getName()))  
  63.                 {// 判断结束标签元素是否是book  
  64.                     books.add(book);// 将book添加到books集合  
  65.                     book = null;  
  66.                 }  
  67.                 break;  
  68.             }  
  69.             event = parser.next();// 进入下一个元素并触发相应事件  
  70.         }// end while  
  71.         return books;  
  72.     }  
  73. }  

三.MainActivity

[java]  view plain copy
  1. package cn.skychi;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import cn.skychi.news.Book;  
  7. import cn.skychi.news.NewsInfo;  
  8. import cn.skychi.parser.NewsPullParser;  
  9. import cn.skychi.parser.PullParseService;  
  10.   
  11. import android.os.Bundle;  
  12. import android.app.Activity;  
  13. import android.app.AlertDialog;  
  14. import android.content.Context;  
  15. import android.content.DialogInterface;  
  16. import android.util.Log;  
  17. import android.view.LayoutInflater;  
  18. import android.view.Menu;  
  19. import android.view.View;  
  20. import android.view.ViewGroup;  
  21. import android.widget.AdapterView;  
  22. import android.widget.ArrayAdapter;  
  23. import android.widget.BaseAdapter;  
  24. import android.widget.ImageButton;  
  25. import android.widget.ImageView;  
  26. import android.widget.ListView;  
  27. import android.widget.RelativeLayout;  
  28. import android.widget.TextView;  
  29. import android.widget.Toast;  
  30.   
  31. public class MainActivity extends Activity  
  32. {  
  33.     private ListView newsListView;  
  34.   
  35.     private ArrayList<NewsInfo> newsInfoList;  
  36.   
  37.     private List<Book> bookList;  
  38.   
  39.     private MyAdapter adapter;  
  40.   
  41.     @Override  
  42.     protected void onCreate(Bundle savedInstanceState)  
  43.     {  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.activity_main);  
  46.         newsListView = (ListView) findViewById(R.id.listView);  
  47.         try  
  48.         {  
  49.             bookList = PullParseService.getBooks(this);  
  50.         }  
  51.         catch(Exception e)  
  52.         {  
  53.             e.printStackTrace();  
  54.         }  
  55.         adapter = new MyAdapter(this, bookList);  
  56.   
  57.         newsListView.setAdapter(adapter);  
  58.   
  59.         newsListView  
  60.                 .setOnItemClickListener(new AdapterView.OnItemClickListener()  
  61.                 {  
  62.                     @Override  
  63.                     public void onItemClick(AdapterView<?> parent, View view,  
  64.                             int position, long id)  
  65.                     {  
  66.                         Toast.makeText(  
  67.                                 MainActivity.this,  
  68.                                 "position = " + position + "\n" + "第" + id  
  69.                                         + "行"3000).show();  
  70.                     }  
  71.                 });  
  72.     }  
  73.   
  74.     private class MyAdapter extends BaseAdapter  
  75.     {  
  76.         private Context context;  
  77.   
  78.         private List<Book> book;  
  79.   
  80.         private LayoutInflater inflater;  
  81.   
  82.         public MyAdapter(Context context, List<Book> book)  
  83.         {  
  84.             super();  
  85.             this.context = context;  
  86.             this.book = book;  
  87.             inflater = LayoutInflater.from(context);  
  88.   
  89.         }  
  90.   
  91.         class ViewHolder  
  92.         {  
  93.             ImageView picture;  
  94.   
  95.             TextView name;  
  96.   
  97.             TextView price;  
  98.   
  99.             ImageButton arrowPicture;  
  100.   
  101.         }  
  102.   
  103.         @Override  
  104.         public Object getItem(int position)  
  105.         {  
  106.   
  107.             return book.get(position);  
  108.         }  
  109.   
  110.         @Override  
  111.         public long getItemId(int position)  
  112.         {  
  113.   
  114.             return position;  
  115.         }  
  116.   
  117.         @Override  
  118.         public View getView(final int position, View convertView,  
  119.                 ViewGroup parent)  
  120.         {  
  121.             ViewHolder holder = new ViewHolder();  
  122.             if (convertView == null)  
  123.             {  
  124.                 convertView = inflater.inflate(R.layout.news_item, null);  
  125.                 holder.picture = (ImageView) convertView  
  126.                         .findViewById(R.id.newsImage);  
  127.                 convertView.setTag(holder);  
  128.                 holder.name = (TextView) convertView  
  129.                         .findViewById(R.id.newsTitle);  
  130.                 holder.price = (TextView) convertView  
  131.                         .findViewById(R.id.newsDate);  
  132.   
  133.                 holder.arrowPicture = (ImageButton) convertView  
  134.                         .findViewById(R.id.menuSend);  
  135.             }  
  136.             else  
  137.             {  
  138.                 holder = (ViewHolder) convertView.getTag();  
  139.             }  
  140.   
  141.             holder.name.setText(book.get(position).getName());  
  142.             holder.price.setText(String.valueOf(book.get(position).getPrice()));  
  143.             holder.arrowPicture  
  144.                     .setImageResource(android.R.drawable.ic_menu_more);  
  145.             holder.picture.setImageResource(R.drawable.ic_launcher);  
  146.   
  147.             holder.arrowPicture.setOnClickListener(new View.OnClickListener()  
  148.             {  
  149.   
  150.                 @Override  
  151.                 public void onClick(View v)  
  152.                 {  
  153.                     showDetailDialog(book, position);  
  154.                 }  
  155.             });  
  156.   
  157.             return convertView;  
  158.         }  
  159.   
  160.         @Override  
  161.         public int getCount()  
  162.         {  
  163.             return book.size();  
  164.         }  
  165.   
  166.     }  
  167.   
  168.     @Override  
  169.     public boolean onCreateOptionsMenu(Menu menu)  
  170.     {  
  171.         // Inflate the menu; this adds items to the action bar if it is  
  172.         // present.  
  173.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  174.         return true;  
  175.     }  
  176.   
  177.     public void showDetailDialog(List<Book> book, int position)  
  178.     {  
  179.         if (book != null && position >= 0)  
  180.         {  
  181.   
  182.             new AlertDialog.Builder(MainActivity.this)  
  183.                     .setTitle("详细信息")  
  184.                     .setMessage(  
  185.                             "Name :" + "\n" + book.get(position).getName()  
  186.                                     + "\n\n" + "Price :" + "\n"  
  187.                                     + +book.get(position).getPrice() + "\n")  
  188.                     .setPositiveButton("ok",  
  189.                             new DialogInterface.OnClickListener()  
  190.                             {  
  191.   
  192.                                 @Override  
  193.                                 public void onClick(DialogInterface dialog,  
  194.                                         int which)  
  195.                                 {  
  196.                                     dialog.cancel();  
  197.                                 }  
  198.                             }).show();  
  199.         }  
  200.     }  
  201.   
  202. }  
[java]  view plain copy
  1. 四.assert目录下.book.xml  
[java]  view plain copy
  1.   
[java]  view plain copy
  1. <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>  
  2. <books>  
  3.     <book id="12">  
  4.         <name>thinking in java</name>  
  5.         <price>85.5</price>  
  6.     </book>  
  7.     <book id="15">  
  8.         <name>Spring in Action</name>  
  9.         <price>39.0</price>  
  10.     </book>  
  11.     <book id="12">  
  12.         <name>thinking in java</name>  
  13.         <price>85.5</price>  
  14.     </book>  
  15.     <book id="15">  
  16.         <name>Spring in Action</name>  
  17.         <price>39.0</price>  
  18.     </book><book id="12">  
  19.         <name>thinking in java</name>  
  20.         <price>85.5</price>  
  21.     </book>  
  22.     <book id="15">  
  23.         <name>Spring in Action</name>  
  24.         <price>39.0</price>  
  25.     </book><book id="12">  
  26.         <name>thinking in java</name>  
  27.         <price>85.5</price>  
  28.     </book>  
  29.     <book id="15">  
  30.         <name>Spring in Action</name>  
  31.         <price>39.0</price>  
  32.     </book><book id="12">  
  33.         <name>thinking in java</name>  
  34.         <price>85.5</price>  
  35.     </book>  
  36.     <book id="15">  
  37.         <name>Spring in Action</name>  
  38.         <price>39.0</price>  
  39.     </book>  
  40.     <book id="15">  
  41.         <name>Spring in Action</name>  
  42.         <price>39.0</price>  
  43.     </book><book id="12">  
  44.         <name>thinking in java</name>  
  45.         <price>85.5</price>  
  46.     </book>  
  47.     <book id="15">  
  48.         <name>Spring in Action</name>  
  49.         <price>39.0</price>  
  50.     </book><book id="12">  
  51.         <name>thinking in java</name>  
  52.         <price>85.5</price>  
  53.     </book>  
  54.     <book id="15">  
  55.         <name>Spring in Action</name>  
  56.         <price>39.0</price>  
  57.     </book>  
  58.     <book id="15">  
  59.         <name>Spring in Action</name>  
  60.         <price>39.0</price>  
  61.     </book><book id="12">  
  62.         <name>thinking in java</name>  
  63.         <price>85.5</price>  
  64.     </book>  
  65.     <book id="15">  
  66.         <name>Spring in Action</name>  
  67.         <price>39.0</price>  
  68.     </book><book id="12">  
  69.         <name>thinking in java</name>  
  70.         <price>85.5</price>  
  71.     </book>  
  72.     <book id="15">  
  73.         <name>Spring in Action</name>  
  74.         <price>39.0</price>  
  75.     </book>  
  76.       
  77.     <book id="15">  
  78.         <name>Spring in Action</name>  
  79.         <price>39.0</price>  
  80.     </book><book id="12">  
  81.         <name>thinking in java</name>  
  82.         <price>85.5</price>  
  83.     </book>  
  84.     <book id="15">  
  85.         <name>Spring in Action</name>  
  86.         <price>39.0</price>  
  87.     </book><book id="12">  
  88.         <name>thinking in java</name>  
  89.         <price>85.5</price>  
  90.     </book>  
  91.     <book id="15">  
  92.         <name>Spring in Action</name>  
  93.         <price>39.0</price>  
  94.     </book>  
  95. </books>  
  96. </pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"></pre><pre name="code" class="java"><pre></pre>五.布局文件:  
  97. <p></p>  
  98. <p>activity_main.xml</p>  
  99. <p></p>  
  100. <pre name="code" class="java"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  101.     xmlns:tools="http://schemas.android.com/tools"  
  102.     android:layout_width="fill_parent"  
  103.     android:layout_height="fill_parent"  
  104.     tools:context=".MainActivity" >  
  105.   
  106.     <ListView  
  107.         android:id="@+id/listView"  
  108.         android:layout_width="fill_parent"  
  109.         android:layout_height="fill_parent"  
  110.          >  
  111.     </ListView>  
  112.   
  113. </RelativeLayout></pre><br>  
  114. news_item.xml  
  115. <p></p>  
  116. <p></p>  
  117. <pre name="code" class="java"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  118.     xmlns:tools="http://schemas.android.com/tools"  
  119.     android:id="@+id/newsInfoLayout"  
  120.     android:layout_width="fill_parent"  
  121.     android:layout_height="wrap_content"  
  122.     android:descendantFocusability="blocksDescendants" >  
  123.   
  124.     <TextView  
  125.         android:id="@+id/newsTitle"  
  126.         android:layout_width="wrap_content"  
  127.         android:layout_height="wrap_content"  
  128.         android:layout_alignTop="@+id/newsImage"  
  129.         android:layout_marginLeft="20dp"  
  130.         android:layout_toLeftOf="@+id/menuSend"  
  131.         android:layout_toRightOf="@+id/newsImage"  
  132.         android:ellipsize="middle"  
  133.         android:singleLine="true"  
  134.         android:text="TextView" />  
  135.   
  136.     <!--  
  137.              <TextView  
  138.             android:id="@+id/newsContent"  
  139.             android:layout_width="wrap_content"  
  140.             android:layout_height="wrap_content"  
  141.             android:layout_above="@+id/newsDate"  
  142.             android:layout_alignLeft="@+id/newsTitle"  
  143.             android:layout_below="@+id/newsTitle"  
  144.             android:layout_toLeftOf="@+id/menuSend"  
  145.             android:visibility="gone" />  
  146.     -->  
  147.   
  148.     <TextView  
  149.         android:id="@+id/newsDate"  
  150.         android:layout_width="wrap_content"  
  151.         android:layout_height="wrap_content"  
  152.         android:layout_alignBottom="@+id/newsImage"  
  153.         android:layout_marginLeft="20dp"  
  154.         android:layout_toLeftOf="@+id/menuSend"  
  155.         android:layout_toRightOf="@+id/newsImage"  
  156.         android:singleLine="true"  
  157.         android:text="TextView" />  
  158.   
  159.     <ImageView  
  160.         android:id="@+id/newsImage"  
  161.         android:layout_width="wrap_content"  
  162.         android:layout_height="wrap_content"  
  163.         android:layout_alignParentLeft="true"  
  164.         android:layout_alignParentTop="true"  
  165.         android:layout_marginLeft="30dp"  
  166.         android:src="@drawable/ic_launcher" />  
  167.   
  168.     <ImageButton  
  169.         android:id="@+id/menuSend"  
  170.         android:layout_width="wrap_content"  
  171.         android:layout_height="wrap_content"  
  172.         android:layout_alignParentRight="true"  
  173.         android:layout_centerVertical="true"  
  174.         android:layout_marginRight="19dp"  
  175.         android:background="#ff00ff"  
  176.         android:clickable="false"  
  177.         android:focusable="false"  
  178.         android:src="@android:drawable/ic_menu_more" />  
  179.   
  180. </RelativeLayout></pre><br>  
  181. 说明:在实现listview item 项以及View控件的监听时要在news_item.xml 中进行配置.即:  
  182. <p>android:descendantFocusability="blocksDescendants" 以及android:clickable="false"android:focusable="false"</p>  
  183. 否则,item 点击事件不会执行.  
  184. <p></p>  
  185. <p><br>  
  186. </p>  
  187. <p><br>  
  188. </p>  
  189. <p><span style="white-space:pre"></span></p>  
  190. <pre></pre>  
  191. <pre></pre>  
  192. <pre></pre>  
  193. </pre>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值