如何编组/解组ContentValues以将泛型类型插入ContentProvider?

编程入门 行业动态 更新时间:2024-10-24 11:24:10
本文介绍了如何编组/解组ContentValues以将泛型类型插入ContentProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将一个通用POJO放入ContentValues并将其在ContentProvider中解组.

I want to put a generic POJO into ContentValues and unmarshall it within the ContentProvider.

我一直在扭动我的小脑袋:Parcelables,ContentValues,然后插入SQLite 关于: njzk2.wordpress/2013/05/31/map-to-contentvalues-abusing-parcelable/

I've been wracking my tiny brain re: Parcelables, ContentValues, and inserting into SQLite Regarding: njzk2.wordpress/2013/05/31/map-to-contentvalues-abusing-parcelable/

如何编写通用在Android的Sqlite中插入数据的代码

我一直试图通过ContentProvider将android.location.Location插入SQLite:

I've been trying to insert a android.location.Location into SQLite via ContentProvider:

Location loc = mLocationClient.getLastLocation(); myParcel = android.os.Parcel.obtain(); loc.writeToParcel(myParcel, 0); ContentValues values = ContentValues.CREATOR.createFromParcel(myParcel );

使用包裹填充值.

问题1) 这是我的ContentProvider.insert方法:

Question 1) Here is my ContentProvider.insert method:

@Override public Uri insert( final Uri uri, final ContentValues values ){ SQLiteDatabase db = Mydatabase.getWritableDatabase(); //db.insert() doesn’t unmarshal the values?? db.insert( myTABLE_NAME, "", values); Uri result = null; db.close(); return result; }

这失败,因为db.insert()不会解组值(我相信) 插入android.database.sqlite.SQLiteException时出错:INSERT INTO myTABLE_NAME()VALUES(NULL)

this fails because the db.insert() doesn’t unmarshal the values (i believe) Error inserting android.database.sqlite.SQLiteException: INSERT INTO myTABLE_NAME() VALUES (NULL)

问题2) 有什么方法可以先解组值,然后再将其编组到另一个ContentValues变量中?也许有getKey()???

Question 2) Is there some way I can unmarshal values first and then marshal it back into another ContentValues variable? maybe w/ getKey()???

推荐答案

这有效:

HashMap hm = new HashMap(); Location loc = mLocationClient.getLastLocation(); hm.put("LOCATIONS", loc); android.os.Parcel myParcel = android.os.Parcel.obtain(); myParcel.writeMap(hm); myParcel.setDataPosition(0); ContentValues values = ContentValues.CREATOR.createFromParcel(myParcel); getContentResolver().insert(MyUri, values);

然后

@Override public Uri insert( final Uri uri, final ContentValues oldvalues ){ SQLiteDatabase db = GAELdatabase.getWritableDatabase(); Uri result = null; Location loc = (Location)oldvalues.get("LOCATIONS"); ContentValues values = new ContentValues(); values.put("ALTITUDE", loc.getAltitude());//meters above sea level values.put("LATITUDE", loc.getLatitude()); values.put("LONGITUDE", loc.getLongitude()); long rowID = db.insert( "MyTABLE_NAME", "", values); db.close(); return result; }

更多推荐

如何编组/解组ContentValues以将泛型类型插入ContentProvider?

本文发布于:2023-11-22 15:12:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   ContentValues   解组   ContentProvider   将泛型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!