当前位置: 首页 > news >正文

Android开发模板------自己定义SimpleCursorAdapter的使用

使用SimpleCursorAdapter所设计的table(数据表)一定要有_id字段名称,否则会出现“找不到_id”的错误

SimpleCursorAdapter直接使用的方法:

SimpleCursorAdapter同意你绑定一个游标的列到ListView上,并使用自己定义的layout显示每一个项目。

SimpleCursorAdapter的创建,须要传入当前的上下文、一个layout资源,一个游标和两个数组:一个包括使用的列的名字,还有一个(同样大小)数组包括View中的资源ID,用于显示对应列的数据值。

//第一步:从数据库读取数据
		dbHelper = new DBHelper(HistoryOrderActivity.this);
		database = dbHelper.getWritableDatabase();
		cursor = database.rawQuery("SELECT * FROM "+DBHelper.TABLE_ORDER+" where feedbackTime is not null", null);
		//startManagingCursor(cursor); 被遗弃的方法,主要是把cursor的生命周期交由Activity管理
		String[] fromColumns = new String[] {
				"orderDescription",
				"orderEffectiveTime",
				"orderConsumeTime",
				"promotion",
				"feedbackInfo",
				"feedbackTime",
				};
		int[] toLayoutIDs = new int[] { 
				R.id.description, 
				R.id.effectiveTime, 
				R.id.consumeTime, 
				R.id.promotion,
				R.id.feedbackInfo, 
				R.id.feedbackTime};
		adapter = new SimpleCursorAdapter(this, R.layout.histortyorder, cursor, fromColumns, toLayoutIDs,0);

SimpleCursorAdapter自己定义的使用:

//第一步:从数据库读取数据
		dbHelper = new DBHelper(HistoryOrderActivity.this);
		database = dbHelper.getWritableDatabase();
		cursor = database.rawQuery("SELECT * FROM "+DBHelper.TABLE_ORDER+" where feedbackTime is not null", null);
		//startManagingCursor(cursor); 被遗弃的方法,主要是把cursor的生命周期交由Activity管理
		String[] fromColumns = new String[] {
				"orderDescription",
				"orderEffectiveTime",
				"orderConsumeTime",
				"promotion",
				"feedbackInfo",
				"feedbackTime",
				};
		int[] toLayoutIDs = new int[] { 
				R.id.description, 
				R.id.effectiveTime, 
				R.id.consumeTime, 
				R.id.promotion,
				R.id.feedbackInfo, 
				R.id.feedbackTime};
		if (cursor == null) {
			return;
		}
		adapter = new HistoryOrderAdapter(HistoryOrderActivity.this, R.layout.histortyorder,
				cursor, fromColumns, toLayoutIDs, 0);

适配器的实现:
public class HistoryOrderAdapter extends SimpleCursorAdapter {
	private Cursor m_cursor;
	private Context m_context;
	private LayoutInflater miInflater;

	public HistoryOrderAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to, int flags) {
		super(context, layout, c, from, to, flags);
		m_context = context;
		m_cursor = c;
	}

	@Override
	public void bindView(View arg0, Context arg1, Cursor arg2) {
		View convertView = null;
		if (arg0 == null) {
			convertView = miInflater.inflate(R.layout.histortyorder, null);
		} else {
			convertView = arg0;
		}
		TextView tv_Description = (TextView) convertView
				.findViewById(R.id.description);
		TextView tv_EffectiveTime = (TextView) convertView
				.findViewById(R.id.effectiveTime);
		TextView tv_ConsumeTime = (TextView) convertView
				.findViewById(R.id.consumeTime);
		TextView tv_promotion = (TextView) convertView
				.findViewById(R.id.promotion);
		TextView tv_FeedbackInfo = (TextView) convertView
				.findViewById(R.id.feedbackInfo);
		TextView tv_FeedbackTime = (TextView) convertView
				.findViewById(R.id.feedbackTime);

		tv_Description.setText(arg2.getString(arg2
				.getColumnIndex("orderDescription")));
		tv_EffectiveTime.setText(ShopUtils.changeTimestampToTime(Long
				.valueOf(arg2.getString(arg2
						.getColumnIndex("orderEffectiveTime")))));
		tv_ConsumeTime
				.setText(ShopUtils.changeTimestampToTime(Long.valueOf(arg2
						.getString(arg2.getColumnIndex("orderConsumeTime")))));
		tv_promotion.setText(arg2.getString(arg2.getColumnIndex("promotion")));
		tv_FeedbackInfo.setText(arg2.getString(arg2
				.getColumnIndex("feedbackInfo")));
		tv_FeedbackTime.setText(ShopUtils.changeTimestampToTime(Long
				.valueOf(arg2.getString(arg2.getColumnIndex("feedbackTime")))));
	}
}

HistoryOrderAdapter有点糙,须要改进。



相关文章:

  • 代码html
  • Php5.5新特性 Generators详解
  • js修改后没反应-看看是不是取的缓存
  • 新时代的“网盘模型”——够快云库
  • ADT下载地址整理(转)
  • IntelliJ IDEA 使用心得与常用快捷键
  • asp.net Session 一般设置
  • iOS UITableView的Section Footer添加按钮
  • 南邮算法分析和实验设计1 分而治之
  • OpenStack对象存储——Swift
  • PHP生成缩略图、加水印
  • C语言中的单引号和双引号含义
  • 《Java从入门到精通》第七章学习笔记
  • 给Python用virtualenvwrapper
  • nginx.conf 完整的集群配置
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Java程序员幽默爆笑锦集
  • scala基础语法(二)
  • Sublime text 3 3103 注册码
  • vue 配置sass、scss全局变量
  • vue从创建到完整的饿了么(18)购物车详细信息的展示与删除
  • 对超线程几个不同角度的解释
  • 给初学者:JavaScript 中数组操作注意点
  • 离散点最小(凸)包围边界查找
  • 深入浅出Node.js
  • 使用SAX解析XML
  • 树莓派 - 使用须知
  • 数据结构java版之冒泡排序及优化
  • nb
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • ​什么是bug?bug的源头在哪里?
  • #QT(一种朴素的计算器实现方法)
  • #每日一题合集#牛客JZ23-JZ33
  • ()、[]、{}、(())、[[]]命令替换
  • (3)选择元素——(17)练习(Exercises)
  • (超简单)构建高可用网络应用:使用Nginx进行负载均衡与健康检查
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (二)hibernate配置管理
  • (十六)串口UART
  • (一) storm的集群安装与配置
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .NET Reactor简单使用教程
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • /proc/stat文件详解(翻译)
  • @Autowired多个相同类型bean装配问题
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [2024-06]-[大模型]-[Ollama]- WebUI
  • [ASP.NET MVC]Ajax与CustomErrors的尴尬
  • [Big Data - Kafka] kafka学习笔记:知识点整理
  • [BUG]Datax写入数据到psql报不能序列化特殊字符
  • [CF494C]Helping People
  • [Codeforces] number theory (R1600) Part.11