前面的两篇博文讲的ListView的实现,首先要拿到布局文件中的ListView,然后为其添加适配器,写一个内部类继承BaseAdapter,然后重写里面的getCount和getView方法,其实在android内部,google工程师已经写好了某些类已继承BaseAdapter,这样我们就可以直接new出ArrayAdapter和SimpleAdapter,选取不同的构造函数达到我们的要求。

 

ArrayAdapter的构造函数如下,其它的构造函数内部实现原理也都一样:

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

context:上下文

 

resource:布局列表项的layout文件

textViewResource:布局文件中要修改的TextView的id

 objects:填充TextView的对象,一般情况下是String数组

public class MainActivity extends Activity {public static String[] data = {"textView1", "textView2", "textView3","textView4", "textView5", "textView6","textView7", "textView8", "textView9"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView listView = (ListView)findViewById(R.id.listView);                listView.setAdapter(new ArrayAdapter(        MainActivity.this,        R.layout.show,        R.id.textView,        data));    }}

 
Android之ListViewArrayAdapter,SimpleAdapter_第1张图片
 

 

 

        由上构造函数可以看出ArrayAdapter的实现功能是比较单一的,它只能够修改布局文件中的TextView的某一个,对比较简单的布局用ArrayAdapter还是可以的,ArrayAdapter就像ListView<1>中在getView方法中直接new一个TextView对象,然后对其进行修饰是一样的,而在ListView<2>中,我们已经可以自己指定ListView显示的条目的结构与内容,google工程师同样用了一个简单的形式去实现这种功能,那就是SimpleAdapter。

 

SimpleAdapter的构造函数:

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

         SimpleAdapter的构造韩函数只此一个

 

context:上下文

data:一个List集合,集合中的元素是Map对象

resource:布局文件

from、to:Map中的键和布局文件中的组建id一一对应

        SimpleAdapter可以实现动态修改布局文件显示的内容:


Android之ListViewArrayAdapter,SimpleAdapter_第2张图片
 

 

 

更多相关文章

  1. 详解Android应用中使用TabHost组件进行布局的基本方法
  2. android 的相对布局的使用小记
  3. 海康威视视频监控demo 源码+库文件
  4. Android studio 添加assets文件夹
  5. android R 文件生成不了
  6. 深入解析_Android的自定义布局
  7. Android通用圆角布局(兼容Android P)
  8. Android 实现文件上传功能(upload)

随机推荐

  1. eclipse上Android(安卓)NDK开发环境搭建
  2. Android(安卓)Vibrator 的用法
  3. Android:UI控件AutoCompleteTextView、Mul
  4. Android(安卓)使用Javascript作为脚本计
  5. 使用Uiautomator遇到的问题
  6. Android设备兼容性 1
  7. Android(安卓)Studio安装及首次运行遇到
  8. 百度云推送实践
  9. android中使用zxing扫描二维码以及条形码
  10. Android的安装过程