[Learn Android Studio 汉化教程]Reminders实验:第一部分(续)
最后,有两个删除方法。第一个针对特定的提醒用 id参数和数据库对象来生成和执行一条删除语句。第二个方法需要数据库生成并执行一条删除语句来移除所有表单里的提醒。
这时,你需要一个手段从数据库提出提醒并放入到ListView。清单5-13演示了必要的逻辑,通过继承之前你看到的特别的Adapter Android类来绑定数据库值到单独的行对象。创建一个新类RemindersSimpleCursorAdapter在com.apress.gerber.reminders包下,并完成处理代码。当解析导入类时,使用android.support.v4.widget.SimpleCursorAdapter class类。
Listing 5-13. RemindersSimpleCursorAdapter Code
public class RemindersSimpleCursorAdapter extends SimpleCursorAdapter {
public RemindersSimpleCursorAdapter(Context context, int layout, Cursor c, String[]
from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
//to use a viewholder, you must override the following two methods and define a ViewHolder class
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.colImp = cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);
holder.listTab = view.findViewById(R.id.row_tab);
view.setTag(holder);
}
if (cursor.getInt(holder.colImp) > 0) {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));
} else {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
}
}
static class ViewHolder {
//store the column index
int colImp;
//store the view
View listTab;
}
}
我们用适配器把所有提醒登记到ListView。在运行时中,ListView将重复调用在适配器里的bindView()方法,以单独的示图对象作为用户装载器,且在清单里滚动。填入这些清单条目到示图里是适配器的工作。在这个例子代码里,我们使用了适配器的子类SimpleCursorAdapter。这个类用了一个游标对象,它保存着跟踪表单里的行轨迹。
这里你看到了一个ViewHolder模式的例子。这是一个容易认识的Android模式,每个ViewHolder对象含有一个示图的标签。用数据源(在这个例子里是Cursor)的值,这个对象加载清单里的示图对象。ViewHolder定义为一个内部静态类,有两个实例变量,一个用于索引重要的表单项另一个用于在布局里定义的row_tab示图。
bindView()方法开始于父类在示图里从游标到元素的map值方法的调用。然后检查看(holder)是否附有一个标签还是有必要创建一个新的(holder)。然后bindView()方法用重要列索引和早先定义的row_tab配置容器的实例变量。在容器被发现或配置后,从当前提醒的COL_IMPORTANT常量秋决定row_tab用什么颜色。这个例子用了新的橙色,那个你要加到colors.xml: <color name="orange">#ffff381a</color>。
早先你用了ArrayAdapter来管理模型和示图之间的关系。SimpleCursorAdapter也是用同样的模式,虽然它的模型是SQLite数据库。将清单5-14更改到你的RemindersDbAdaper和RemindersSimpleCursorAdapter类里。
Listing 5-14. RemindersActivity Code
public class RemindersActivity extends ActionBarActivity {
private ListView mListView;
private RemindersDbAdapter mDbAdapter;
private RemindersSimpleCursorAdapter mCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
mListView.setDivider(null);
mDbAdapter = new RemindersDbAdapter(this);
mDbAdapter.open();
Cursor cursor = mDbAdapter.fetchAllReminders();
//from columns defined in the db
String[] from = new String[]{
RemindersDbAdapter.COL_CONTENT
};
//to the ids of views in the layout
int[] to = new int[]{
R.id.row_text
};
mCursorAdapter = new RemindersSimpleCursorAdapter(
//context
RemindersActivity.this,
//the layout of the row
R.layout.reminders_row,
//cursor
cursor,
//from columns defined in the db
from,
//to the ids of views in the layout
to,
//flag - not used
0);
// the cursorAdapter (controller) is now updating the listView (view)
//with data from the db (model)
mListView.setAdapter(mCursorAdapter);
}
//Abbreviated for brevity
}
这时候如果你运行app,你还是看不到清单里有任何东西;屏幕完全是空的,因为你最后的修改在例子的数据部分插入了数据库功能。按Ctrl+K | Cmd+K并提交的修改,提交信息:Adds SQLite database persistence for reminders and a new color for important reminders。聪明点的话,你可能想弄清楚怎么用新的RemindersDbAdaper把例子里的条目加回来。这将在下章描述,你可以继续看下去并检查下作业了。
Summary
小结
致此,你有了个成熟的Android应用。在这章,你学会了怎样设置第一个Android项目并用Git控制代码。你也探索了怎么编辑Android布局,用设计或文本模式。你也看到建立一个在动作条里的溢出菜单。这章的最后探索了ListView和Adapter,以及绑定数据到内建的SQLite数据库。在接下来的章节里,增加创建和编辑提醒的功能,你将完成这个app。
最后贴心的送上源代码:
RemindersActivity.java
import android.database.Cursor;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;public class RemindersActivity extends ActionBarActivity {private ListView mListView;private RemindersDbAdapter mDbAdapter;private RemindersSimpleCursorAdapter mCursorAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_reminders);mListView = (ListView) findViewById(R.id.reminders_list_view);mListView.setDivider(null);mDbAdapter = new RemindersDbAdapter(this);mDbAdapter.open();Cursor cursor = mDbAdapter.fetchAllReminders();//from columns defined in the dbString[] from = new String[]{RemindersDbAdapter.COL_CONTENT};//to the ids of views in the layoutint[] to = new int[]{R.id.row_text};mCursorAdapter = new RemindersSimpleCursorAdapter(//contextRemindersActivity.this,//the layout of the rowR.layout.reminders_row,//cursorcursor,//from columns defined in the dbfrom,//to the ids of views in the layoutto,//flag - not used0);// the cursorAdapter (controller) is now updating the listView (view)//with data from the db (model)mListView.setAdapter(mCursorAdapter);//Abbreviated for brevity// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
// this,
// R.layout.reminders_row,
// R.id.row_text,
// new String[]{"1Record", "2Record", "3Record", "4Record"});
// mListView.setAdapter(arrayAdapter);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.action_new://create new ReminderLog.d(getLocalClassName(), "create new Reminder");return true;case R.id.action_exit:finish();return true;default:return false;}}}
RemindersSimpleCursorAdapter.java
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.ViewGroup;/*** 权兴权意* Created by car on 2016/8/15.*/
public class RemindersSimpleCursorAdapter extends SimpleCursorAdapter{public RemindersSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {super(context, layout, c, from, to, flags);}@Overridepublic View newView(Context context, Cursor cursor, ViewGroup parent) {return super.newView(context, cursor, parent);}@Overridepublic void bindView(View view, Context context, Cursor cursor) {super.bindView(view, context, cursor);ViewHolder holder = (ViewHolder) view.getTag();if(holder == null){holder = new ViewHolder();holder.collmp = cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);holder.listTab = view.findViewById(R.id.row_tab);view.setTag(holder);}if(cursor.getInt(holder.collmp) > 0){holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));}else{holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));}}static class ViewHolder{int collmp;//column indexView listTab;}
}
[Learn Android Studio 汉化教程]Reminders实验:第一部分(续)相关推荐
- 第七章 : Git 介绍 (上)[Learn Android Studio 汉化教程]
Learn Android Studio 汉化教程 [翻译]Git介绍 Git版本控制系统(VCS)快速成为Android应用程序开发以及常规的软件编程领域内的事实标准.有别于需要中心服务器支持的早期 ...
- [Learn Android Studio 汉化教程]Refactoring Code
重构代码 在Android Studio中开发,解决方案不会总是一蹴而成的.作为一个有效率的编程者,在你的开发,调试和测试中需要一些弹性以及代码重构.随着在这章中的行进,你将明白Android Stu ...
- Android Studio 汉化教程
Android Studio 汉化教程 前言 1. 第一种方法,直接在软件中汉化 2. 第二种方法,下载汉化包 前言 Android Studio 汉化教程,有两种汉化教程: 1. 第一种方法,直接在 ...
- Android Studio汉化教程
相信大家在刚入门一款新的编译软件的时候经常会因为软件的语言问题,导致使用起来不够流畅,这时候,汉化是我们最好的选择,在这里,我来给大家简单介绍一下AS汉化的方法: 第一步 先下载AS一个汉化包http ...
- Android Studio汉化(插件教程)
Android Studio汉化(万能插件菜鸟教程) 查找中文插件 查找自己需要的版本 安装插件 无敌菜鸟教程,希望大家点赞,你们的加油是我学习的动力!! 查找中文插件 打开jetbrian插件 查找 ...
- Android Studio汉化包
链接:https://pan.baidu.com/s/1nwSsm-pexmpfRzw_Vu97qw 提取码:v3w2 内附汉化教程,亲测有效! 但是感觉汉化之后的Android Studio 不太 ...
- Android studio 汉化方法
Android studio 汉化方法 我的汉化版链接:http://download.csdn.net/detail/ai_yantinghao/9814988 下载汉化包,加压以后,直接复制到软件 ...
- [转自安智论坛]Android软件汉化教程(强制汉化/Apktool汉化/精简/去广告)
前言: 现在随处都可以找到功能强大的汉化工具,操作简单,上手快,汉化不再是件麻烦事. 想学汉化的朋友只要你用心,你也可以自己汉出优秀的作品,因为汉化根本没啥技术含量,要的只是持之以恒. 不要再羡慕别人 ...
- android studio汉化流程
1.查看自己的android studio版本 2.访问汉化包官网 3.打开andriod studio,点击setting之后点击导入 最后重启软件即可
- Android Studio 汉化
1. 第一步,下载这个汉化包:链接: https://pan.baidu.com/s/1bYKg_0WvvnmqU6CkH92AEw 提取码:vvp2 2. 将下载好的文件复制到这个目录: 3. 重启 ...
最新文章
- 深入理解malloc和free
- 【OpenGL】十二、OpenGL 绘制线段 ( 绘制单条线段 | 绘制多条线段 | 依次连接的点组成的线 | 绘制圈 | 绘制彩色的线 )
- [Qt教程] 第21篇 数据库(一)Qt数据库应用简介
- 解决webpack 打包出现额外的xxxx.LICENSE.js文件
- SAP Hybris Commerce启用customer coupon的前提条件
- unchecked异常_为什么要在Java中使用Unchecked异常而不是Checked异常
- js字符串拼接中关于单引号和双引号的那些事
- 122_Power PivotPower BI不连续日期的日环比
- windows命令行启动常用工具
- SAP License:SAP合同类型的使用
- DB2数据库对现有表格字段修改
- OpenCV——CvMatchShapes函数
- VS2010 中文版本
- 心理传染与恐怖的“模仿者效应”
- WPF 3D 贴图: 为你的二次元老婆们做个3D画廊
- MIT.6.00.1X --Week 3 Lecture 5 -- 'Divid and conqer' algorithm 分而治之
- mac 教程 终端设置代理
- 树莓派有线网络设置_树莓派设置固定IP之有线网和无线网方法
- vivado安装步骤
- 对一名电子信息工程专业应届毕业生的建议 .
热门文章
- Electron--桌面应用开发(基本应用,快速入门)
- 我的世界服务器卡无限刷物品,我的世界怎么刷物品 我的世界无限刷物品教程...
- 【Leetcode】780. Reaching Points
- 如何从零创造一个围棋AI
- Scroller类及scroll相关方法总结
- matlab dff求导,matlab的多元函数微积分学.ppt
- mybatis批量删除 java_Mybatis批量删除数据操作方法
- 《关键对话》要点整理
- python第三方模块之pyquery
- 一篇文章从了解到入门shell