您的位置:首页 > 移动开发 > Android开发

【android】ArrayAdapter 使用浅析

2016-03-08 12:40 453 查看

目录

操作环境

导读

源码

实例

操作环境

操作系统:win7-64bit 旗舰版

android 版本:android-23

模拟器:海马玩模拟器 0.9.0 Beta

导读

ArrayAdapter 是 BaseAdapter 的封装,目的是简化 Adapter 的继承复写等。ArrayAdapter 默认情况下希望你仅提供只有一个 TextView 的 item 布局;当然,如果布局内容远远不止于此,可以增加一个参数,该参数用于描述将要使用的 TextView。

源码

构造函数:

public ArrayAdapter(Context context, @LayoutRes int resource)
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId)


public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull T[] objects)
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
@NonNull T[] objects)


public ArrayAdapter(Context context, @LayoutRes int resource, @NonNull List<T> objects)
public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
@NonNull List<T> objects)


getView 方法:

/**
* {@inheritDoc}
*/
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(mInflater, position, convertView, parent, mResource);
}

private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
ViewGroup parent, int resource) {
View view;
TextView text;

if (convertView == null) {
view = inflater.inflate(resource, parent, false);
} else {
view = convertView;
}

try {
if (mFieldId == 0) {
//  If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
//  Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}

T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}

return view;
}


简单说明:

构造方法:

参数 @LayoutRes int resource: item 布局对应的布局文件;

参数 @IdRes int textViewResourceId: 布局文件对应的 TextView 控件Id,如果不传入该参数,则默认值为0;

getView 方法:

如果 View convertView 非 null,则继续使用它;否则,通过 inflater.inflate( resource, parent, false ) 方式创建新的;

【此处高能】:简单说就是 ArrayAdapter 只接受 TextView 或其衍生类来设置数据映射

如果未指定 textViewResourceId,则会将 convertView 强转成 TextView

否则,通过 findViewById 将其从布局文件中找到后 强转成 TextView

将数据集中对应的元素转成String 或 CharSequence之后设置到 TextView 系列控件;

实例

test_item.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">

<TextView android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="50dp"
/>

</LinearLayout>


test_array_adapter.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/lv_test"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

</LinearLayout>


MainActivity.java:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_array_adapter);
this.testArrayAdatper();
}

// 测试 ArrayAdapter
void testArrayAdatper(){
ListView lv = (ListView)this.findViewById(R.id.lv_test);
// 设置数据列表
ArrayList<String> list = new ArrayList<>();
for(int i=0; i<5; i++){
list.add("item_" + i);
}
// 实例化对象
ArrayAdapter aa = new ArrayAdapter(this, R.layout.test_item, R.id.tv_content, list);
// 设置适配器
lv.setAdapter(aa);
}


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息