本篇主要功能.

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

一.实体类.

Book.java

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

二.xmlPull解析.

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

三.MainActivity

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

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

  1. 【Android开发】自定义ListView,使用通用适配器,并实现ListView上的每一项和每一项上的按钮等控件同时监听

    ListView在Android开发中是比较常用的系统组件,但是有时候我们除了需要做ListView上每一行的点击监听事件之外,如果每一行上还有其他需要监听的控件例如Button.CheckBox等, ...

  2. Android ListView item里控件的监听

    在进行android开发的时候,我们会经常遇到,需要监听listview的item的控件(如button)的问题,比如点击item的图片跳转详情页,音乐列表里的item都有一个播放按钮,点击这个播放按 ...

  3. listview刷新与内部控件的监听

    困扰我多日的关于Android中的listview问题,今天终于完全解决了,首先讲一下到底遇到的是什么问题:(1)点击listview的item布局中的ImageButton按钮来获取相应的Textv ...

  4. android自定义控件中文乱码,Android笔记--自定义View之组合控件

    Android-自定义View 分享是最好的记忆-- 如需转发请注明出处 [强调]:共同学习 共同进步 不喜勿喷 内容简介 前言 实现 总结 1. 前言 这次更新有2个目的 1. 复用控件,而不是每次 ...

  5. mp8播放器 android 1.4,listview(自定义适配器)与媒体播放器android

    嘿家伙我在listview(自定义适配器)面临问题 . 我已经实现了播放音频(mp3文件)的媒体播放器的listview . 我已经在我的原始文件夹中包含了mp3文件 . 我知道这是一个循环视图 . ...

  6. android listview 滑动条显示_第七十六回:Android中UI控件之RecyclerView基础

    各位看官们,大家好,上一回中咱们说的是Android中UI控件之ListView优化的例子,这一回咱们说的例子是UI控件之RecyclerView.闲话休提,言归正转.让我们一起Talk Androi ...

  7. python gui 显示表格_python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例...

    PyQt5表格控件QTableView简介 在通常情况下,一个应用需要和一批数据进行交互,然后以表格的形式输出这些信息,这时就需要用到QTableView类了,在QTableView中可以使用自定义的 ...

  8. mfc调取摄像头显示并截图_用OpenCV在MFC Dialog中Picture控件上显示摄像头采集实时视频...

    OpenCV之所以能在MFC Dialog的Picture控件上绘图,全靠了CvvImage::DrawToHDC()方法.这就是下文为出现CvvImage和HDC的原因.下面是具体过程,用OpenC ...

  9. android自定义view流布局,Android控件进阶-自定义流式布局和热门标签控件

    一.概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧 类似的自定义换行流式布局控件.下 ...

最新文章

  1. 知乎高赞怎么自学 python,大概要多久?
  2. JSF/SpringMVC/Struts2区别与比较
  3. JDBC , 使用java来控制mysql。JavaWeb开发的分层设计-三层架:DAO层设计,连接池使用,类加载使用,配置...
  4. 使用百度UEditor
  5. 【TensorFlow】conv2d函数参数解释以及padding理解
  6. windows常用指令
  7. Java知识点总结(Java容器-ArrayList)
  8. ios13.5正式版信号怎样?
  9. 还被python收智商税?做大数据的朋友告诉我月薪2w的方法
  10. 相分离相关文章阅读Intrinsically disordered linkers determine the interplay between phase separation and gelat
  11. 海量数据挖掘MMDS week6: 决策树Decision Trees
  12. 【一】如果让我学习TensorFlow,我该怎么学?
  13. SpringBoot Controller Post接口单元测试
  14. Unity 移动端简单手势控制(移动,旋转,缩放)
  15. 北京大学C语言学习第6天
  16. 爬取唯美女生网站上所有小姐姐的照片
  17. wlan:11a/11b/11g/11n/11ac
  18. Netlify前端自动化部署服务
  19. 60帧的丝般顺畅 - QQ飞车手游优化点滴
  20. 怎么搭建一个自己的博客?

热门文章

  1. 【C 语言】内存四区原理 ( 栈内存与堆内存对比示例 | 函数返回的堆内存指针 | 函数返回的栈内存指针 )
  2. 【Android 安全】DEX 加密 ( 阶段总结 | 主应用 | 代理 Application | Java 工具 | 代码示例 ) ★
  3. 【计算理论】自动机 示例 ( 自动机示例 | 自动机表示方式 | 自动机计算流程简介 )
  4. 02 - Unit010:关联映射
  5. Atitit. 破解  拦截 绕过 网站 手机 短信 验证码  方式 v2 attilax 总结
  6. result的type属性
  7. Linux服务器通过rz/sz轻松上传下载文件
  8. 分页控件-ASP.NET(AspNetPager)
  9. USACO SEC.1.2 No.4 Palindromic Squares
  10. Oracle10g 64位 在Windows 2008 Server R2 中的安装 解决方案