从服务发送光标移动到活动 [英] Send a cursor to an activity from a service

查看:179
本文介绍了从服务发送光标移动到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个服务(一个IntentService)到我的活动(实现一个接收器可以捕捉到该服务的任何响应)发送数据库游标。一旦我从查询光标= db.query获取数据(DBHelper.DB_BOOKS_TABLE,列,其中,whereArgs,GROUPBY,HAVING排序依据); 我想发回从服务给他的呼叫者直通接收机光标。但束结构只是让我把蒂卡尔数据(字符串,整型,认为这样的)。我知道捆也让发送parcelable对象和序列化对象。但我怎么转换成一个典型的光标移动到parcelable或序列化的光标。 thks。

I'm trying to send database cursor from a service(An IntentService) to my Activity(Which implements a Receiver to catch any response from the service). Once i get the data from query cursor = db.query(DBHelper.DB_BOOKS_TABLE, columns, where, whereArgs, groupBy, having, orderBy); i would like to send back the cursor from the service to his caller thru the receiver. but the bundle structure just let me send tical data(Strings, ints, thinks like that). i know the bundle also let send parcelable objects and Serializable Objects. but how do i convert a typical cursor to a parcelable or serializable cursor. thks.

推荐答案

我选择它实现了一个 Parcelable接口做了我自己的自定义类。因为parcelable光标如 CrossProcessCursor 是对我来说很清楚。

I opt for made my own custom class which implements a Parcelable Interface. since the parcelable cursors like CrossProcessCursor is quite unknown for me.

在我自己的ParcelableRow的,我只是执行的HashMap 对象。要不要再担心行或列的数量,我只映射光标移动到我的自己ParcelableRow对象的。这里是MI code:

Inside of my own ParcelableRow i just implement HashMap object. To don't worry anymore about amount of rows or columns, I just map the cursor to my own ParcelableRow object. here is mi code:

public class ParcelableRow implements Parcelable {
 private HashMap<String, String> colsMap;


 public static final Parcelable.Creator<ParcelableRow> CREATOR
 = new Parcelable.Creator<ParcelableRow>() {

    @Override
    public ParcelableRow createFromParcel(Parcel source) {
        return new ParcelableRow(source);
    }

    @Override
    public ParcelableRow[] newArray(int size) {
        return new ParcelableRow[size];
    }
};


public ParcelableRow(Parcel in) {
    colsMap = new HashMap<String, String>();
    readFromParcel(in);
}
public ParcelableRow() {
    colsMap = new HashMap<String, String>();
}
@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    for(String key: colsMap.keySet()){

        dest.writeString(key);
        dest.writeString(colsMap.get(key));

    }

}
public void readFromParcel(Parcel parcel) {

    int limit = parcel.dataSize();
    parcel.setDataPosition(0);
    for(int i = 0; i < limit; i++){
        colsMap.put(parcel.readString(), parcel.readString());
    }

}


public void addNewCol(String colName, String colValue){
    colsMap.put(colName, colValue);
}
public String getColumnValue(String colName){
    return colsMap.get(colName);
}

}
由于@CommonsWare来给了我一个线索。竖起大拇指!

} Thanks to @CommonsWare to gave me a clue. thumbs up!.

我希望这可以成为有用的人,我只是花一些时间设法找到的东西,能够满足我的需求。 这里一些code范例我已经使用。建议都欢迎。

I hope this can be useful to someone, i've just spend some days trying to find something that could satisfy my needs. here are some code examples which i've used. advises are welcome.

警告只满足我,因为我反对的时间。

Warning It only satisfy me because i'm against the time.

请编码!

这篇关于从服务发送光标移动到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