• 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有点糙,须要改进。



  • 相关阅读:
    redis 储存对象
    redis key 查看器
    c# 控制台程序编写RabbitMQ 生产者
    C# 使用Topshelf 构建 基于 window 服务的 RabbitMQ消费端
    asp.net webapi 使用定时任务Hangfire
    asp.net webpi 中使用 ClientHelper 发起HTTP请求
    SQL Server 导入和导出向导 未在本地计算机上注册Mircrosoft.ACE.OLEDB.12.0 提供程序
    c# 使用Linq 表达式 对查询结果分组,保留价格最低的一条
    Asp.Net s请求报传输流收到意外的 EOF 或 0 个字节
    asp.net webapi 中使用rdlc 报表
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4551603.html
Copyright © 2020-2023  润新知