ArrayAdapter详解:ArrayAdapter(Context context, int textViewResourceId, T[] objects)

标签: ArrayAdapterandroid
1661人阅读 评论(0) 收藏 举报
分类:

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

尊重原创,转载请注明出处】http://blog.csdn.net/guyuealian/article/details/46790873
1、定义:android.widget.ArrayAdapter<T>
      ArrayAdapter常用6个构造方法:
      ArrayAdapter(Context context, int
textViewResourceId)
      ArrayAdapter(Context context, int resource, int
textViewResourceId)
      ArrayAdapter(Context context, int
textViewResourceId, T[] objects)
      ArrayAdapter(Context context, int resource, int
textViewResourceId, T[] objects)        
      ArrayAdapter(Context context, int
textViewResourceId, List<T> objects)
      ArrayAdapter(Contextcontext, intresource, inttextViewResourceId, List<T>objects)
2、参数说明:
(1)context      : The current context.(传递Context对象,一般为this)
(2)resource   :  The id of the TextView within the layout resource to be populated:该布局资源在res/layout/Xxx.xml中定义后会自动在R.java文件中生成对应的ID,然后利用R.layout.Xxx调用即可,也可以使用系统已定义好的布局文件,比如:
android.R.layout.simple_list_item_1(前面必须加上android否则出错)
android.R.layout.simple_expandable_list_item_1等等。
注意:布局文件必须是关于TextView组件的XML文件(也可以TextView类的子类组件,如Button, CheckedTextView, Chronometer, DigitalClock, EditText, TextClock都是TextView的子类),列表项的每个条项都是按照这个TextView组件进行显示。
(3)textViewResourceId: 对应的是resource布局文件中TextView组件的ID;
(4
)objects     :The objects to represent in the ListView.数组或者List集合,用于为多个列表项提供数据
从中可知:textViewResourceId用于控制每个列表项的TextView组件(控制布局形式显示风格),而objects(数组或者List集合)为每个列表项的TextView组件提供显示数据。数组或者List集合有多少个元素,将会生成多少个列表项。

      从中可知:textViewResourceId用于控制每个列表项的TextView组件(控制布局形式显示风格),而objects(数组或者List集合)为每个列表项的TextView组件提供显示数据。数组或者List集合有多少个元素,将会生成多少个列表项。

3、 resource和textViewResourceId参数区别:

      一般来说,resource对应的是布局XML资源文件,而textViewResourceId指的是XML资源文件中textView组件的ID。问题来了,为什么ArrayAdapter构造方法都需要传入TextViewResourceId,有时不需要传入resource参数。例如给构造方法ArrayAdapter<String>(Context context, int textViewResourceId, String[] objects)传入参数是这样的new ArrayAdapter<String>(this, R.layout.array_item, arr1);你会发现此时textViewResourceId传入的参数是R.layout.array_item不是组件textView的ID,而是布局文件的ID,Why???????

       先来看看ArrayAdapter源码先!!!!!!!!你回惊奇的发现,ArrayAdapter的6个构造方法都调用了一个init()方法:

   /**
     * Constructor
     *
     * @param context The current context.
     * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     */
    public ArrayAdapter(Context context, int textViewResourceId) {
        init(context, textViewResourceId, 0, new ArrayList<T>());
    }

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a layout to use when
     *                 instantiating views.
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     */
    public ArrayAdapter(Context context, int resource, int textViewResourceId) {
        init(context, resource, textViewResourceId, new ArrayList<T>());
    }

    /**
     * Constructor
     *
     * @param context The current context.
     * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
        init(context, textViewResourceId, 0, Arrays.asList(objects));
    }

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a layout to use when
     *                 instantiating views.
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
        init(context, resource, textViewResourceId, Arrays.asList(objects));
    }

    /**
     * Constructor
     *
     * @param context The current context.
     * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
        init(context, textViewResourceId, 0, objects);
    }

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a layout to use when
     *                 instantiating views.
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
        init(context, resource, textViewResourceId, objects);
    }

      init()方法源码:

    private void init(Context context, int resource, int textViewResourceId, List<T> objects) {
        mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mResource = mDropDownResource = resource;
        mObjects = objects;
        mFieldId = textViewResourceId;
    }

            对比各个构造方法和init()的参数,你会发现init()第二个参数是resource是必须要传入的,也就是布局资源文件不可省略,而textViewResourceId默认是0。 当构造方法含有resource参数时是布局资源文件的ID,且在实例化View时,该布局资源文件包含一个TextView 组件The resource ID for a layout file containing a TextView to use when instantiating views.);若省略resource参数时, 此时构造方法调用init()传入的第三个参数 默认设置为0。

       因此:new ArrayAdapter<String>(this, R.layout.array_item, arr1);等于new ArrayAdapter<String>(this, R.layout.array_item,R.id.TextView ,arr1);,其中R.id.TextView是布局文件array_item中的组件TextView的ID

例子1:new ArrayAdapter<BookContent.Book>(getActivity(),android.R.layout.simple_list_item_activated_1,android.R.id.text1, arr))

     这里第二个参数resource:android.R.layout.simple_list_item_activated_1,该布局文件是系统自定义好的TextView组件,其ID为@android:id/text1

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
      第三个参数textViewResourceId:android.R.id.text1,对应的是resource布局文件中TextView组件的ID,即text1

例子2

package org.crazyit.ui;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class ListActivityTest extends ListActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// 无需使用布局文件
		String[] arr = { "孙悟空", "猪八戒", "唐僧" };
		// 创建ArrayAdapter对象
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			android.R.layout.simple_list_item_multiple_choice, arr);
		// 设置该窗口显示列表
		setListAdapter(adapter);
	}
}


1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:277333次
    • 积分:3611
    • 等级:
    • 排名:第9159名
    • 原创:87篇
    • 转载:42篇
    • 译文:1篇
    • 评论:82条
    博客专栏
    最新评论