安卓ArrayAdapter的构造方法有什么不同

2025-03-24 06:29:37
推荐回答(2个)
回答1:

ArrayAdapter是从BaseAdapter派生出来的,具备BaseAdapter的所有功能,但ArrayAdapter更为强大,它实例化时可以直接使用泛型构造,我们在Android SDK中可以看到android.widget.ArrayAdapter的字样,当然也可以使用ArrayAdapter(Context context, int textViewResourceId) 第二个参数直接绑定一个layout,下文的例子我们使用Java泛型实例化。

通过Adapter我们构造一个支持icon的item,下面我们在getView中使用的是imageView显示图片,当然android123提示大家其实TextView也可以直接绑定一个drawable对象显示的,void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) 或void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) 和void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) 即可,其中第二种的int类型指定的资源id,方位则是textview什么位置显示drawable对象

回答2:

public ArrayAdapter( int resource, int textViewResourceId)一般用的是这个,前者是布局样式,后者是数据源,比如数组什么的