gpt4 book ai didi

java - 如何在android中更新realm

转载 作者:行者123 更新时间:2023-12-02 05:25:15 24 4
gpt4 key购买 nike

我有 RealmObject 类,其中状态列现在存在,我想更新该状态列值尽管我查看了 Realm 官方博客,但我不知道如何执行此操作,但没有太大帮助。

public void storeDevice() {
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(Login.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
}
if (telephonyManager != null) {
Log.e("imeino", telephonyManager.getDeviceId());
}

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = null;
if (wifiManager != null) {
wInfo = wifiManager.getConnectionInfo();
}
String macAddress = "02:00:00:00:00:00";
try {
if (wInfo != null) {
macAddress = wInfo.getMacAddress();
}
Log.e("device_mac", macAddress);
} catch (Exception e) {
}

mobile = edit_mobile.getText().toString().trim();
imei_no = telephonyManager.getDeviceId();
brand = Build.MANUFACTURER;
device_model = Build.MODEL;
mac = macAddress;
device_version = Build.VERSION.RELEASE;

realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
DeviceModel deviceModel = realm.createObject(DeviceModel.class);
RealmResults<DeviceModel> deviceModels = realm.where(DeviceModel.class).equalTo("imei_no", imei_no).findAll();
deviceModels.deleteAllFromRealm();

deviceModel.setMobile(mobile);
deviceModel.setImei_no(imei_no);
deviceModel.setStatus("1");
deviceModel.setBg_device_brand(brand);
deviceModel.setDevice_mac(mac);
deviceModel.setBg_device_model(device_model);

}
});
}

这里我想更新 if block 内 OnResponse 方法中的值

private void storeCustomerData(Map<String, String> map, HashMap<String, Integer> map1) {

Call<JsonElement> call = apiInterface.customerData(map, map1);
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
JsonElement jsonElement = response.body();
Log.i("jsonElement", "data");
JsonObject jsonObject = jsonElement.getAsJsonObject();
String status = jsonObject.getAsJsonPrimitive("Status").toString();
Log.i("status", status);
if (status.equals("1")) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<DeviceModel> results = realm.where(DeviceModel.class).equalTo("mobile", realm_mobile).findAll();
results.setValue("status", "5");
deviceModel.getStatus();
Log.i("status",deviceModel.getStatus());
}
});

Toast.makeText(RequiredData.this, "Your Data Has Been Succesfully Added", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RequiredData.this, MainActivity.class);
startActivity(intent);

} else {
Toast.makeText(RequiredData.this, "Sorry, Unsucess to Save Data", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<JsonElement> call, Throwable t) {

}
});
}

最佳答案

您可以查看official documentation并搜索“insertOrUpdate”。您必须定义主键才能使用 insertOrUpdate。在您的情况下,主键将为“imei_no”。

例如,这是我的 UserInfo 对象类。

public class UserInfo extends RealmObject {

@PrimaryKey
private String userId;
private String userAge;
private String userSex;

public UserInfo() {

}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserId() {
return userId;
}


public String getUserAge() {
return userAge;
}

public void setUserAge(String userAge) {
this.userAge = userAge;
}

public String getUserSex() {
return userSex;
}

public void setUserSex(String userSex) {
this.userSex = userSex;
}

}

并插入或更新 UserInfo 对象,如下所示:

mRealm = Realm.getDefaultInstance();

if (!mRealm.isInTransaction()) {
mRealm.beginTransaction();
}

UserInfo userInfo = new UserInfo();
userInfo.setUserId("135455");
userInfo.setUserAge("22");
userInfo.setUserSex("male");

mRealm.insertOrUpdate(userInfo);
mRealm.commitTransaction();

关于java - 如何在android中更新realm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com