本篇主要功能.
通过xml pull解析得到数据,然后通过自定义的Adapter绑定数据源,ListView绑定适配器,并且实现Item项的点击事件以及子View控件的点击事件.
一.实体类.
Book.java
- package cn.skychi.news;
- /**
- * @package : cn.skychi.news
- * @description: 实体类.
- * @author : qc
- * @version : v1.0
- * @date : 2012-11-29 下午2:45:49
- */
- public class Book
- {
- private int id;
- private String name;
- private float price;
- public int getId()
- {
- return id;
- }
- public void setId(int id)
- {
- this.id = id;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public float getPrice()
- {
- return price;
- }
- public void setPrice(float price)
- {
- this.price = price;
- }
- @Override
- public String toString()
- {
- return this.id + ":" + this.name + ":" + this.price;
- }
- }
二.xmlPull解析.
- package cn.skychi.parser;
- /**
- * @package : cn.skychi.parser
- * @description:
- * @author : qc
- * @version : v1.0
- * @date : 2012-11-29 下午2:49:00
- */
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.xmlpull.v1.XmlPullParser;
- import cn.skychi.news.Book;
- import android.content.Context;
- import android.util.Log;
- import android.util.Xml;
- public class PullParseService
- {
- public static List<Book> getBooks(Context context) throws Exception
- {
- List<Book> books = null;
- Book book = null;
- InputStream inputStream = context.getResources().getAssets()
- .open("book.xml");
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(inputStream, "UTF-8");
- int event = parser.getEventType();// 产生第一个事件
- while (event != XmlPullParser.END_DOCUMENT)
- {
- switch (event)
- {
- case XmlPullParser.START_DOCUMENT:// 判断当前事件是否是文档开始事件
- books = new ArrayList<Book>();// 初始化books集合
- break;
- case XmlPullParser.START_TAG:// 判断当前事件是否是标签元素开始事件
- if ("book".equals(parser.getName()))
- {// 判断开始标签元素是否是book
- book = new Book();
- book.setId(Integer.parseInt(parser.getAttributeValue(0)));// 得到book标签的属性值,并设置book的id
- }
- if (book != null)
- {
- if ("name".equals(parser.getName()))
- {// 判断开始标签元素是否是name
- book.setName(parser.nextText());
- }
- else if ("price".equals(parser.getName()))
- {// 判断开始标签元素是否是price
- book.setPrice(Float.parseFloat(parser.nextText()));
- }
- Log.d("TAG", "" + book.toString());
- }
- break;
- case XmlPullParser.END_TAG:// 判断当前事件是否是标签元素结束事件
- if ("book".equals(parser.getName()))
- {// 判断结束标签元素是否是book
- books.add(book);// 将book添加到books集合
- book = null;
- }
- break;
- }
- event = parser.next();// 进入下一个元素并触发相应事件
- }// end while
- return books;
- }
- }
三.MainActivity
- package cn.skychi;
- import java.util.ArrayList;
- import java.util.List;
- import cn.skychi.news.Book;
- import cn.skychi.news.NewsInfo;
- import cn.skychi.parser.NewsPullParser;
- import cn.skychi.parser.PullParseService;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity
- {
- private ListView newsListView;
- private ArrayList<NewsInfo> newsInfoList;
- private List<Book> bookList;
- private MyAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- newsListView = (ListView) findViewById(R.id.listView);
- try
- {
- bookList = PullParseService.getBooks(this);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- adapter = new MyAdapter(this, bookList);
- newsListView.setAdapter(adapter);
- newsListView
- .setOnItemClickListener(new AdapterView.OnItemClickListener()
- {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id)
- {
- Toast.makeText(
- MainActivity.this,
- "position = " + position + "\n" + "第" + id
- + "行", 3000).show();
- }
- });
- }
- private class MyAdapter extends BaseAdapter
- {
- private Context context;
- private List<Book> book;
- private LayoutInflater inflater;
- public MyAdapter(Context context, List<Book> book)
- {
- super();
- this.context = context;
- this.book = book;
- inflater = LayoutInflater.from(context);
- }
- class ViewHolder
- {
- ImageView picture;
- TextView name;
- TextView price;
- ImageButton arrowPicture;
- }
- @Override
- public Object getItem(int position)
- {
- return book.get(position);
- }
- @Override
- public long getItemId(int position)
- {
- return position;
- }
- @Override
- public View getView(final int position, View convertView,
- ViewGroup parent)
- {
- ViewHolder holder = new ViewHolder();
- if (convertView == null)
- {
- convertView = inflater.inflate(R.layout.news_item, null);
- holder.picture = (ImageView) convertView
- .findViewById(R.id.newsImage);
- convertView.setTag(holder);
- holder.name = (TextView) convertView
- .findViewById(R.id.newsTitle);
- holder.price = (TextView) convertView
- .findViewById(R.id.newsDate);
- holder.arrowPicture = (ImageButton) convertView
- .findViewById(R.id.menuSend);
- }
- else
- {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.name.setText(book.get(position).getName());
- holder.price.setText(String.valueOf(book.get(position).getPrice()));
- holder.arrowPicture
- .setImageResource(android.R.drawable.ic_menu_more);
- holder.picture.setImageResource(R.drawable.ic_launcher);
- holder.arrowPicture.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- showDetailDialog(book, position);
- }
- });
- return convertView;
- }
- @Override
- public int getCount()
- {
- return book.size();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is
- // present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- public void showDetailDialog(List<Book> book, int position)
- {
- if (book != null && position >= 0)
- {
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("详细信息")
- .setMessage(
- "Name :" + "\n" + book.get(position).getName()
- + "\n\n" + "Price :" + "\n"
- + +book.get(position).getPrice() + "\n")
- .setPositiveButton("ok",
- new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog,
- int which)
- {
- dialog.cancel();
- }
- }).show();
- }
- }
- }
- 四.assert目录下.book.xml
- <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
- <books>
- <book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- <book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book><book id="12">
- <name>thinking in java</name>
- <price>85.5</price>
- </book>
- <book id="15">
- <name>Spring in Action</name>
- <price>39.0</price>
- </book>
- </books>
- </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>五.布局文件:
- <p></p>
- <p>activity_main.xml</p>
- <p></p>
- <pre name="code" class="java"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- tools:context=".MainActivity" >
- <ListView
- android:id="@+id/listView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- </ListView>
- </RelativeLayout></pre><br>
- news_item.xml
- <p></p>
- <p></p>
- <pre name="code" class="java"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/newsInfoLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:descendantFocusability="blocksDescendants" >
- <TextView
- android:id="@+id/newsTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignTop="@+id/newsImage"
- android:layout_marginLeft="20dp"
- android:layout_toLeftOf="@+id/menuSend"
- android:layout_toRightOf="@+id/newsImage"
- android:ellipsize="middle"
- android:singleLine="true"
- android:text="TextView" />
- <!--
- <TextView
- android:id="@+id/newsContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@+id/newsDate"
- android:layout_alignLeft="@+id/newsTitle"
- android:layout_below="@+id/newsTitle"
- android:layout_toLeftOf="@+id/menuSend"
- android:visibility="gone" />
- -->
- <TextView
- android:id="@+id/newsDate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBottom="@+id/newsImage"
- android:layout_marginLeft="20dp"
- android:layout_toLeftOf="@+id/menuSend"
- android:layout_toRightOf="@+id/newsImage"
- android:singleLine="true"
- android:text="TextView" />
- <ImageView
- android:id="@+id/newsImage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="30dp"
- android:src="@drawable/ic_launcher" />
- <ImageButton
- android:id="@+id/menuSend"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="19dp"
- android:background="#ff00ff"
- android:clickable="false"
- android:focusable="false"
- android:src="@android:drawable/ic_menu_more" />
- </RelativeLayout></pre><br>
- 说明:在实现listview item 项以及View控件的监听时要在news_item.xml 中进行配置.即:
- <p>android:descendantFocusability="blocksDescendants" 以及android:clickable="false"android:focusable="false"</p>
- 否则,item 点击事件不会执行.
- <p></p>
- <p><br>
- </p>
- <p><br>
- </p>
- <p><span style="white-space:pre"></span></p>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre>