ListView用自定义适配器绑定数据
2014-08-22 15:52
525 查看
1、在ListView里显示数据
2、getScrollData()代码
3、一条数据单击事件
//自定义适配器 private void show2() { List<Person> persons=service.getScrollData(0, 20);//需要绑定的数据,getScrollData代码在下方
PersonAdapter adapter=new PersonAdapter(getApplicationContext(), persons, R.layout.item); listView.setAdapter(adapter); }
2、getScrollData()代码
/** * 分页获取记录 * @param offset 跳过前面多少条记录 * @param maxResult 每页显示多少条记录 * @return */ public List<Person> getScrollData(int offset,int maxResult){ SQLiteDatabase db=dbOpenHelper.getReadableDatabase(); List<Person> persons=new ArrayList<Person>(); Cursor cursor=db.rawQuery("select * from person order by personid asc limit?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)}); while(cursor.moveToNext()){ int personid=cursor.getInt(cursor.getColumnIndex("personid")); String name=cursor.getString(cursor.getColumnIndex("name")); String phone=cursor.getString(cursor.getColumnIndex("phone")); int amount=cursor.getInt(cursor.getColumnIndex("amount")); persons.add(new Person(personid, name, phone,amount)); } cursor.close(); return persons; }
3、一条数据单击事件
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); service=new PersonService(this); listView=(ListView)this.findViewById(R.id.listview); show(); listView.setOnItemClickListener(new ItemClickListener()); } private final class ItemClickListener implements OnItemClickListener{ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ListView listView=(ListView)arg0; //自定义适配器 Person person=(Person)listView.getItemAtPosition(arg2); Toast.makeText(getApplicationContext(), person.getName(), 2).show(); } }
相关文章推荐
- 安卓:将数据库中的数据查询出来用adapter(SimpleAdapter,SimpleCursorAdapter,自定义适配器CursorAdapter)绑定到listview上
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- Android之ListView性能优化——一行代码绑定数据——万能适配器
- ListView的数据显示,三种适配器绑定方式
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(一)
- ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听.
- ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听.
- ListView通过自定义适配器来显示数据并对Item项以及子view项的控件实现监听
- Android中 自定义数据绑定适配器BaseAdapter的方法
- KSOAP解析webService绑定到自定义适配器listView中显示
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
- ListView 数据绑定、数据汇总及自定义排序示例
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- Android 自定义Listview 如何绑定Sqlite数据库数据
- 复杂自定义数据对象绑定到GridView的问题
- 具有ADO.NET和自定义对象的数据绑定应用程序