背单词App开发日记5(上)
2015-08-02 10:54
369 查看
2015.07.28
今天是我阳历生日,先自己庆祝一下!哈哈.
为什么总是写博客的时间和发博客的时间不一样那?这我也没办法啊.....因为每天花在这上面的时间比较少,所以做完一项的时间很长(回家全是事啊.....衰).但是又不想看起来拖得太长,所以就这样了......
好了,开始正题,本次我集中精力解决了"背单词App开发日记3"中的第四个问题----怎么把数据库(单词表)绑定到控件上还有显示的问题.至此,我认为我的开发来到了最核心的部分.就是说把数据可视化,当然没有那么高大上了......废话少说,还是记录一下我的实现过程把.
我当时在想我该如何实现这个事情,想了好久想出来几个方案:
a.直接把db数据库对象通过intent传过去,然后读一条显示一条顺便修改一条.
b.预处理一下,用intent传过去一些等效的对象,这样方便显示,在通过一些方式传回来修改.
c.不用intent,直接在里面生成一个数据库,然后去修改.
d.下下策:抛弃数据库,用其他办法实现这个事情
对于这几个替补我最后选择了第二个方法.主要是因为我认为第一个方法比较麻烦,重载过程我有点受不了...于是选择将数据库的信息取出来传到显示单词的那个控件里去在想办法传回来;第三个办法我暂时想不到怎么实现保存,而且我之前已经封装了SQLite,不想再重新写了......下面着重写一下我目前完成的事情.
1.intent的传递
简单地讲,intent对象是一种请求,它传递的是一些关于接下来的信息集合,它可以连接控件和控件,也可以连接控件和服务.在intent里面,我们可以定义若干需要下一个控件去接收去处理的信息,例如数据,动作(概括的讲)等等.收到intent的控件就可以根据intent里面的信息去进行一系列的操作以完成要在不同的activity之间传递数据,就要在intent中包含相应的内容,一般来说数据中最基本的应该包括:
Action:用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。具体的可以查阅android SDK-> reference中的Android.content.intent类,里面的constants中定义了所有的action。
一些常用的Action:
ACTION_CALL activity 启动一个电话.
ACTION_EDIT activity 显示用户编辑的数据.
ACTION_MAIN activity 作为Task中第一个Activity启动
ACTION_SYNC activity 同步手机与数据服务器上的数据.
ACTION_BATTERY_LOW broadcast receiver 电池电量过低警告.
ACTION_HEADSET_PLUG broadcast receiver 插拔耳机警告
ACTION_SCREEN_ON broadcast receiver 屏幕变亮警告.
ACTION_TIMEZONE_CHANGED broadcast receiver 改变时区警告.
Data: 要事实的具体的数据,一般由一个Uri变量来表示
Category:一个字符串,包含了关于处理该intent的组件的种类的信息。一个intent对象可以有任意个category。intent类定义了许多category常数.
addCategory()方法为一个intent对象增加一个category,
removeCategory删除一个category,
getCategories()获取intent所有的category.
Type:显式指定Intent的数据类型(MIME)(多用途互联网邮件扩展,Multipurpose Internet Mail Extensions)。比如,一个组件是可以显示图片数据的而不能播放声音文件。很多情况下,data类型可在URI中找到,比如content:开头的URI,表明数据由设备上的content provider提供。但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
MIME类型有2种形式:
1.1 单个记录的格式: vnd.android.cursor.item/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains/122(一条列车信息的uri)的MIME类型是vnd.android.cursor.item/vnd.example.rail
1.2 多个记录的格式:vnd.android.cursor.dir/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains (所有列车信息)的MIME类型是vnd.android.cursor.dir/vnd.example.rail
component:指定Intent的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。例如:
Intent it = new Intent(Activity.Main.this, Activity2.class); startActivity(it);
startActivity(it);
extras:附加信息,例如ACTION_TIMEZONE_CHANGED的intent有一个"time-zone"附加信息来指明新的时区,而ACTION_HEADSET_PLUG有一个“state”附加信息来指示耳机是被插入还是被拔出。intent对象有一系列put...()和set...()方法来设定和获取附加信息。 这些方法和Bundle对象很像。事实上附加信息可以使用putExtras()和getExtras()作为Bundle来读和写。例如:
//用Bundle传递数据 Intent it = new Intent(Activity.Main.this, Activity2.class); Bundle bundle=new Bundle(); bundle.putString("name", "This is from MainActivity!"); it.putExtras(bundle); startActivity(it); //获得数据 Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name");
在应用中,我们可以以两种形式来使用Intent:
1.1 显式Intent:指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context, Class)来指定)。通过指定具体的组件类,通知应用启动对应的组件。
2.2 隐式Intent:没有指定comonent属性的Intent。这些Intent需要包含足够的信息,这样系统才能根据这些信息,在在所有的可用组件中,确定满足此Intent的组件。
对于直接Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些间接Intent,通过解析将 Intent映射给可以处理此Intent的Activity、Service或Broadcast Receiver。
2.intent传递参数的具体实现
首先在第一个封装数据库操作的activity里面进行数据的提取:
然后在显示界面的控件里面就可以接收数据了,我还设置了界面自动刷新的功能,用的handler.
今天就写这么多,后面再下集里面讨论一下怎么更新数据库,加油!
今天是我阳历生日,先自己庆祝一下!哈哈.
为什么总是写博客的时间和发博客的时间不一样那?这我也没办法啊.....因为每天花在这上面的时间比较少,所以做完一项的时间很长(回家全是事啊.....衰).但是又不想看起来拖得太长,所以就这样了......
好了,开始正题,本次我集中精力解决了"背单词App开发日记3"中的第四个问题----怎么把数据库(单词表)绑定到控件上还有显示的问题.至此,我认为我的开发来到了最核心的部分.就是说把数据可视化,当然没有那么高大上了......废话少说,还是记录一下我的实现过程把.
我当时在想我该如何实现这个事情,想了好久想出来几个方案:
a.直接把db数据库对象通过intent传过去,然后读一条显示一条顺便修改一条.
b.预处理一下,用intent传过去一些等效的对象,这样方便显示,在通过一些方式传回来修改.
c.不用intent,直接在里面生成一个数据库,然后去修改.
d.下下策:抛弃数据库,用其他办法实现这个事情
对于这几个替补我最后选择了第二个方法.主要是因为我认为第一个方法比较麻烦,重载过程我有点受不了...于是选择将数据库的信息取出来传到显示单词的那个控件里去在想办法传回来;第三个办法我暂时想不到怎么实现保存,而且我之前已经封装了SQLite,不想再重新写了......下面着重写一下我目前完成的事情.
1.intent的传递
简单地讲,intent对象是一种请求,它传递的是一些关于接下来的信息集合,它可以连接控件和控件,也可以连接控件和服务.在intent里面,我们可以定义若干需要下一个控件去接收去处理的信息,例如数据,动作(概括的讲)等等.收到intent的控件就可以根据intent里面的信息去进行一系列的操作以完成要在不同的activity之间传递数据,就要在intent中包含相应的内容,一般来说数据中最基本的应该包括:
Action:用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。具体的可以查阅android SDK-> reference中的Android.content.intent类,里面的constants中定义了所有的action。
一些常用的Action:
ACTION_CALL activity 启动一个电话.
ACTION_EDIT activity 显示用户编辑的数据.
ACTION_MAIN activity 作为Task中第一个Activity启动
ACTION_SYNC activity 同步手机与数据服务器上的数据.
ACTION_BATTERY_LOW broadcast receiver 电池电量过低警告.
ACTION_HEADSET_PLUG broadcast receiver 插拔耳机警告
ACTION_SCREEN_ON broadcast receiver 屏幕变亮警告.
ACTION_TIMEZONE_CHANGED broadcast receiver 改变时区警告.
Data: 要事实的具体的数据,一般由一个Uri变量来表示
Category:一个字符串,包含了关于处理该intent的组件的种类的信息。一个intent对象可以有任意个category。intent类定义了许多category常数.
addCategory()方法为一个intent对象增加一个category,
removeCategory删除一个category,
getCategories()获取intent所有的category.
Type:显式指定Intent的数据类型(MIME)(多用途互联网邮件扩展,Multipurpose Internet Mail Extensions)。比如,一个组件是可以显示图片数据的而不能播放声音文件。很多情况下,data类型可在URI中找到,比如content:开头的URI,表明数据由设备上的content provider提供。但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
MIME类型有2种形式:
1.1 单个记录的格式: vnd.android.cursor.item/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains/122(一条列车信息的uri)的MIME类型是vnd.android.cursor.item/vnd.example.rail
1.2 多个记录的格式:vnd.android.cursor.dir/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains (所有列车信息)的MIME类型是vnd.android.cursor.dir/vnd.example.rail
component:指定Intent的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。例如:
Intent it = new Intent(Activity.Main.this, Activity2.class); startActivity(it);
startActivity(it);
extras:附加信息,例如ACTION_TIMEZONE_CHANGED的intent有一个"time-zone"附加信息来指明新的时区,而ACTION_HEADSET_PLUG有一个“state”附加信息来指示耳机是被插入还是被拔出。intent对象有一系列put...()和set...()方法来设定和获取附加信息。 这些方法和Bundle对象很像。事实上附加信息可以使用putExtras()和getExtras()作为Bundle来读和写。例如:
//用Bundle传递数据 Intent it = new Intent(Activity.Main.this, Activity2.class); Bundle bundle=new Bundle(); bundle.putString("name", "This is from MainActivity!"); it.putExtras(bundle); startActivity(it); //获得数据 Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name");
在应用中,我们可以以两种形式来使用Intent:
1.1 显式Intent:指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context, Class)来指定)。通过指定具体的组件类,通知应用启动对应的组件。
2.2 隐式Intent:没有指定comonent属性的Intent。这些Intent需要包含足够的信息,这样系统才能根据这些信息,在在所有的可用组件中,确定满足此Intent的组件。
对于直接Intent,Android不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些间接Intent,通过解析将 Intent映射给可以处理此Intent的Activity、Service或Broadcast Receiver。
2.intent传递参数的具体实现
首先在第一个封装数据库操作的activity里面进行数据的提取:
@SuppressWarnings("deprecation") public void gogogo(View view) { Cursor c=mgr.queryTheCursor(); startManagingCursor(c); c.moveToFirst(); int k=0; int []_id=new int[3113]; String []English=new String[3112]; String []Chinese=new String[3113]; int []WordRepeatedNumber=new int[3113]; //这里貌似忽略了第一条数据...不过问题不大 while(c.moveToNext()) { _id[k]=c.getInt(c.getColumnIndex("_id")); English[k]=c.getString(c.getColumnIndex("en_meaning")); Chinese[k]=c.getString(c.getColumnIndex("cn_meaning")); WordRepeatedNumber[k]=c.getInt(c.getColumnIndex("repeating_number")); k++; } //Log.i("tag"," "+English[1]); //参数传递 Intent intent=new Intent(MySQLiteActivity.this,MyHandlerActivity.class); Bundle bundle=new Bundle(); bundle.putIntArray("_id", _id); bundle.putStringArray("English", English); bundle.putStringArray("Chinese", Chinese); bundle.putIntArray("WordRepeatedNumber", WordRepeatedNumber); intent.putExtras(bundle); MySQLiteActivity.this.startActivity(intent); }这里用到了bundle这个对象,通过bundle这个载体我将数据库的单词,释义和记忆次数变成了数组保存在了intent对象里面.
然后在显示界面的控件里面就可以接收数据了,我还设置了界面自动刷新的功能,用的handler.
package example.mywordapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import example.mywordapp.R; import mywordapp.wordsqlite.Word; public class MyHandlerActivity extends Activity { private Button btnStart; private Button btnStop; private TextView EnText; private TextView CnText; private boolean run = false; public String []English=new String[3113]; public String []Chinese=new String[3113]; public int []WordRepeatedNumber=new int[3113]; public int k=0; private final Handler handler = new Handler(); private final Runnable task = new Runnable() { //建立一个线程任务,通过重复调用这个任务来实现定时刷新的功能 @Override public void run() { // TODO Auto-generated method stub if (run) { handler.postDelayed(this, 1000); k++; } EnText.setText("" + English[k]); CnText.setText(""+Chinese[k]); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_handler); <span style="white-space:pre"> </span>//接收数据的代码 Intent intent=getIntent(); English=intent.getStringArrayExtra("English"); Chinese=intent.getStringArrayExtra("Chinese"); WordRepeatedNumber=intent.getIntArrayExtra("WordRepeatedNumber"); btnStart = (Button) findViewById(R.id.Button01); btnStop = (Button) findViewById(R.id.Button02); EnText = (TextView) findViewById(R.id.en_text); CnText=(TextView)findViewById(R.id.cn_text); btnStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub run = true; updateButton(); handler.postDelayed(task, 1000); //刷新 } }); btnStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub run = false; updateButton(); handler.post(task); } }); } private void updateButton() { btnStart.setEnabled(!run); btnStop.setEnabled(run); } }这里把刷新时间设置为了一秒,有点短,后面再改.
今天就写这么多,后面再下集里面讨论一下怎么更新数据库,加油!
相关文章推荐
- iOS Quartz2D - 矩阵操作
- Android 媒体扫描MediaScanner
- Appium - WebView测试(Android)
- Android进阶学习之路——View事件的传递机制
- ios-状态栏的改变
- Android Service 访问安全
- Android中对Log日志文件的分析
- Foundation框架-学习笔记<一>
- Android中的长度单位详解(dp、sp、px、in、pt、mm)
- 【Cocos2d入门教程十三】 浅析Cocos2d下3.x与2.x屏幕截图的区别
- iOS Quartz2D - 画文字 和 图片
- ubuntu14.04 android设备调试问题
- 如何在Android上编写高效的Java代码
- Android中ContentProvider详解例程
- IBM AppScan官方帮助文档错别字缺陷,IBM的测试人员也太粗心了吧
- Android DatePicker日期选择器、TimePicker时间选择器的使用
- 浅析Android Camera开发中的三个尺寸和三种变形 (贡献一个自适配Picturesize和Previewsize的工具类)
- Android ListView工作原理完全解析
- Android项目打包成APK文件
- [iOS]用instancetype代替id作返回类型有什么好处?