其实这一节的视频,也不需要怎么做笔记了,
打开EOE社区组编的那本《Android开发入门与实战》第107至110页,
同样也是做通讯录,内容看一遍,代码敲一遍,
就是这一节30分钟视频的内容。
官方介绍
1.ListView概述
A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view.
ListView是一个在垂直可滚动的列表中展示内容的view控件。
跟这个view控件相关的展示内容来源于ListAdapter。
2.BaseAdapter概述
Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface) and Spinner (by implementing the specialized SpinnerAdapter interface).
BaseAdapter实现了ListAdapter和SpinnerAdapter这两个接口,
在ListView或者Spinner需要一个Adapter时,都可以用BaseAdapter。
(我自己的烂英语翻译的,这里"Common"是指"共同",而不是"常见"吧?)
核心代码
Activity的:
ListView listView = (ListView) findViewById(R.id.listView);
PhoneBookAdapter phoneBookAdapter = new PhoneBookAdapter(MainActivity.this);
listView.setAdapter(phoneBookAdapter);
ListView所在的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
PhoneBookAdapter:
public class PhoneBookAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private String[] mNames = {"AAAAA","BBBBB","CCCCC"};
public PhoneBookAdapter(Context context){
this.mContext = context;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mNames.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mNames[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.item_phone_book_friend,null);
TextView nameTextView = (TextView)convertView.findViewById(R.id.name_text_view);
nameTextView.setText(mNames[position]);
return convertView;
}
}
单个item的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
效果:
最基本的内容,真没什么好说的,
难的在后面……