设为首页 收藏本站
查看: 1263|回复: 0

[经验分享] Android数据存储之三SQLite嵌入式数据库(2)

[复制链接]

尚未签到

发表于 2016-11-30 11:53:14 | 显示全部楼层 |阅读模式
      5.编写测试类
      编写一个针对ContactsService的测试类,测试ContactsService类中的各个方法是否正确。
packagecom.changcheng.sqlite.test;
 
importjava.util.List;
importcom.changcheng.sqlite.MyOpenHelper;
importcom.changcheng.sqlite.entity.Contact;
importcom.changcheng.sqlite.service.ContactsService;
importandroid.database.Cursor;
importandroid.test.AndroidTestCase;
importandroid.util.Log;
 
publicclassContactsServiceTestextendsAndroidTestCase {
 
        privatestaticfinalStringTAG="ContactsServiceTest";
 
        //测试创建表
        publicvoidtestCreateTable()throwsThrowable {
                  MyOpenHelper openHelper =newMyOpenHelper(this.getContext());
                  openHelper.getWritableDatabase();
        }
 
        //测试save
        publicvoidtestSave()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  Contact contact1 =newContact(null,"tom","13898679876");
                  Contact contact2 =newContact(null,"lili","13041094909");
                  Contact contact3 =newContact(null,"jack","13504258899");
                  Contact contact4 =newContact(null,"heary","1335789789");
                  contactsService.save(contact1);
                  contactsService.save(contact2);
                  contactsService.save(contact3);
                  contactsService.save(contact4);
        }
 
        //测试find
        publicvoidtestFind()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  Contact contact = contactsService.find(1);
                  Log.i(TAG, contact.toString());
        }
 
        //测试update
        publicvoidtestUpdate()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  Contact contact = contactsService.find(1);
                  contact.setPhone("1399889955");
                  contactsService.update(contact);
        }
 
        //测试getCount
        publicvoidtestGetCount()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  Log.i(TAG, contactsService.getCount() +"");
        }
 
        //测试getScrollData
        publicvoidtestGetScrollData()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  List<Contact> contacts = contactsService.getScrollData(0, 3);
                  Log.i(TAG, contacts.toString());
        }
        
        //测试getScrollDataCursor
        publicvoidtestGetScrollDataCursor()throwsThrowable {
                  ContactsService contactsService =newContactsService(this.getContext());
                  Cursor cursor = contactsService.getScrollDataCursor(0, 3);
                  while(cursor.moveToNext()) {
                           Contact contact =newContact(cursor.getInt(0),
                                              cursor.getString(1), cursor.getString(2));
                           Log.i(TAG, contact.toString());
                  }
        }
 
}
      启用测试功能,不要忘记在AndroidManifest.xml文件中加入测试环境。为application元素添加一个子元素:<uses-library android:name="android.test.runner"/>,为application元素添加一个兄弟元素:<instrumentation android:name="android.test.InstrumentationTestRunner"     android:targetPackage="com.changcheng.sqlite" android:label="Tests for My App" />。
 
      SQLite数据库以单个文件存储,就像微软的Access数据库。有一个查看SQLite数据库文件的工具——SQLite Developer,我们可以使用它来查看数据库。Android将创建的数据库存放在”/data/data/ com.changcheng.sqlite/databases/contacts”,我们将它导出然后使用SQLite Developer打开。
 
      6.分页显示数据
      我们在ContactsService类中,提供了一个获取分页数据的方法。我们将调用它获取的数据,使用ListView组件显示出来。
 
      编辑mail.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <!-- ListView -->
        <ListViewandroid:layout_width="fill_parent"
                  android:layout_height="fill_parent"android:id="@+id/listView"/>
 
</LinearLayout>
 
在mail.xml所在目录里添加一个contactitem.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"android:layout_height="wrap_content">
 
        <!-- contact.id -->
        <TextViewandroid:layout_width="30dip"android:layout_height="wrap_content"
                  android:textSize="20sp"android:id="@+id/tv_id"/>
 
        <!-- contact.name -->
        <TextViewandroid:layout_width="150dip"android:layout_height="wrap_content"
                  android:textSize="20sp"android:layout_toRightOf="@id/tv_id"
                  android:layout_alignTop="@id/tv_id"android:id="@+id/tv_name"/>
 
        <!-- contact.phone -->
        <TextViewandroid:layout_width="150dip"android:layout_height="wrap_content"
                  android:textSize="20sp"android:layout_toRightOf="@id/tv_name"
                  android:layout_alignTop="@id/tv_name"android:id="@+id/tv_phone"/>
 
