一、使用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>

ListView及Adapter的使用的更多相关文章

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

    ListView 显示大量相同格式数据 常用属性: listSelector            listView每项在选中.按下等不同状态时的Drawable divider            ...

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

    ListView和Adapter的使用   首先介绍一下ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组成: ...

  3. 在为ListView设置adapter时出错

    为listView设置adapter,代码如下: SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.m ...

  4. Android listview与adapter用法

    listview与adapter用法 博客分类: android   一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

  5. ListView 和 Adapter用法

    一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...

  6. android 开发之 ListView 与Adapter 应用实践

    在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...

  7. [Android] Android RecycleView和ListView 自定义Adapter封装类

    在网上查看了很多对应 Android RecycleView和ListView 自定义Adapter封装类 的文章,主要存在几个问题: 一).网上代码一大抄,复制来复制去,大部分都运行不起来,或者 格 ...

  8. Android listview与adapter用法(BaseAdapter + getView)

    Android listview与adapter用法http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html package ...

  9. listview与adapter用法

    Android listview与adapter用法 listview与adapter用法 博客分类: android   一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用 ...

随机推荐

  1. c-version:null]] could not deserialize the servlet-context scoped attribute with name: "MENU_LIST"

    <Jul 26, 2013 10:45:02 AM CST> <Error> <HTTP> <BEA-101362> <[ServletConte ...

  2. CSS-边框-效果

    1.1边框 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特性,我们需要重点掌握. 1.1.1边框圆角 border-radius 每个角可以设置两个值,x值,y值 ...

  3. Android studio教程:[3]修改背景主题

    android studio开发环境的背景主题是可以更改的,现在都流行黑色背景,这样让软件显得更高端大气的,更加赏心悦目,但最主要的还是看起来更舒适更顺眼.下面就教大家如何更改背景主题. 工具/原料 ...

  4. 池化 - Apache Commons Pool

    对于那些创建耗时较长,或者资源占用较多的对象,比如网络连接,线程之类的资源,通常使用池化来管理这些对象,从而达到提高性能的目的.比如数据库连接池(c3p0, dbcp), java的线程池 Execu ...

  5. xaml控件样式大全(太有用了)C#

    地址:链接:http://pan.baidu.com/s/1jGlMyEi 密码:zaeg http://blog.csdn.net/lhx527099095/article/category/943 ...

  6. ajax数据显示,使用js通用模板

    最近用ajax获取数据,上级要求要自己写一个js模板,以往看到的js模板,大都数都是在js里面拼接的,现在换一种比较简单的写法, 通过ajax获取数据源,js模板循环显示数据 function Get ...

  7. qq电脑管家抽奖页面源代码

    桌面qq电脑管家中个人中心的抽奖页面竟然是HTML写的,光大我HTML.!!! <!DOCTYPE html> <html> <head lang="en&qu ...

  8. CSS混合模式

    前面的话   层叠上下文z-index只是解决两个元素覆盖,谁离用户更近的问题.而CSS混合模式,则是处理两个元素覆盖部分如何混合的问题.如果了解photoshop的话,对这种现象应该不陌生.CSS3 ...

  9. HTML5学习笔记-video

    HTML5 <video> - 方法.属性以及事件 方法 play()——播放 pause()——暂停 load()——加载 canPlayType——检测您的浏览器是否能播放不同类型的视 ...

  10. SQL Server中in与exist效率比较

    in和exists in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询. 一直以来认为exists比in效率高的说法是不准确的. 如果查询的两 ...