1.listview入门,自定义的数据适配器<RelativeLayout 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" tools:context=".ListViewActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></RelativeLayout>public class ListViewActivity extends Activity { private ListView lv; public String tag = "ListViewActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new MyAdapter()); } /** * 自定义的数据适配器 * * @author Administrator * */ private class MyAdapter extends BaseAdapter { /** * 控制listview里面有多个条目. */ @Override public int getCount() { return 15; } /** * 返回每个位置对应的view对象 */ @Override public View getView(int position, View convertView, ViewGroup parent) { // 这个方法被调用了多少次? Log.i(tag, "POSITION:" + position); TextView tv = new TextView(ListViewActivity.this); tv.setTextSize(30); tv.setTextColor(Color.RED); tv.setText("我是第" + position + "个条目"); return tv; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } } }2.采用layoutInflater打气筒创建一个view对象在上面的基础上添加list_item.xml<?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" /> <TextView android:id="@+id/tv_number" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>/** * 返回每个位置对应的view对象 */@Overridepublic View getView(int position, View convertView, ViewGroup parent) { // 这个方法被调用了多少次? Log.i(tag, "POSITION:" + position); View view = View.inflate(getApplicationContext(), R.layout.list_item, null); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_number = (TextView) view.findViewById(R.id.tv_number); tv_name.setText("name:"+position); tv_number.setText(position+""); return view;}3.常用数据适配器ArrayAdapterprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.tv_name, new String[] { "aaaa", "bbbb", "cccc", "ddddd" }));}4.常用数据适配器SimpleAdapterprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); lv = (ListView) findViewById(R.id.lv); List<Map<String, String>> data = http://www.mamicode.com/new ArrayList
无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)