一、列表视图(ListView)
附:适配器包括三种:
ArrayAdapter
SimpleAdapter
SimpleCursorAdapter
1.ArrayAdapter—-ListView:
ListView允许用户通过手指上下滑动的方式,将屏幕外的数据滚动到屏幕内,同时屏幕原有数据滚出屏幕。
1.简单用法:
简单功能:建立一个简单的listview,可以滑动。
步骤:
1)在布局中加入listview控件
2)修改代码:
创建要传递给listview的数组;
这里借助ArrayAdapter适配器,把要传入的数组传给listview。适配器的三个参数为:当前上下文、listview子项布局的id、要适配的数据。
范例:
layout:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view"
></ListView>
activity:
public class ThirdActivity extends Activity{
private ListView mListview;
private String[] array = {
"apple","banana","orange","pear","grape","strawberry","cherry","pear","ettre","rewy","rewyhrtwhj","rewytyrestre","rewytrw","trwhtrw","trewe4rty","rewytrew","rewyhtrw","trwyrtew"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
mListview=(ListView) findViewById(R.id.list_view);
//创建适配器,将array数据以安卓已写好的simple_list_item_1布局的格式做好。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ThirdActivity.this, android.R.layout.simple_list_item_1, array);
//将创建好的适配器设置给listview
mListview.setAdapter(adapter);
}
}
结果:
补充:可以将simple_list_item_1布局更改,但id名称一定不能改变,以为android默认arrayadapter找这个id。
2.SimpleAdapter-ListView
SimpleAdapter(context,数据,item布局,from,to)
注:1)这里的数据类型是list
//1.listView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
//2.自定义的item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<TextView
android:id="@+id/hobby"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
//3.activity界面代码
public class ThirdActivity extends Activity{
private ListView mListView;
private String[] array = {
"apple","pear","orange","neat","watar"};
private List<Map<String,String>> list ;//这里list选用ArrayList,map类型选择HashMap
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
mListView = (ListView) findViewById(R.id.listview);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, array);
//mListView.setAdapter(adapter);
//SimpleAdapter演示:传入的数据时MAP类型
initList();
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.simple_list,new String[]{
"name","age","sex","hobby"},new int[]{R.id.name,R.id.age,R.id.sex,R.id.hobby});
mListView.setAdapter(adapter);
}
//对数据进行初始化,不用返回值
public void initList(){
list = new ArrayList();//对list进行初始化,新建ArrayList
HashMap<String, String> zhangsan = newHash("张三"