如何在对象中存储数据并将其保存到SQLite数据库中?

时间:2014-07-22 08:20:20

标签: android sqlite bluetooth

我通过蓝牙从传感器读取数据。我创建了一个DataReceiverClass,它创建了具有我需要的属性的对象,它看起来像这样:

public class DataReceiverClass{

public int heartRate;
public int heartBeatNo;
public long distance;
public long speed;
public long strides;

public DataReceiverClass(int heartRate, int heartBeatNo,long distance, long speed, long strides) {
    this.heartRate = heartRate;
    this.heartBeatNo = heartBeatNo;
    this.distance = distance;
    this.speed = speed;
    this.strides = strides;
}

public int getHeartRate() {
    return heartRate;
}

public void setHeartRate(int heartRate) {
    this.heartRate = heartRate;
}


public int getHeartBeatNo() {
    return heartBeatNo;
}

public void setHeartBeatNo(int heartBeatNo) {
    this.heartBeatNo = heartBeatNo;
}


public long getDistance() {
    return distance;
}

public void setDistance(long distance) {
    this.distance = distance;
}
public long getSpeed() {
    return speed;
}

public void setSpeed(long speed) {
    this.speed = speed;
}

public long getStrides() {
    return strides;
}

public void setStrides(long strides) {
    this.strides = strides;
}

}

这就是我称之为的课程。我使用了一种名为getInfo()的方法。我必须在某处保存大约100个这种类型的对象,然后将它们插入到SQLite数据库中(我猜的是批量插入)。 这是我的主要类代码。有人知道怎么做吗?

public class hxmDemo extends Activity {


private static final String TAG = "hxmDemo";
private TextView mTitle;
private TextView mStatus;
private String mHxMName = null;
private String mHxMAddress = null;
private BluetoothAdapter mBluetoothAdapter = null;
private HxmService mHxmService = null;
private ArrayList<DataReceiverClass> dataReceiverList = new ArrayList<DataReceiverClass>();

private void connectToHxm() {

   //connection stuff
}

private boolean getFirstConnectedHxm() {

 //connection stuff
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  // other stuff
}

@Override
public void onStart() {
    super.onStart();
    //onStart() stuff

}

@Override
public synchronized void onResume() {
    super.onResume();
   //resume stuff
}

private void setupHrm() {

}

@Override
public synchronized void onPause() {
    super.onPause();
}

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onDestroy() {
    super.onDestroy();

}

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case R.string.HXM_SERVICE_MSG_STATE:
                Log.d(TAG, "handleMessage():  MESSAGE_STATE_CHANGE: " + msg.arg1);
                switch (msg.arg1) {
                    case R.string.HXM_SERVICE_CONNECTED:
                        if ((mStatus != null) && (mHxMName != null)) {
                            mStatus.setText(R.string.connectedTo);
                            mStatus.append(mHxMName);
                        }
                        break;

                    case R.string.HXM_SERVICE_CONNECTING:
                        mStatus.setText(R.string.connecting);
                        break;

                    case R.string.HXM_SERVICE_RESTING:
                        if (mStatus != null ) {
                            mStatus.setText(R.string.notConnected);
                        }
                        break;
                }
                break;

            case R.string.HXM_SERVICE_MSG_READ: {

                byte[] readBuf = (byte[]) msg.obj;
                HrmReading hrm = new HrmReading( readBuf );
                hrm.displayRaw();
                dataReceiverList.add(hrm.getInfo());
                break;
            }

            case R.string.HXM_SERVICE_MSG_TOAST:
                Toast.makeText(getApplicationContext(), msg.getData().getString(null),Toast.LENGTH_SHORT).show();
                break;
        }
    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   //  ...
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 // ...
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   // ...
}

public class HrmReading {
   // reading the 5 info from the sensor: heartRate,heartBeatNumber,distance,speed,strides
    }
private DataReceiverClass getInfo(){
    DataReceiverClass dataReceiverClass = new DataReceiverClass(heartRate,heartBeatNumber,distance,speed,strides);
    return dataReceiverClass;

}


private void displayRaw() {
        display ( R.id.heartRate, (int)heartRate );
        display ( R.id.heartBeatNumber,  (int)heartBeatNumber );
        display ( R.id.distance,   distance );
        display ( R.id.speed,   speed );
        display ( R.id.strides,  (int)strides );


    }
    private DataReceiverClass getInfo(){
        DataReceiverClass dataReceiverClass = new DataReceiverClass(heartRate,heartBeatNumber,distance,speed,strides);
        return dataReceiverClass;

    }

}

1 个答案:

答案 0 :(得分:0)

Gson gson = new Gson();
String backUpData = gson.toJson(dataReceiverClass,DataReceiverClass.class);

在此之后,您可以将此String直接插入数据库。