怎么获取listview获取item对象中每一行的price 进行累加

从网络读取数据并动态的显示在ListView中
这两天写了个小程序,使用了从网络读取xml数据,并显示在ListView中。
这里面有几个关键点:
从网络读取数据
SAX解析xml
异步填充ListView
先看下截图:
非常简单的界面哈
为了方便,我再自己的服务器上,放了一个xml文件,其内容主要是:
&?xml version=&1.0&?&&
&products&&
&&& &product&&
&&&&&&& &price&100&/price&&
&&&&&&& &name&android dev&/name&&
&&&&&&& &image src=&image/android1.png&/&&
&&& &/product&&
&&& &product&&
&&&&&&& &price&100&/price&&
&&&&&&& &name&androiddev2&/name&&
&&&&&&& &image src=&image/android1.png&/&&
&&& &/product&&
&!-- 接着重复下去.... --&&
&/products&&
该程序,首先用一个AsyncTask新启一个线程,来下载和解析xml;和主线程通过Handler对象来通讯。
下载XML文件
通过HTTPGet来下载xml。这时apache提供的包,需要首先包含如下包:
import org.apache.http.HttpE&
import org.apache.http.HttpR&
import org.apache.http.HttpS&
import org.apache.http.client.HttpC&
import org.apache.http.client.methods.HttpG&
import org.apache.http.impl.client.DefaultHttpC&
代码如下:(使用Get方法)
protected String doInBackground(String... params) {&
&&& HttpGet httpRequest = new HttpGet(params[0]); //从url 创建一个HttpGet对象&
&&& HttpClient httpclient = new DefaultHttpClient();&
&&& //mShowHtml.setText(&&);&
&&& try {&
&&&&&&& HttpResponse httpResponse = httpclient.execute(httpRequest);&
&&&&& &&&&
&&&&&&& if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){//获取http的返回值代码&
&&&&&&&&&&& HttpEntity entitiy = httpResponse.getEntity();&
&&&&&&&&&&&&&
&&&&&&&&&&& InputStream in = entitiy.getContent();//获得内容&
&&&&&&&&&&& //解析xml,下面详述&
&&&&&&&&&&& InputSource source = new InputSource(in);&
&&&&&&&&&&&&&
&&&&&&&&&&& SAXParserFactory sax = SAXParserFactory.newInstance();&
&&&&&&&&&&& XMLReader xmlReader = sax.newSAXParser().getXMLReader();&
&&&&&&&&&&&&&
&& &&&&&&&&&xmlReader.setContentHandler(new ProductHandler());&
&&&&&&&&&&&&&
&&&&&&&&&&& xmlReader.parse(source);&
&&&&&&&&&&&&&
&&&&&&& }&
&&&&&&& else {&
&&&&&&&&&&& //return &请求失败!&;&
&&&&&&&&&&& //mShowHtml.setText(&请求失败&);&
&&&&&&&&&&& //Message mymsg = mMainHandler.obtainMessage();&
&&&&&&&&&&& //mymsg.obj = &请求失败&;&
&&&&&&&&&&& //mMainHandler.sendMessage(mymsg);&
&&&&&&& }&
&&& }catch(IOException e){&
&&&&&&& e.printStackTrace();&
&&& }catch(SAXException e) {&
&&&&&&& e.printStackTrace();&
&&& }catch(ParserConfigurationException e) {&
&&&&&&& e.printStackTrace();&
使用SAX的解析方法,先包含必须得包
import org.xml.sax.InputS&
import org.xml.sax.SAXE&
import org.xml.sax.XMLR&
import org.xml.sax.helpers.DefaultH&
解析的代码,正如上面所示:
&&&&&&&&&&&&&&&&
//InputSource是解析源,可以通过一个InputStream创建&
urce source = new InputSource(in);&&&&&&&&&&&&&&&&&&&
SAXParserFactory sax = SAXParserFactory.newInstance();&
der xmlReader = sax.newSAXParser().getXMLReader();&&&&&&&&&&&&&&&&&&&&&&&&&&&&
xmlReader.setContentHandler(new ProductHandler());//ProductHandler是解析的句柄&&&&&&&&&&&&&&&&&&&&&&&&&
der.parse(source);&
SAX主要使用ContentHandler接口来传递解析好得数据,不过,更经常使用的是DefaultHandler
class ProductHandler extends DefaultHandler { //从DefaultHandler继承即可&
&&& private ProductInfo curP&
&&& private S&
&&& //解析到一个标签时调用&
&&& public void startElement(String uri, String localName, String name, org.xml.sax.Attributes attributes) throws SAXException {&
&&&&&&& if(localName.equals(&product&)) {//遇到product标签,进行解析&
&&&&&&&&&&& curProduct = new ProductInfo();&
&&&&&&& }&
&&&&&&& else if(localName.equals(&image&)) {&
&&&&&&&&&&& //set name&
&&&&&&&&&&& curProduct.image = attributes.getValue(&src&);//提取image src属性&
&&&&&&& }&
&&&&&&& super.startElement(uri, localName, name, attributes);&
&&& public void endElement(String uri, String localName, String name) throws SAXException{&
&&&&&&& if(localName.equals(&product&)) {//当解析到一个标签结束时,发送一个消息,把Product类作为参数传递&
&&&&&&&&&&& //send event&
&&&&&&&&&&& //get main handler&
&&&&&&&&&&& Message msg = mMainHandler.obtainMessage();&
&&&&&&&&&&& msg.obj = curP&
&&&&&&&&&&& mMainHandler.sendMessage(msg);&
&&&&&&&&&&& //Log.i(&Product:&, curProduct.toString());&
&&&&&&& }&
&&&&&&& else if(localName.equals(&name&)) {&
&&&&&&&&&&& //set name&
&&&&&&&&&&& curProduct.name =//保存名字&
&&&&&&& }&
&&&&&&& else if(localName.equals(&price&)) {&
&&&&&&&&&&& curProduct.price = Float.parseFloat(content);&span style=&background-color: rgb(255, 255, 255); &&//保存价格&/span&&
&&&&&&& }&
&&&&&&& super.endElement(uri, localName, name);&
&&& //这里获取具体的内容,是标签下保存的文本&
&&& public void characters (char[] ch, int start, int length)& throws SAXException&
&&&&&&& content = new String(ch, start, length);&
&&&&&&& //Log.i(&Parser:& ,content);&
&&&&&&& super.characters(ch, start, length);&
ProductInfo类的定义非常简单
class ProductInfo {&
&&& public S&
&&& public S&
&&& public String toString() {&
&&&&&&& return &\nName:& + name +&\nPrice :& + price + &\nImage:& +&
在AsyncTask中执行以上代码
public class GetHttpTask extends AsyncTask&String, Integer, String& {&
&&& public GetHttpTask() {&
&&& protected void onPreExecute() { //在进入线程之前执行。该函数在调用者线程内执行&
&&& protected String doInBackground(String... params) {//线程的执行主体&
&&&&&&& HttpGet httpRequest = new HttpGet(params[0]);&
.................. //主要执行下载和解析的嗲吗&
&&& protected void onPostExecute(String result) {//完成后调用&
在主线程中,调用GetHttpTask的execute方法就可以执行
&&&&& btn.setOnClickListener(new View.OnClickListener() {//在onCreate函数中调用&
&&& @Override&
&&& public void onClick(View v) {&
&&&&&&& // TODO Auto-generated method stub&
&&&&&&& httpGet();&
httpGet方法:
void httpGet() {&
&&& GetHttpTask task = new GetHttpTask();&
&&& task.execute(&http://192.168.1.111:8080/nfcdemo/products.xml&);&
异步传递消息
上面的例子中,在endElement函数中,发送了消息,下面是接收消息
在主Activity中,声明了一个Handler mHandler对象,并在onCreate时,这样做
mMainHandler = new Handler() {&
&&& public& void handleMessage(Message msg) {&
&&&&&&& //mShowHtml.append((String)msg.obj);&
&&&&&&& if(msg.obj != null) {&
&&&&&&&&&&& ProductInfo prod = (ProductInfo)msg.&
&&&&&&&&&&& Log.i(&Prodcut:&, prod.toString());&
&&&&&&&&&&& //mShowHtml.append(prod.toString());&
&&&&&&&&&&& HashMap&String, Object& map = new HashMap&String, Object&();&
&&&&&&&&&&& map.put(&name&, prod.name);&
&&&&&&&&&&& map.put(&price&, &RMB& + prod.price);&
&&&&&&&&&&& mlist.add(map); //mlist保存了列表的具体数据&
&&&&&&&&&&& mProdList.notifyDataSetChanged();//mProdList是一个ListView对象,该函数引起ListView重读数据&
&&&&&&& }&
这个过程的要点基本如上,贴上所有代码吧!关于ListView的用法,可以参考http://blog.csdn.net/hellogv/article/details/4542668
package com.test.&
import java.io.IOE&
import java.io.InputS&
import java.util.ArrayL&
import java.util.HashM&
import javax.xml.parsers.ParserConfigurationE&
import javax.xml.parsers.SAXParserF&
import org.apache.http.HttpE&
import org.apache.http.HttpR&
import org.apache.http.HttpS&
import org.apache.http.client.HttpC&
import org.apache.http.client.methods.HttpG&
import org.apache.http.impl.client.DefaultHttpC&
import org.xml.sax.InputS&
import org.xml.sax.SAXE&
import org.xml.sax.XMLR&
import org.xml.sax.helpers.DefaultH&
import android.app.A&
import android.os.AsyncT&
import android.os.B&
import android.os.H&
import android.os.M&
import android.util.L&
import android.view.V&
import android.widget.B&
import android.widget.EditT&
import android.widget.ListV&
import android.widget.SimpleA&
public class HtmltestActivity extends Activity {&
&&& EditText mUrlT&
&&& //EditText mShowH&
&&& ListView mP&
&&& Handler mMainH&
&&& SimpleAdapter mProdL&
&&& ArrayList&HashMap&String,Object&&&
&&& /** Called when the activity is first created. */&
&&& @Override&
&&& public void onCreate(Bundle savedInstanceState) {&
&&&&&&& super.onCreate(savedInstanceState);&
&&&&&&& setContentView(R.layout.main);&
&&&&&&& mUrlText = (EditText)findViewById(R.id.eturl);&
&&&&&&& //mShowHtml = (EditText)findViewById(R.id.etshowhtml);&
&&&&&&& mProducts = (ListView)findViewById(R.id.productList);&
&&&&&&& Button btn = (Button)findViewById(R.id.btngo);&
&&&&&&& mlist = new ArrayList&HashMap&String, Object&&();&
&&&&&&& mProdList = new SimpleAdapter(this,&
&&&&&&&&&&&&&&& mlist,&
&&&&&&&&&&&&&&& R.layout.listitem,&
&&&&&&&&&&&&&&& new String[]{&name&, &price&},&
&&&&&&&&&&&&&&& new int[]{R.id.prd_title, R.id.prd_price}&
&&&&&&&& &&&&&&&);&
&&&&&&& mProducts.setAdapter(mProdList);&
&&&&&&& btn.setOnClickListener(new View.OnClickListener() {&
&&&&&&&&&&&&&
&&&&&&&&&&& @Override&
&&&&&&&&&&& public void onClick(View v) {&
&&&&&&&&&&&&&&& // TODO Auto-generated method stub&
&&&&&&&&&&&&&&& httpGet();&
&&&&&&&&&&& }&
&&&&&&& });&
&&&&&&& mMainHandler = new Handler() {&
&&&&&&&&&&& public& void handleMessage(Message msg) {&
&&&&&&&&&&&&&&& //mShowHtml.append((String)msg.obj);&
&&&&&&&&&&&&&&& if(msg.obj != null) {&
&&&&&&&&&&&&&&&&&&& ProductInfo prod = (ProductInfo)msg.&
&&&&&&&&&&&&&&&&&&& Log.i(&Prodcut:&, prod.toString());&
&&&&&&&&&&&&&&&&&&& //mShowHtml.append(prod.toString());&
&&&&&&&&&&&&&&&& &&&HashMap&String, Object& map = new HashMap&String, Object&();&
&&&&&&&&&&&&&&&&&&& map.put(&name&, prod.name);&
&&&&&&&&&&&&&&&&&&& map.put(&price&, &RMB& + prod.price);&
&&&&&&&&&&&&&&&&&&& mlist.add(map);&
&&&&&&&&&&&&&&&&&&& mProdList.notifyDataSetChanged();&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&& }&
&&&&&&& };&
&&& void httpGet() {&
&&&&&&& GetHttpTask task = new GetHttpTask();&
&&&&&&& task.execute(&http://192.168.1.111:8080/nfcdemo/products.xml&);&
&&& public class GetHttpTask extends AsyncTask&String, Integer, String& {&
&&&&&&& public GetHttpTask() {&
&&&&&&& }&
&&&&&&& protected void onPreExecute() {&
&&&&&&&&&&&&&
&&&&&&& }&
&&&&&&& protected String doInBackground(String... params) {&
&&&&&&&&&&& HttpGet httpRequest = new HttpGet(params[0]);&
&&&&&&&&&&& HttpClient httpclient = new DefaultHttpClient();&
&&&&&&&&&&&&&
&&&&&&&&&&& //mShowHtml.setText(&&);&
&&&&&&&&&&& try {&
&&&&&&&&&&&&&&& HttpResponse httpResponse = httpclient.execute(httpRequest);&
&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&& if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){&
&&&&&&&&&&&&&&&&&&& HttpEntity entitiy = httpResponse.getEntity();&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&& &&&&&&&InputStream in = entitiy.getContent();&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& InputSource source = new InputSource(in);&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& SAXParserFactory sax = SAXParserFactory.newInstance();&
&&&&&&&&&&&&&&&&&&& XMLReader xmlReader = sax.newSAXParser().getXMLReader();&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& xmlReader.setContentHandler(new ProductHandler());&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&& xmlReader.parse(source);&
&&&&&&&&&& &&&&&&&&&&&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&&& else {&
&&&&&&&&&&&&&&&&&&& //return &请求失败!&;&
&&&&&&&&&&&&&&&&&&& //mShowHtml.setText(&请求失败&);&
&&&&&&&&&&&&&&&&&&& //Message mymsg = mMainHandler.obtainMessage();&
&&&&&&&&&&&&&&&&&&& //mymsg.obj = &请求失败&;&
&&&&&&&&&&&&&&&&&&& //mMainHandler.sendMessage(mymsg);&
&&&&&&&&&&&&&&& }&
&&&&&&&&&&&&&
&&&&&&&&&&& }catch(IOException e){&
&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&& }catch(SAXException e) {&
&&&&&&&&&&&&&&& e.printStackTrace();&
&&& &&&&&&&&}catch(ParserConfigurationException e) {&
&&&&&&&&&&&&&&& e.printStackTrace();&
&&&&&&&&&&& }&
&&&&&&&&&&&&
&&&&&&& }&
&&&&&&& protected void onPostExecute(String result) {&
&&&&&&&&&&& //mShowHtml.setText(result);&
&&&&&&& }&
&&& class ProductInfo {&
&&&&&&& public S&
&&&&&&& public S&
&&&&&&& public String toString() {&
&&&&&&&&&&& return &\nName:& + name +&\nPrice :& + price + &\nImage:& +&
&&&&&&& }&
&&& class ProductHandler extends DefaultHandler {&
&&&&&&& private ProductInfo curP&
&&&&&&& private S&
&&&&&&& public void startElement(String uri, String localName, String name, org.xml.sax.Attributes attributes) throws SAXException {&
&&&&&&&&&&& if(localName.equals(&product&)) {&
&&&&&&&&&&&&&&& curProduct = new ProductInfo();&
&&&&&&&&&&& }&
&&&&&&&&&&& else if(localName.equals(&image&)) {&
&&&&&&&&&&&&&&& //set name&
&&&&&&&&&& &&&&&curProduct.image = attributes.getValue(&src&);&
&&&&&&&&&&& }&
&&&&&&&&&&& super.startElement(uri, localName, name, attributes);&
&&&&&&& }&
&&&&&&& public void endElement(String uri, String localName, String name) throws SAXException{ &
&&&&&&&&&&& if(localName.equals(&product&)) {&
&&&&&&&&&&&&&&& //send event&
&&&&&&&&&&&&&&& //get main handler&
&&&&&&&&&&&&&&& Message msg = mMainHandler.obtainMessage();&
&&&&&&&&&&&&&&& msg.obj = curP&
&&&&&&&&&&&&&&& mMainHandler.sendMessage(msg);&
&&&&&&&&&&&&&&& //Log.i(&Product:&, curProduct.toString());&
&&&&&&&&&&& }&
&&&&&&&&&&& else if(localName.equals(&name&)) {&
&&&&&&&&&&&&&&& //set name&
&&&&&&&&&&&&&&& curProduct.name =&
&&&& &&&&&&&}&
&&&&&&&&&&& else if(localName.equals(&price&)) {&
&&&&&&&&&&&&&&& curProduct.price = Float.parseFloat(content);&
&&&&&&&&&&& }&
&&&&&&&&&&& super.endElement(uri, localName, name);&
&&&&&&& }&
&&&&&&& public void characters (char[] ch, int start, int length)& throws SAXException&
&&&&&&& {&
&&&&&&&&&&& content = new String(ch, start, length);&
&&&&&&&&&&& //Log.i(&Parser:& ,content);&
&&&&&&&&&&& super.characters(ch, start, length);&
&&&&&&& }&
&?xml version=&1.0& encoding=&utf-8&?&&
&LinearLayout xmlns:android=&/apk/res/android&&
&&& android:layout_width=&fill_parent&&
&&& android:layout_height=&fill_parent&&
&&& android:orientation=&vertical& &&
&&& &LinearLayout&
&&&&&&& android:id=&@+id/linearLayout1&&
&&&&&&& android:layout_width=&match_parent&&
&&&&&&& android:layout_height=&wrap_content& &&
&&&&&&& &EditText&
&&&&&&&&&&& android:id=&@+id/eturl&&
&&&&&&&&&&& android:layout_width=&match_parent&&
&&&&&&&&&&& android:layout_height=&wrap_content&&
&&&&&&&&&&& android:layout_weight=&1& &&
&&&&&&&&&&& &requestFocus /&&
&&&&&&& &/EditText&&
&&&&&&& &Button&
&&&&&&&&&&& android:id=&@+id/btngo&&
&&&&&&&&&&& android:layout_width=&100dp&&
&&&&&&&&&&& android:layout_height=&wrap_content&&
&&&&&&&&&&& android:layout_weight=&1&&
&&&&&&&&&&& android:text=&Go!& /&&
&&& &/LinearLayout&&
&&& &ListView&
&&&&&&& android:id=&@+id/productList&&
&&&&&&& android:layout_width=&match_parent&&
&&&&&&& android:layout_height=&match_parent& &&
&&& &/ListView&&
&/LinearLayout&&
listitem.xml
&?xml version=&1.0& encoding=&utf-8&?&&
&RelativeLayout xmlns:android=&/apk/res/android&&
&&& android:layout_width=&match_parent&&
&&& android:layout_height=&match_parent& &&
&&& &ImageView&
&&&&&&& android:id=&@+id/product_icon&&
&&&&&&& android:layout_width=&wrap_content&&
&&&&&&& android:layout_height=&wrap_content&&
&&&&&&& android:layout_alignParentLeft=&true&&
&&&&&&& android:layout_alignParentTop=&true&&
&&&&&&& android:src=&@drawable/ic_launcher& /&&
&&& &TextView&
&&&&&&& android:id=&@+id/prd_title&&
&&&&&&& android:layout_width=&wrap_content&&
&&&&&&& android:layout_height=&wrap_content&&
&&&&&&& android:layout_alignParentTop=&true&&
&&&&&&& android:layout_toRightOf=&@+id/product_icon&&
&&&&&&& android:text=&TextView& /&&
&&& &TextView&
&&&&&&& android:id=&@+id/prd_price&&
&&&&&&& android:layout_width=&wrap_content&&
&&&&&&& android:layout_height=&wrap_content&&
&&&&&&& android:layout_alignParentRight=&true&&
&&&&&&& android:layout_alignParentTop=&true&&
&&&&&&& android:text=&TextView& /&&
&/RelativeLayout&& &
doon的专栏Android(21)
&&&&&&&&& Android中显示数据有多种控件,这节我们来认识下ListView,ListView是Android中最常用的数据显示控件,可以显示简单数据源,也可以显示复杂数据源,我们在Android系统中常看到的列表项,基本都是ListView的功劳。ListView中显示数据,肯定要绑定数据源。数据源的绑定是通过Adapter来完成的,Android中有两种常用的适配器,ArrayAdapter(数组适配器)&
SimpleAdapter(简单适配器),适配器的作用就是把复杂的数据源显示到istview界面视图上,是数据源和界面之间的桥梁。
&&&&& 这一节我们来认识下这两个适配器,数组适配器用来显示简单的数据,简单适配器主要用来显示复杂的数据。
&&&&&&&& 1.&数组适配器ArrayAdapter
&&&&&&&&&&&&&&&&&&&&&&&&&&& 数组适配器显示的数据比较单一,我们看下面的例子
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:id=&@+id/listView1&
android:layout_width=&match_parent&
android:layout_height=&wrap_content& &
&/ListView&
&/LinearLayout&
package com.example.
import android.app.A
import android.os.B
import android.widget.ArrayA
import android.widget.ListV
public class FirstListView extends Activity {
private ListV
private ArrayAdapter&String&
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstlistview);
lv=(ListView)findViewById(R.id.listView1);//获取Listview对象
//listview数据源
String[] arr={&游响云停工作室1&,&游响云停工作室2&,&游响云停工作室ArrayAdapter演示&,&游响云停交流群&};
//初始化适配器,参数1是上下文对象,参数2是Listview中每个列表的布局文件,参数3是数据源
adapter=new ArrayAdapter&String&(this,android.R.layout.simple_list_item_1,arr);
lv.setAdapter(adapter);//绑定数据
&&&&&&&&&&&&&&&&&&& 我们来分析下数组适配器的参数
&&&&&&&&&&&&&&&&&adapter=new ArrayAdapter&String&(this,android.R.layout.simple_list_item_1,arr);
&&&&&&&&&&&&&&&&&&&&&& 第一个参数为当前上下文对象
&&&&&&&&&&&&&&&&&&&&& 第二个参数为布局文件,我们例子中使用的系统自带的布局文件
&&&&&&&&&&&&&&&&&&&&& 第三个参数是数据源
&&&&&&&& 2. 简单适配器SimpleAdapter
&&&&&&&&&&&&&&&&&&&&&&&& 简单适配器用来显示复杂的数据,我们看下这个示例
&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&& 首先创建一个LISTVIEW中每项的布局文件listitem.xml
&&&&&&&&&&&&&&&&&&&&&
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&100dp&
android:orientation=&vertical&
android:gravity=&top&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal&
android:layout_weight=&7&
android:gravity=&center&
&ImageView
android:id=&@+id/pic&
android:layout_width=&80dp&
android:layout_height=&80dp&
android:src=&@drawable/ic_launcher& /&
&/LinearLayout&
&LinearLayout
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:orientation=&vertical&
android:layout_weight=&3&
android:id=&@+id/tvname&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginTop=&5dp&
android:text=&商品名称:& /&
android:id=&@+id/tvprice&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginTop=&5dp&
android:text=&商品价格:& /&
android:id=&@+id/tvcolor&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_marginTop=&5dp&
android:layout_marginBottom=&5dp&
android:text=&商品颜色& /&
&/LinearLayout&
&/LinearLayout&
&LinearLayout android:layout_width=&fill_parent&
android:layout_height=&2dp& android:background=&#F0F0F0&&
&/LinearLayout&
&/LinearLayout&
&&&&&&&&&&&&&&& 主页面布局文件
&&&&&&&&&&&&&&&
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
android:orientation=&vertical& &
android:id=&@+id/listView2&
android:layout_width=&match_parent&
android:layout_height=&wrap_content& &
&/ListView&
&/LinearLayout&
package com.example.
import java.util.ArrayL
import java.util.HashM
import java.util.L
import java.util.M
import android.app.A
import android.os.B
import android.widget.ArrayA
import android.widget.ListV
import android.widget.SimpleA
public class SimpleListView
extends Activity {
private ListV
private SimpleA//定义适配器
private List&Map&String,Object&& mapL//定义数据源
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simplelistview);
lv=(ListView)findViewById(R.id.listView2);
参数1是上下文对象
参数2是数据源
参数3是布局文件
参数4是键名
参数5是绑定布局文件中视图ID
mapList=new ArrayList&Map&String,Object&&();
for(int i=0;i&10;i++)
Map&String,Object& map=new HashMap&String,Object&();
map.put(&pic&,R.raw.pad);
map.put(&name&,&商品名称:Ipad Air&);
map.put(&price&,&商品价格:$&+i);
map.put(&color&,&商品颜色:白色&);
mapList.add(map);
adp=new SimpleAdapter(this, mapList,R.layout.listitem, new String[]{&pic&,&name&,&price&,&color&}, new int[]{R.id.pic,R.id.tvname,R.id.tvprice,R.id.tvcolor});
lv.setAdapter(adp);
&&&&&&&&&&&&&&&&&&&&& 我们来分析下简单适配器的参数
&&&&&&&&&&&&&&&& adp=new SimpleAdapter(this, mapList,R.layout.listitem, new String[]{&pic&,&name&,&price&,&color&}, new int[]{R.id.pic,R.id.tvname,R.id.tvprice,R.id.tvcolor});
&&&&&&&&&&&&&&&&& 第一个参数是上下文对象
&&&&&&&&&&&&&&&& 第二个参数是数据源,数据源的类型是集合
&&&&&&&&&&&&&&&&& 第三个参数是ListView中每一项的布局文件
&&&&&&&&&&&&&&&& 第四个参数是数组,数组里面每一项对应数据源中MAP的键名称
&&&&&&&&&&&&&&&& 第五个参数就是ListVIew中对应的子布局文件中对应的控件的ID
&&&&&&&&&&&&&&&&&&& 下载:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:606975次
积分:8083
积分:8083
排名:第2481名
原创:214篇
转载:33篇
评论:99条
(1)(6)(2)(7)(8)(4)(8)(24)(11)(12)(1)(4)(11)(9)(4)(12)(1)(1)(8)(3)(4)(2)(2)(1)(4)(11)(6)(6)(6)(5)(2)(3)(2)(1)(4)(3)(5)(1)(10)(2)(1)(5)(5)(4)(2)(2)(6)(6)

我要回帖

更多关于 listview获取item对象 的文章

 

随机推荐