</RelativeLayout>
 
 
      编辑AndroidSQLite类:
packagecom.changcheng.sqlite;
 
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importcom.changcheng.sqlite.R;
importcom.changcheng.sqlite.entity.Contact;
importcom.changcheng.sqlite.service.ContactsService;
importandroid.app.Activity;
importandroid.database.Cursor;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;
importandroid.widget.Toast;
importandroid.widget.AdapterView.OnItemClickListener;
 
publicclassAndroidSQLiteextendsActivity {
        /** Called when the activity is first created. */
        @Override
        publicvoidonCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                  //获取分页数据
                  ContactsService contactsService =newContactsService(this);
                  List<Contact> contacts = contactsService.getScrollData(0, 3);
                  //获取ListView
                  ListView lv = (ListView)this.findViewById(R.id.listView);
                  //生成List<? extends Map<String, ?>>数据
                  List<HashMap<String, Object>> data =newArrayList<HashMap<String, Object>>();
                  for(Contact contact : contacts) {
                           HashMap<String, Object> item =newHashMap<String, Object>();
                           item.put("_id", contact.get_id());
                           item.put("name", contact.getName());
                           item.put("phone", contact.getPhone());
                           data.add(item);
                  }
                  //生成Adapter
                  SimpleAdapter adapter =newSimpleAdapter(this, data,
                                    R.layout.contactitem,newString[] {"_id","name","phone"},
                                    newint[] { R.id.tv_id, R.id.tv_name, R.id.tv_phone});
                  //设置ListView适配器
                  lv.setAdapter(adapter);
                  
                  //为ListView添加事件
                  lv.setOnItemClickListener(newOnItemClickListener() {
 
                           @Override
                           publicvoidonItemClick(AdapterView<?> parent, View view,
                                              intposition,longid) {
                                    HashMap<String, Object> item = (HashMap<String, Object>) parent
                                                      .getItemAtPosition((int) id);
                                    Toast.makeText(AndroidSQLite.this, item.get("name").toString(),
                                                       1).show();
                           }
 
                  });
        }
}
 
上面编写的分页显示数据比较麻烦,Android为我们提供了一个SimpleCursorAdapter类。使用它可以方便的显示分页数据。将AndroidSQLite类修改为:
      
packagecom.changcheng.sqlite;
 
importcom.changcheng.sqlite.R;
importcom.changcheng.sqlite.service.ContactsService;
importandroid.app.Activity;
importandroid.database.Cursor;
importandroid.os.Bundle;
importandroid.widget.ListView;
importandroid.widget.SimpleCursorAdapter;
 
publicclassAndroidSQLiteextendsActivity {
        /** Called when the activity is first created. */
        @Override
        publicvoidonCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
 
                  //获取分页数据
                  ContactsService contactsService =newContactsService(this);
                  Cursor cursor = contactsService.getScrollDataCursor(0, 3);
                  //获取ListView
                  ListView lv = (ListView)this.findViewById(R.id.listView);
                  //创建Adapter
                  SimpleCursorAdapter adapter =newSimpleCursorAdapter(this,
                                    R.layout.contactitem, cursor,newString[] {"_id","name",
                                                       "phone"},newint[] { R.id.tv_id, R.id.tv_name,
                                                       R.id.tv_phone});
                  //设置ListView适配器
                  lv.setAdapter(adapter);
 
                  //为ListView添加事件
                  lv.setOnItemClickListener(newOnItemClickListener() {
 
                           @Override
                           publicvoidonItemClick(AdapterView<?> parent, View view,
                                              intposition,longid) {
                                    Cursor cursor = (Cursor) parent
                                                       .getItemAtPosition((int) position);
                                    Toast.makeText(AndroidSQLite.this, cursor.getString(1), 1)
                                                       .show();
                           }
                  });
        }
}
 
      OK,在Android中的SQLite操作总结结束!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307723-1-1.html 上篇帖子: 基于SQLITE数据库的C语言编程 下篇帖子: Sqlite中INTEGER PRIMARY KEY的修正
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表