安卓(Android) 怎么从Context获取实体(Entity)

2019年10月18日22:00:59安卓(Android) 怎么从Context获取实体(Entity)已关闭评论 382 1581字阅读5分16秒

1.简介

安卓(Android) 怎么从Context获取实体(Entity)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 


import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;

public class Main {
    public static ContentValues getEntity(Context context, Uri uri,
            String[] projection) {
        return getEntity(context.getContentResolver(), uri, projection);
    }

    public static ContentValues getEntity(ContentResolver cr, Uri uri,
            String[] projection) {
        return getEntity(cr, uri, projection, null, null);
    }

    public static ContentValues getEntity(Context context, Uri uri,
            String[] projection, String selection, String[] selectionArgs) {
        return getEntity(context.getContentResolver(), uri, projection,
                selection, selectionArgs);
    }

    public static ContentValues getEntity(ContentResolver cr, Uri uri,
            String[] projection, String selection, String[] selectionArgs) {
        return getEntity(cr, uri, projection, selection, selectionArgs,
                null);
    }

    public static ContentValues getEntity(Context context, Uri uri,
            String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        return getEntity(context.getContentResolver(), uri, projection,
                selection, selectionArgs, sortOrder);
    }

    public static ContentValues getEntity(ContentResolver cr, Uri uri,
            String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        ContentValues values = null;
        Cursor cursor = null;
        try {
            cursor = cr.query(uri, projection, selection, selectionArgs,
                    sortOrder);
            if (cursor.moveToNext()) {
                values = new ContentValues();
                DatabaseUtils.cursorRowToContentValues(cursor, values);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return values;
    }
}

 
继续阅读
编程十万个怎么办