ListView
目录
ListView的使用是遵循MVC架构的
M: 模型层---用户要查看的内容-----数据源
V: 视图层---用户可以看到的界面---listview
C: 控制层---将M和V绑在一起-------adapter
一般数据源都是用arraylist之类动态数组;
至于ListView中的视图层则是用两个layout下的xml文件实现的
其中一个(与activity绑定的)只需在布局文件中加入ListView节点;
另一个则是用来进行ListView控件中的布局,该布局会在适配器中与数据源绑定Adapter,根据ListView要实现的功能,适配器分为三类:ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。其中SimpleAdapter有最好的扩充性,有最好的使用效果;
ArrayAdapter 只能生成一行。而SimpleCursorAdapter则是simpleAdapter与数据库的结合,可以显示出数据库中内容。
ArrayAdapter
activity代码
package com.example.listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.lv);
String[] cities = new String[]{"上海","北京","南京","武汉","东京"};
ArrayAdapter myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,cities);
//simple_list_item_1是系统自带的一个list表单
listview.setAdapter(myAdapter);
}
}
####layout代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市" />
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>"
</LinearLayout>
SimpleAdapter详解:
activity:
package com.example.listview; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listview = (ListView) findViewById(R.id.lv); ArrayList<HashMap<String,Object>> arraylist = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> map = new HashMap<String ,Object>(); map.put("城市","北京"); map.put("国家", "中国"); map.put("照片",R.drawable.ic_launcher); arraylist.add(map); map = null; map = new HashMap<String , Object>(); map.put("城市","冲绳"); map.put("国家", "日本"); map.put("照片",R.drawable.ic_launcher); arraylist.add(map); map=null; map = new HashMap<String , Object>(); map.put("城市","首尔"); map.put("国家", "韩国"); map.put("照片",R.drawable.ic_launcher); arraylist.add(map); map=null; map = new HashMap<String , Object>(); map.put("城市","平壤"); map.put("国家", "朝鲜"); map.put("照片",R.drawable.ic_launcher); arraylist.add(map); //生成适配器的Item和动态数组对应的元素 SimpleAdapter simpleAdapter = new SimpleAdapter(this, arraylist,//数据源 R.layout.listv, //Listv的XML实现 //(*****)动态数组与ImageItem对应的子项 new String[]{"图片","国家","城市"}, new int[]{R.id.iv_photo,R.id.tv_country,R.id.tv_city}); //添加并且显示 listview.setAdapter(simpleAdapter); }
}
activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="城市" /> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="match_parent"/>" </LinearLayout>
listv
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv.photo"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_country"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_city"/> </LinearLayout>
由代码可知,ListView的绑定使用步骤:
1.绑定layout里的listview组件
2.将要输入listview的数据,加入到生成的动态数组中
3.用adapter将动态数组与listview绑定(即上面说的定义了Listview组件布局的xml),各参数含义见上面代码
4.将该adapter添加并显示到页面布局:list.setAdapter(listItemAdapter);
SimpleCursorAdapter
由名字就知道,这是SimpleAdapter的一个增强型。再看cursor又能知道,这种增强是基于数据库的。
package com.example.listview;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.lv);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query( ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if(cursor !=null){
startManagingCursor(cursor);
}
ListAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor,
new String[] { PhoneLookup.DISPLAY_NAME },
new int[] { android.R.id.text1 });
listview.setAdapter(listAdapter);
stopManagingCursor(cursor);
}
}
这只是简单的获取联系人姓名的例子而已,当然,为了能够运行该例子,我们需要添加下面的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
这里有一个方法很值得我们注意:startManagingCursor()。它的使用是基于这样的前提:游标结果集里有很多的数据记录,像是通讯录这样的结果集,肯定符合要求。使用该方法的目标主要是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期就和Activity自动同步了,这样在Activity结束的时候就能自动结束Cursor的使用。使用前最好是先判断Cursor是否为空,以免发生错误,而且使用后也要用stopManagingCursor()方法来停掉它。
SimpleCursorAdapter除了数据来源指定是数据库之外,就和SimpleAdapter的用法几乎一样了。