ListActivity

来源:互联网 发布:钢结构软件tekla 编辑:程序博客网 时间:2024/05/16 18:18

ListActivity

 

         ListActivity中比较复杂的部分就是配置其AdapterAdapter常用的有ArrayAdapterSimpleAdapter

 

首先我们先看一下简单的ArrayAdapterListActivity不同于普通Activity的地方是该Activity里边有一个ListView,该ListView绑定了Adapter中的数据。在这个示例中,该ListView绑定了包含三个元素的ArrayList,这三个元素分别是”1, 2, 3”

        

         主要代码:

         首先我们需要让我们的类继承于ListActivity

   1: pubic class ListActivityTest extends ListActivity
   2: {
   3: …..
   4: }
 

让我们直接看代码吧:

   1: public class ListActivityTest extends ListActivity
   2: {
   3:     /** Called when the activity is first created. */
   4:     @Override
   5:     public void onCreate(Bundle savedInstanceState) 
   6:     {
   7:         super.onCreate(savedInstanceState);
   8:         this.setTheme(android.R.style.Theme_Black);
   9:     //    setContentView(R.layout.main);
  10:         
  11:         List items = fillArray();
  12:         
  13:         ArrayAdapter adapter = new ArrayAdapter(
  14:                 this, android.R.layout.simple_expandable_list_item_1, items);
  15:         this.setListAdapter(adapter);
  16:     }
  17:     
  18:     private List fillArray()
  19:     {
  20:         List items = new ArrayList();
  21:         items.add("1");
  22:         items.add("2");
  23:         items.add("3");
  24:         return items;
  25:     }
  26: }
fillArray()返回了一个有三个String对象的List集合,我们在后边用来绑定ListView显示的内容。

然后我们需要在类中的onCreate()函数里边添加如下内容:

List items = this.getData();  // 获得待绑定的集合

ArrayAdapter adapter = new ArrayAdapter(

                this, android.R.layout.simple_expandable_list_item_1,

 items);

         this.setListAdapter(adapter);

其中:this代表当前的Contextandroid.R.layout.simple_expandable_list_item_1代表一个包含ListViewlayoutID,系统自带了几种,我们在这里选择的是系统自带的;items表示被绑定的数据源。

 

OK,简单的看完我们就可以看复杂一点的SimpleAdapter

我们还是先来看一下效果:

 

先上代码:

   1: public class ListActivityTest extends ListActivity
   2: {
   3:     /** Called when the activity is first created. */
   4:     @Override
   5:     public void onCreate(Bundle savedInstanceState) 
   6:     {
   7:         super.onCreate(savedInstanceState);
   8:         
   9:         List> listContent = this.getData();
  10:         
  11:         SimpleAdapter adapter = new SimpleAdapter(this,
  12:                 listContent,
  13:                 android.R.layout.simple_list_item_2,
  14:                 new String[] {"Name", "Address"},
  15:                 new int[] {android.R.id.text1, android.R.id.text2});
  16:         
  17:         setListAdapter(adapter);
  18:     }
  19:     
  20:     private List> getData()
  21:     {
  22:         List> listContent = new ArrayList>();
  23:         
  24:         Hashtable table1 = new Hashtable();
  25:         table1.put("Name", "LiLi");
  26:         table1.put("Address", "China");
  27:         listContent.add(table1);
  28:         
  29:         Hashtable table2 = new Hashtable();
  30:         table2.put("Name", "NiuNiu");
  31:         table2.put("Address", "ChinaHeibei");
  32:         listContent.add(table2);
  33:         
  34:         return listContent;
  35:     }
  36: }

 

讲解:

SimpleAdapter可以实现比ArrayAdapter更加复杂的ListView界面,我们这个程序实现的是一项中有两行文字的ListView

其中的getData()函数返回的是一个List,这个不懂的朋友请查阅相关的Java基础资料。在onCreate()函数中构造了一个SimpleAdapter(),我们看一下SimpleAdapter()的构造函数:

SimpleAdapter(Context context, List> data, int resource, String[] from, int[] to)

其中:

context : 当前的环境上下文,一般直接传递thisOK

Data:数据源,我们之前构造的listContent就是该ListView的数据源

resourcelayout模板资源,我们可以传递Android自带的,当然也可以自定义,我们在这里为了简单传递的是Android自带的

fromto:这个我们需要联合起来看,这两个是平行数组,也就是数组的大小相同,根据我们的例子,我们需要在android.R.id.text1中显示数据源中索引为”name”的项,在android.R.id.text2种显示索引为”address”的项。

原创粉丝点击