Android入门:ListView(SimpleAdapter实现)

ListView是类似于将一个界面分为一行一行,如下图:



注意: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);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值