android学习之ListView总结

ListView总结

在原来的基础上,增加了点击后的消息响应函数。

源代码下载:http://download.csdn.net/detail/nuptboyzhb/4484920

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据 具体的将被映射的字符串,图片,或者基本组件。

分类:

根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter

其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。

我们从最简单的ListView开始

MyListView.java

package org.lee.android;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;

publicclass MyListView extends Activity implementsOnItemClickListener{

   private ListView listView;

   //private List<String> data = new ArrayList<String>();

   @Override

   publicvoid onCreate(Bundle savedInstanceState){

      super.onCreate(savedInstanceState);

      listView = new ListView(this);

      List<String>list=getData();

      ArrayAdapter<String>adapter=newArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);

      listView.setAdapter(adapter);

      setContentView(listView);

      listView.setOnItemClickListener(this);//绑定监听接口  

   }

  

  

  

   private List<String> getData(){

     

      List<String> data = newArrayList<String>();

      data.add("测试数据1");

      data.add("测试数据2");

      data.add("测试数据3");

      data.add("测试数据4");

      return data;

   }

   /*实现OnItemClickListener接口*/

   @Override

   publicvoid onItemClick(AdapterView<?> parent, View v, int position, long id){

      Log.d("Click","you have click!");

      switch(position){

      case 0:

         Toast.makeText(MyListView.this,

                "你点击了第一个!",

                Toast.LENGTH_SHORT).show();

         break;

      case 1:

         Toast.makeText(MyListView.this,

                "你点击了第二个!",

                Toast.LENGTH_SHORT).show();

         break;

      case 2:

         Toast.makeText(MyListView.this,

                "你点击了第三个!",

                Toast.LENGTH_SHORT).show();

         break;

      case 3:

         Toast.makeText(MyListView.this,

                "你点击了第四个!",

                Toast.LENGTH_SHORT).show();

         break;

      }

   }

}


上面代码使用了ArrayAdapter(Contextcontext,int textViewResourceId,List<T>objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的 构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布 局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时 用setAdapter()完成适配的最后工作。

SimpleAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方便显示而已。

下面的程序是实现一个带有图片的类表。

首先需要定义好一个用来显示每一个列内容的xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="horizontal" android:layout_width="fill_parent"

   android:layout_height="fill_parent">

   <ImageViewandroid:id="@+id/img"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:layout_margin="5px"/>

   <LinearLayout android:orientation="vertical"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content">

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

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textColor="#FFFFFFFF"

         android:textSize="22px" />

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

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textColor="#FFFFFFFF"

         android:textSize="13px" />

   </LinearLayout>

</LinearLayout>


下面是实现代码java:

package org.lee.android;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import android.app.ListActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

importandroid.widget.AdapterView.OnItemClickListener;

/**

 * @author allin

 *

 */

publicclass MyListView3 extends ListActivity implementsOnItemClickListener{

 

 

   // private List<String> data = new ArrayList<String>();

   @Override

   publicvoid onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

 

      SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,

            new String[]{"title","info","img"},

            newint[]{R.id.title,R.id.info,R.id.img});

      setListAdapter(adapter);

      ListViewlistView=getListView();

      listView.setOnItemClickListener(this);

   }

 

   private List<Map<String,Object>> getData() {

      List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();

 

      Map<String, Object> map =new HashMap<String, Object>();

      map.put("title", "G1");

      map.put("info", "google 1");

      map.put("img",R.drawable.i1);

      list.add(map);

 

      map = new HashMap<String,Object>();

      map.put("title", "G2");

      map.put("info", "google 2");

      map.put("img",R.drawable.i2);

      list.add(map);

 

      map = new HashMap<String,Object>();

      map.put("title", "G3");

      map.put("info", "google 3");

      map.put("img",R.drawable.i3);

      list.add(map);

     

      return list;

   }

   /*实现OnItemClickListener接口*/

   @Override

   publicvoid onItemClick(AdapterView<?> parent,View v, int position, long id){

      Log.d("Click","you have click!");

      switch(position){

      case 0:

         Toast.makeText(MyListView3.this,

                "你点击了第一个!",

                Toast.LENGTH_SHORT).show();

         break;

      case 1:

         Toast.makeText(MyListView3.this,

                "你点击了第二个!",

                Toast.LENGTH_SHORT).show();

         break;

      case 2:

         Toast.makeText(MyListView3.this,

                "你点击了第三个!",

                Toast.LENGTH_SHORT).show();

         break;

      }

   }

}


使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键 值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

参考博客:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值