android复习数据存储Word下载.docx
《android复习数据存储Word下载.docx》由会员分享,可在线阅读,更多相关《android复习数据存储Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
@string/app_name"
theme="
@style/AppTheme"
activity
name="
com.xu.junittest.MainActivity"
intent-filter>
actionandroid:
android.intent.action.MAIN"
categoryandroid:
android.intent.category.LAUNCHER"
/intent-filter>
/activity>
uses-libraryandroid:
android.test.runner"
/>
/application>
instrumentationandroid:
android.test.InstrumentationTestRunner"
targetPackage="
/instrumentation>
/manifest>
2编写单元测试代码
packagecom.xu.junittest.service;
importjunit.framework.Assert;
importandroid.test.AndroidTestCase;
publicclassPersonServiceTestextendsAndroidTestCase
{
publicvoidtestSave()throwsException
{
PersonServiceservice=newPersonService();
service.save(null);
}
publicvoidtestAdd()throwsException
intresult=service.add(1,2);
Assert.assertEquals(3,result);
}
*****************************************************************************************************************************************************************
使用SAX或者DOM或者PULL解析XML文件
在Android平台上可以使用SimpleAPIforXML(SAX),DocumentObjectModel(DOM)和Android附带的pull解析器解析XML文件
Person.xml
persons>
personid="
23"
name>
liming<
/name>
age>
30<
/age>
/person>
20"
zhangxiaoxiang<
50<
/persons>
Person.java
packagecom.pull.domain;
publicclassPerson
privateIntegerid;
privateStringname;
privateIntegerage;
publicIntegergetId()
returnid;
publicvoidsetId(Integerid)
this.id=id;
publicStringgetName()
returnname;
publicvoidsetName(Stringname)
this.name=name;
publicIntegergetAge()
returnage;
publicvoidsetAge(Integerage)
this.age=age;
@Override
publicStringtoString()
return"
Person[id="
+id+"
name="
+name+"
age="
+age+"
]"
;
PersonService.java
packagecom.pull.service;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;
importorg.xmlpull.v1.XmlPullParser;
//importorg.xmlpull.v1.XmlPullParserFactory;
importandroid.util.Xml;
importcom.pull.domain.Person;
publicclassPersonService
publicstaticList<
Person>
getPersons(InputStreamxml)throwsException
List<
persons=null;
Personperson=null;
//XmlPullParserpullPaeser=XmlPullParserFactory.newInstance().newPullParser();
XmlPullParserpullPaeser=Xml.newPullParser();
//得到pull解析器
pullPaeser.setInput(xml,"
UTF-8"
);
//为pull解析器设置要解析的数据
intevent=pullPaeser.getEventType();
while(event!
=XmlPullParser.END_DOCUMENT)
switch(event)
{
caseXmlPullParser.START_DOCUMENT:
persons=newArrayList<
();
break;
caseXmlPullParser.START_TAG:
if("
person"
.equals(pullPaeser.getName()))
intid=newInteger(pullPaeser.getAttributeValue(0));
person=newPerson();
person.setId(id);
}
name"
Stringname=pullPaeser.nextText();
person.setName(name);
age"
intage=newInteger(pullPaeser.nextText());
person.setAge(age);
caseXmlPullParser.END_TAG:
persons.add(person);
person=null;
default:
event=pullPaeser.next();
returnpersons;
/*保存数据到xml文件
*
*@parampersons
*@throwsException
*/
publicstaticvoidsave(List<
persons,OutputStreamout)throwsException
XmlSerializerserilaize=Xml.newSerializer();
serilaize.setOutput(out,"
serilaize.startDocument("
true);
serilaize.startTag(null,"
persons"
for(Personperson:
persons)
serilaize.startTag(null,"
serilaize.attribute(null,"
id"
person.getId().toString());
serilaize.text(person.getName());
serilaize.endTag(null,"
serilaize.text(person.getAge().toString());
serilaize.endTag(null,"
serilaize.endTag(null,"
serilaize.endDocument();
out.flush();
out.close();
Manifest.xml
com.pull.xmlparse"
8"
com.pull.xmlparse.MainActivity"
PersonServiceTest.java
packagecom.pull.test;
importcom.pull.service.PersonService;
importandroid.util.Log;
publicvoidtestPersons()throwsException
InputStreamxml=this.getClass().getClassLoader().getResourceAsStream("
person.xml"
persons=PersonService.getPersons(xml);
Log.i("
PersonServiceTest"
person.toString());
//保存一些数据到xml文件中
publicvoidtestsavePersons()throwsException
persons=newArrayList<
persons.add(newPerson(43,"
zhangshan"
80));
persons.add(newPerson(42,"
zhangshan1"
persons.add(newPerson(44,"
zhangshan2"
persons.add(newPerson(45,"
zhangshan3"
FilexmlFile=newFile(getContext().getFilesDir(),"
file.xml"
FileOutputStreamout=newFileOutputStream(xmlFile);
PersonService.save(persons,out);
insertintoperson(name,number)values('
zhangshan'
'
10'
)
select*frompersonwherename='
updatepersonsetnumber='
119'
wherename='
deletefrompersonwherename='
内容提供者(contentprovider)
创建一个内容提供者
1继承contentprovider
2并在配置文件中配置
3定义一个uri的匹配器用于匹配uri
packagecom.hema.db;
importandroid.content.ContentProvider;
importandroid.content.ContentValues;
importandroid.content.UriMatcher;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
import.Uri;
publicclassPersonProviderextendsContentProvider{
//定义一个uri的匹配器用于匹配uri,如果路径不满足条件返回-1
privatestaticUriMatchermatcher=newUriMatcher(UriMatcher.NO_MATCH);
privatestaticfinalintINSERT=1;
privatestaticfinalintDELETE=2;
privatestaticfinalintUPDATE=3;
privatestaticfinalintQUERY=4;
privatePersonSQLiteOpenHelperhelper=null;
static
//添加一组匹配规则
matcher.addURI("
com.hema.db.PersonProvider"
"
insert"
INSERT);
delete"
DELETE);
update"
UPDATE);
query"
QUERY);
//
//content:
//com.hema.db.PersonProvider/insert添加
//com.hema.db.PersonProvider/delete添加
//com.hema.db.PersonProvider/update添加
//com.hema.db.PersonProvider/query添加
//当内容提供者被创建的时候调用适合数据初始化的操作
@Override
publicbooleanonCreate(){
helper=newPersonSQLiteOpenHelper(getContext());
returnfalse;
publicCursorquery(Uriuri,String[]projection,Stringselection,
String[]selectionArgs,StringsortOrder){
if(matcher.match(uri)==QUERY)
//返回查询的结果集
SQLiteDatabasedb=helper.getReadableDatabase();
Cursorcursor=db.query("
projection,selection,selectionArgs,null,null,sortOrder);
returncursor;
else
thrownewIllegalArgumentException("
路径不匹配,不能执行查询"
publicStringgetType(Uriuri){
//TODOAuto-generatedmethodstub
returnnull;
publicUriinsert(Uriuri,ContentValuesvalues){
publicintdelete(Uriuri,Stringselection,String[]selectionArgs){
return0;
publicintupdate(Uriuri,ContentValuesvalues,Stringselection,
String[]selectionArgs){
providerandroid:
authorities="
com.hema.db.personprovider"
/provider>
如何使用内容提供者
ContentResolverresolver=getContentResolver();
使用获得的contentResolver对象对内容提供者进行增删改查的工作
packagecom.xu.providerother;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.content.ContentResolver;
importandroid.view.View;
publicclassMainActivityextendsActivity{
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public