Android入门:ListView(SimpleAdapter实现)
2012-07-01 00:30
387 查看
ListView是类似于将一个界面分为一行一行,如下图:
![](http://my.csdn.net/uploads/201205/04/1336137136_7838.GIF)
注意:listView.getItemAtPosition(int pos)内部调用了adapter.getItem(int position)方法,而每种适配器返回的类型都是不一样的:
当SimpleAdapter返回Map<String,Object>
SimpleCursorAdapter返回Cursor;
继承BaseAdapter返回自己实现的类型;
一般ListView都是用来显示列表的,一般列表的数据都是来自数据库的,因此我们这里假设前面已经实现了一个DBService类,里面存在pageQuery(int offset,int length);
比如dbservice.pageQuery(3,5);表示跳过3个记录,插入5条记录;
![](http://my.csdn.net/uploads/201206/30/1341071847_9191.GIF)
main.xml
item.xml
MainActivity.java
总结:SimpleAdapter不需要内部实现Adapter,只能实现每个item布局都一样的列表;
SimpleCursorAdapter:
注意:listView.getItemAtPosition(int pos)内部调用了adapter.getItem(int position)方法,而每种适配器返回的类型都是不一样的:
当SimpleAdapter返回Map<String,Object>
SimpleCursorAdapter返回Cursor;
继承BaseAdapter返回自己实现的类型;
一般ListView都是用来显示列表的,一般列表的数据都是来自数据库的,因此我们这里假设前面已经实现了一个DBService类,里面存在pageQuery(int offset,int length);
比如dbservice.pageQuery(3,5);表示跳过3个记录,插入5条记录;
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content"//注意一定要wrap_content android:layout_height="wrap_content"//注意一定要wrap_content android:orientation="horizontal" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="ID" /> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="NAME" /> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="AGE" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="fill_parent" > </ListView> </LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/id1" android:layout_width="100dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/name" android:layout_width="100dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/age" android:layout_width="100dp" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package org.xiazdong.db; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xiazdong.db.domain.Person; import org.xiazdong.db.service.DBService; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)this.findViewById(R.id.listview); DBService service = new DBService(this); List<Person> persons = service.pageQuery(0, 10); List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>(); for(Person person:persons){ HashMap<String,Object>map = new HashMap<String,Object>(); map.put("id", person.getId()); map.put("name", person.getName()); map.put("age", person.getAge()); data.add(map); } System.out.println(data); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age}); //data表示显示的数据,一个Map为一行,List<Map>表示多行 //R.layout.item表示一个item的布局 //new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上 listView.setAdapter(adapter); } }
总结:SimpleAdapter不需要内部实现Adapter,只能实现每个item布局都一样的列表;
二、设置每个Item的监听器
SimpleAdapter:private OnItemClickListener listener = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position,//parent就是ListView,view表示Item视图,position表示数据索引 long id) { ListView lv = (ListView)parent; HashMap<String,Object> person = (HashMap<String,Object>)lv.getItemAtPosition(position);//SimpleAdapter返回Map Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show(); } }; listView.setOnItemClickListener(listener);
SimpleCursorAdapter:
Cursor cursor = (Cursor)lv.getItemAtPosition(position);
相关文章推荐
- Android入门:ListView(SimpleAdapter实现)
- Android入门:ListView(SimpleAdapter实现)
- Android入门:ListView(SimpleAdapter实现)
- Android入门- listView的实现 SimpleAdapter
- 10_android入门_listView的几种Adaptor的实现_SimpleAdapter
- Android入门:ListView(SimpleCursorAdapter实现)
- Android入门教程四十一之ListView Item多布局的实现
- Android入门:ListView(SimpleCursorAdapter实现)
- 07_android入门_listView的几种Adaptor的实现_自定义的adaptor
- android——ListView功能的实现(使用simpleAdapter)
- 06-android入门_listview采用系统和自定义的方式实现item
- Android入门:ListView(继承BaseAdapter实现)
- 08_android入门_listView的几种Adaptor的实现_ArrayAdapter
- 09_android入门_listView的几种Adaptor的实现_simplecursoradaptor
- Android入门:ListView(继承BaseAdapter实现)
- android总结整理----(2)自定义ListView(SimpleAdapter实现)
- Android采用ListView实现数据列表显示2-使用SimpleAdapter进行数据绑定
- Android中的ListView使用案例(SimpleAdapter实现)
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- Android基础入门教程——2.4.8 ListView Item多布局的实现