adm1989

导航

ListView和Adapter

1.简单Adapter

     其中的主要是两个函数的使用:

     a.得到一个Adapter

             new ArrayAdapter<String> (Context context, int textViewResourceId, List<String> objects)

    b.使用ListView的setAdapter()方法

         setAdapter()(ListAdapter adapter)

 

2.simpleCursorAdapter的用法

      a.得到一个Cursor对象

         query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

          Cursor cursor = getContentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);

         b.得到一个 SimpleCursorAdapter对象

             SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

             

 

 

3.simpleAdapter的用法

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
得到一个SimpleAdapter对象,重点是getData函数(将得到的数据保存在一个List里面)

 

4.自定义Adapter的用法

    1.继承BaseAdapter类

    2.重写getView()--每一项显示成什么样有它决定 

        getView(int position, View convertView, ViewGroup parent)

         可以在这个函数中实现Button的setOnClickListener事件

    3.重写getCount()--一共有多少项由它决定

    4.实现OnItemClickListener事件--为每一项添加事件实现

       在布局文件中的LinearLayout添加   android:descendantFocusability="blocksDescendants";

     Button中添加android:focusable="false"

       可以完成setOnClickListener和onListItemClick事件的分别响应

     5.继承ListActivity的布局文件中如果有ListView的话,其ID应该为 ”@android:id/list”,如果你不定义成这样,这个 ListView 是不能被 ListActivity 识别的

 

 

 

 

posted on 2012-07-30 12:33  adm1989  阅读(294)  评论(0编辑  收藏  举报