您的位置:首页 > 移动开发 > Android开发

Android之ArrayAdapter的详解

2015-10-05 09:47 288 查看
ArrayAdapter
简单的ArrayAdapter

Android中Adapter我是这么理解的,是数据和视图之间的桥梁,数据在adapter中做处理,然后显示到视图(ListView)上面。

Adapter有很多种,有ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter.
我在项目中用到过的就ArrayAdapter<T>, (数组适配器也可以是对象数组适配器),BaseAdapter(所有Adapter的基类),SimpleAdapter,CursorAdapter(数据来源是cursor)。


ArrayAdapter

本文主要讲解ArrayAdapter的创建方法,我把ArrayAdapter分为三种:简单的、样式丰富的但内容简单的、内容丰富的。

简单的ArrayAdapter

主函数:

public class MainActivity extends AppCompatActivity {
private ListView mListView;
private ArrayAdapter arrayAdapter;
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView= (ListView) findViewById(R.id.listview);
//1、新建一个适配器
//ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
String [] data={"文本一","文本二","文本三","文本四","文本五","文本六"};
//适配器加载数据源
arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
//视图(ListView)加载适配器
mListView.setAdapter(arrayAdapter);
}
}


布局文件:

<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:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>

</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: