`
kanwoerzi
  • 浏览: 1647196 次
文章分类
社区版块
存档分类
最新评论

Entity简介

 
阅读更多
public final class
Entity
extends Object
java.lang.Object
android.content.Entity
该类是ContentValues数据的集合。它包括一个主数据ContentValues,和子数据的集合。每项子数据由Uri和ContentValues组成。
该类的一个典型应用就是在Contact中,Entity的实例对应一个RawContact,主数据ContentValues对应RawContact的主表数据,
一个子数据对应RawContact的一个子表项,其中Uri对应子表项的Uri(The uri refers to the Data table uri for each row.),
ContentValues对应子表项的数据.
构造函数
publicEntity(ContentValues values)
这里ContentValues values是用来初始话主数据的。
:主数据只能在该构造函数中被初始化,以后不能改变主数据的引用。
主要public函数
void addSubValue(Uri uri, ContentValues values)
加入一个子元素。它会把Uri uri和ContentValues values打包成NamedContentValues然后存储在一个ArrayList<NamedContentValues>中。
ContentValues getEntityValues()
得到主数据的引用。
ArrayList<Entity.NamedContentValues> getSubValues()
得到子数据集合ArrayList<Entity.NamedContentValues>的引用。
注1:我们往往需要重新解包NamedContentValues。但是解包NamedContentValues很简单,直接访问它的两个成员变量(有且仅有)Uri uri和ContentValues values就可以,它们和addSubValue中两变量相对应。
注2:NamedContentValues是Entity的内部类。更多可参照后面附的Entity源码。
public String toString ()
把主数据和子数据都转化为String,以便打印。
附1
Entity的源码
来源http://hi-android.info/src/
Entity.java文件

package android.content;

import android.os.Parcelable;
import android.os.Parcel;
import android.net.Uri;
import android.util.Log;

import java.util.ArrayList;

/**
* A representation of a item using ContentValues. It contains one top level ContentValue
* plus a collection of Uri, ContentValues tuples as subvalues. One example of its use
* is in Contacts, where the top level ContentValue contains the columns from the RawContacts
* table and the subvalues contain a ContentValues object for each row from the Data table that
* corresponds to that RawContact. The uri refers to the Data table uri for each row.
*/
public final classEntity{
final private ContentValuesmValues;
final private ArrayList<NamedContentValues>mSubValues;

publicEntity(ContentValues values){
mValues = values;
mSubValues = new ArrayList<NamedContentValues>();
}

publicContentValuesgetEntityValues(){
return mValues;
}

public ArrayList<NamedContentValues>getSubValues(){
return mSubValues;
}

public voidaddSubValue(Uri uri, ContentValues values){
mSubValues.add(new Entity.NamedContentValues(uri, values));
}

public static classNamedContentValues{
public final Uri uri;
public final ContentValues values;

public NamedContentValues(Uri uri, ContentValues values) {
this.uri = uri;
this.values = values;
}
}

public StringtoString(){
final StringBuilder sb = new StringBuilder();
sb.append("Entity: ").append(getEntityValues());
for (Entity.NamedContentValues namedValue : getSubValues()) {
sb.append("\n ").append(namedValue.uri);
sb.append("\n -> ").append(namedValue.values);
}
return sb.toString();
}
}
<wbr style="line-height:25px"></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics