ListView及Adapter的使用

一、使用ArrayAdapter

其中ArrayAdapter的构造函数有如下几个,其中resource是指每个列表项的布局文件,objects是指列表项的数据源,此处通常指一个数组

ArrayAdapter(Context context, int resource)

ArrayAdapter(Context context, int resource, int textViewResourceId)

ArrayAdapter(Context context, int resource, T[] objects)    

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

)">ArrayAdapter(Context context, int resource, List<T> objects)

)">ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Public Constructors


public ArrayAdapter (Context context, int resource)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

public ArrayAdapter (Context context, int resource, int textViewResourceId)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

public ArrayAdapter (Context context, int resource, T[] objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, List<T> objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

objects
The objects to represent in the ListView.

 

1.MainActivity.java

package org.crazyit.ui;

import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.ListView;

public class MainActivity extends Activity{   @Override   protected void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      ListView list1 = (ListView) findViewById(R.id.list1);      // 定义一个数组      String[] arr1 = { "孙悟空", "猪八戒", "牛魔王" };      // 将数组包装为ArrayAdapter      ArrayAdapter<String> adapter1 = new ArrayAdapter<String>            (this, R.layout.array_item, arr1);      // 为ListView设置Adapter      list1.setAdapter(adapter1);      ListView list2 = (ListView) findViewById(R.id.list2);      // 定义一个数组      String[] arr2 = { "Java", "Hibernate", "Spring" , "Android" };      // 将数组包装为ArrayAdapter      ArrayAdapter<String> adapter2 = new ArrayAdapter<String>            (this, R.layout.checked_item, arr2);      // 为ListView设置Adapter      list2.setAdapter(adapter2);   }}
2.main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="match_parent"   android:layout_height="match_parent">   <!-- 设置使用红色的分隔条 -->   <ListView      android:id="@+id/list1"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:divider="#f00"      android:dividerHeight="2px"      android:headerDividersEnabled="false"/>   <!-- 设置使用绿色的分隔条 -->   <ListView      android:id="@+id/list2"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:divider="#0f0"      android:dividerHeight="2px"      android:headerDividersEnabled="false"/></LinearLayout>
3.array_item.xml
<?xml version="1.0" encoding="utf-8"?><TextView   xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/TextView"   android:layout_width="match_parent"   android:layout_height="wrap_content"      android:textSize="24dp"   android:padding="10px"   android:shadowColor="#f0f"   android:shadowDx="4"   android:shadowDy="4"   android:shadowRadius="2"/>
4.checked_item.xml
<?xml version="1.0" encoding="utf-8"?><CheckedTextView   xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/TextView"   android:layout_width="match_parent"   android:layout_height="wrap_content"      android:textSize="24dp"   android:checkMark="@drawable/ok"   android:shadowColor="#f0f"   android:shadowDx="4"   android:shadowDy="4"   android:shadowRadius="2"/>

二、使用SimpleAdaper

SimpleAdapter的构造函数如下,其中resource为该列表项的布局资源,to为该布局资源中每一列的ID;data为该列表项的数据源,其中data的每一列表项,包含多列Map<String,?>,每一个Map为一列,Map中的每一个Key值均要在from中指出。


public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Added in API level 1

Constructor

Parameters

context

The context where the View associated with this SimpleAdapter is running

data

A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

resource

Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

from

A list of column names that will be added to the Map associated with each item.

to

The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

1.MainActivity.java

package org.crazyit.ui;

import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import static android.widget.AdapterView.OnItemClickListener;import static android.widget.AdapterView.OnItemSelectedListener;import android.widget.ListView;import android.widget.SimpleAdapter;

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;

public class MainActivity extends Activity{   private String[] names = new String[]         { "虎头", "弄玉", "李清照", "李白"};   private String[] descs = new String[]         { "可爱的小孩", "一个擅长音乐的女孩"               , "一个擅长文学的女性", "浪漫主义诗人"};   private int[] imageIds = new int[]         { R.drawable.tiger , R.drawable.nongyu               , R.drawable.qingzhao , R.drawable.libai};   @Override   public void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      // 创建一个List集合,List集合的元素是Map      List<Map<String, Object>> listItems =            new ArrayList<Map<String, Object>>();      for (int i = 0; i < names.length; i++)      {         Map<String, Object> listItem = new HashMap<String, Object>();         listItem.put("header", imageIds[i]);         listItem.put("personName", names[i]);         listItem.put("desc", descs[i]);         listItems.add(listItem);      }      // 创建一个SimpleAdapter      SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,            R.layout.simple_item,            new String[] { "personName", "header" , "desc"},            new int[] { R.id.name, R.id.header , R.id.desc });      ListView list = (ListView) findViewById(R.id.mylist);      // 为ListView设置Adapter      list.setAdapter(simpleAdapter);

      // 为ListView的列表项的单击事件绑定事件监听器      list.setOnItemClickListener(new OnItemClickListener()      {         // 第position项被单击时激发该方法         @Override         public void onItemClick(AdapterView<?> parent, View view,                           int position, long id)         {            System.out.println(names[position]                  + "被单击了");         }      });      // 为ListView的列表项的选中事件绑定事件监听器      list.setOnItemSelectedListener(new OnItemSelectedListener()      {         // 第position项被选中时激发该方法         @Override         public void onItemSelected(AdapterView<?> parent, View view,                              int position, long id)         {            System.out.println(names[position]                  + "被选中了");         }         @Override         public void onNothingSelected(AdapterView<?> parent)         {         }      });

   }}

2.main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="horizontal"   android:layout_width="match_parent"   android:layout_height="wrap_content">   <!-- 定义一个ListView -->   <ListView android:id="@+id/mylist"      android:layout_width="match_parent"      android:layout_height="wrap_content"/></LinearLayout>
3.simple_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="horizontal"   android:layout_width="match_parent"   android:layout_height="wrap_content"><!-- 定义一个ImageView,用于作为列表项的一部分。 --><ImageView android:id="@+id/header"   android:layout_width="wrap_content"   android:layout_height="wrap_content"    android:paddingLeft="10dp"/><LinearLayout   android:orientation="vertical"   android:layout_width="match_parent"   android:layout_height="wrap_content"><!-- 定义一个TextView,用于作为列表项的一部分。 --><TextView android:id="@+id/name"   android:layout_width="wrap_content"    android:layout_height="wrap_content"   android:textSize="20dp"   android:textColor="#f0f"   android:paddingLeft="10dp"/><!-- 定义一个TextView,用于作为列表项的一部分。 --><TextView android:id="@+id/desc"   android:layout_width="wrap_content"    android:layout_height="wrap_content"   android:textSize="14dp"   android:paddingLeft="10dp"/></LinearLayout></LinearLayout>

三、使用BaseAdapter

使用BaseAdapter最主要的及时重写其中提供的方法,以及布局文件通过其的getView实现。

package org.crazyit.ui;

import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;

public class MainActivity extends Activity{   ListView myList;   @Override   public void onCreate(Bundle savedInstanceState)   {      super.onCreate(savedInstanceState);      setContentView(R.layout.main);      myList = (ListView) findViewById(R.id.myList);      BaseAdapter adapter = new BaseAdapter()      {         @Override         public int getCount()         {            // 指定一共包含40个选项            return 10;         }         @Override         public Object getItem(int position)         {            return null;         }         // 重写该方法,该方法的返回值将作为列表项的ID         @Override         public long getItemId(int position)         {            return position;         }         // 重写该方法,该方法返回的View将作为列表框         @Override         public View getView(int position               , View convertView , ViewGroup parent)         {            // 创建一个LinearLayout,并向其中添加两个组件            LinearLayout line = new LinearLayout(MainActivity.this);            line.setOrientation(0);            ImageView image = new ImageView(MainActivity.this);            image.setImageResource(R.drawable.ic_launcher);            TextView text = new TextView(MainActivity.this);            text.setText("第" + (position +1 ) + "个列表项");            text.setTextSize(20);            text.setTextColor(Color.RED);            line.addView(image);            line.addView(text);            // 返回LinearLayout实例            return line;         }      };      myList.setAdapter(adapter);   }}2.main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="match_parent"   android:layout_height="match_parent">   <ListView      android:id="@+id/myList"      android:layout_width="match_parent"      android:layout_height="match_parent"/></LinearLayout>
时间: 2024-08-23 22:53:16

ListView及Adapter的使用的相关文章

ListView和Adapter信息显示

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" andr

ListView和Adapter数据适配器的简单介绍

ListView 显示大量相同格式数据 常用属性: listSelector            listView每项在选中.按下等不同状态时的Drawable divider                ListView每项间的间隔Drawable dividerHeight        ListView每项间间隔的间隔高度 常用方法: setAdapter()                设置数据适配器 setOnItemClickListener()        设置每项点击事件

ListView和Adapter的配合使用以及Adapter的重写

ListView和Adapter的使用 首先介绍一下ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组成: 1.View,用于展示列表,通常是一个xml所指定的.大家都知道Android的界面基本上是由xml文件负责完成的,所以ListView的界面也理所应当的使用了xml定义.例如在ListView中经常用到的“android.R.layout.simple_list_item”等, 就是Android系统

listview及adapter

http://blog.csdn.net/shaojie519/article/details/6595720 http://blog.csdn.net/liuhe688/article/details/6532519 http://tech.cncms.com/shouji/android/78565.html http://blog.csdn.net/wangjinyu501/article/details/7716785 http://blog.csdn.net/wangkuifeng01

Android ListView 和 Adapter 从本地/网络获取歌曲列表

本文内容 环境 项目结构 演示1:SimpleAdapter 演示2:BaseAdapter 演示3:customlazylist 演示4:customcompletelazylist 本文只给出演示概要,代码太多,贴出来意义不大,自己下载调试一下,点击此处下载. 本文通过四个示例,循序渐进地演示,将歌曲列表加载到 ListView 控件,歌曲列表,包括缩略图.歌手名.歌曲名等信息,或存放在本地,或以 JSON 形式存放在网络. 环境 Windows 2008 R2 64 位 Eclipse A

第十九讲:ListView与Adapter(一)

天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为.--<孟子·告子下> 本讲内容:ListView列表组件 与 Adapter适配器的用法 一.ListView列表组件: 作用:ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作(通过绑定监听器). 创建一个ListView需要3个元素. (1)ListView展示每一列的View. (2)填入View的数据或者图片等. (3)连接数据与ListView的适配器. ListVi

第十九讲:ListView与Adapter(二)

会当凌绝顶,一览众山小. -- 杜  甫<望岳> 本讲内容:ListView列表组件 与 Adapter适配器的用法 一.ListView使用SimpleAdapter 很多时候需要在列表中展示一些除了文字以外的东西,比如图片等.这时候可以使用SimpleAdapter.可以通过它 使用simpleAdapter的数据一般都是用HashMap构成的列表,列表的每一节对应ListView的每一行.通过SimpleAdapter的构造函数,将HashMap的每个键的数据映射到布局文件中对应控件上.

Xamarin.Android之ListView和Adapter

一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xamarin去实现它,以及如何使用适配器和自定义适配器(本文中的适配器的主要内容就是将原始的数据转换成了能够供列表控件显示的项). 二.简介适配器 在开始之前我们需要先了解下适配器,首先是提供给我们使用的适配器之间的关系: 下面我们将上面的适配器进行简单的介绍: BaseAdapter:顾名思义,就是所以适配器的基类,但是我们不能将其实例化,因为它是一个虚类,一般我们都是继承该类并实现其中的方法,形成形成自定义的列表(大多

超简便的ListView中Adapter的写法

对于 ListView 的使用,他有两个重点的部分,一个是下拉刷新和加载更多,这个今天我们不讲,另外一个是 BaseAdapter 的使用,这个是今天的主角,BaseAdapter 中又有 ViewHolder 模式来实现缓存视图 继承BaseAdapter类,实现以下几个方法 getCount() ->int 返回的是 List的个数 getView(int, View, ViewGroup)->View 返回显示的视图 getItemId(int position) ->long返回