Java 领域数据库未更新
嗨,朋友们,请帮助我,我正在制作聊天应用程序,可以转换文本和图像,每当用户更改其个人资料中的图像时,它也需要在转换中更新,但不会更新,但每当我发送新消息时,新图像就会更新,而且我没有设置主键 下面是我尝试过的链接,没有任何效果:Java 领域数据库未更新,java,android,realm,Java,Android,Realm,嗨,朋友们,请帮助我,我正在制作聊天应用程序,可以转换文本和图像,每当用户更改其个人资料中的图像时,它也需要在转换中更新,但不会更新,但每当我发送新消息时,新图像就会更新,而且我没有设置主键 下面是我尝试过的链接,没有任何效果: public class Chat\u历史记录扩展了AppCompative活动{ @BindView(R.id.chat\u history\u工具栏) 自定义工具栏聊天历史工具栏; @BindView(R.id.sendmessgae) 自定义编辑文本发送消息;
public class Chat\u历史记录扩展了AppCompative活动{
@BindView(R.id.chat\u history\u工具栏)
自定义工具栏聊天历史工具栏;
@BindView(R.id.sendmessgae)
自定义编辑文本发送消息;
@BindView(R.id.chat\u history\u列表)
ListView聊天记录列表;
聊天室\历史\适配器聊天室适配器;
RealmResults chatthistorylist;
境界与境界;
广播接收机;
public static final String BroadCastAction=“com.parallelspace.parallelspace.Chat\u history”;
RealmChangeListener ChatUpdateListener=新的RealmChangeListener(){
@凌驾
公共void onChange(对象元素){
addmessage(chathistorylist);
}
};
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_历史);
把(这个)绑起来;
init(getApplicationContext());
设置支持操作栏(聊天记录工具栏);
断言getSupportActionBar()!=null;
**chatrealm=Realm.getDefaultInstance();
chatrealm.beginTransaction();
RealmQuery getloginda=chatrealm.where(Chat_history_pojo.class).beginGroup().equalTo(“receiverid”,getIntent().getStringExtra(“rid”).endGroup();
RealmResults registrationRealm=getLoginData.findAll();
用于(聊天室、历史记录、注册领域){
chtapojo.setReceiver_配置文件(getIntent().getStringExtra(“笠美”));
copyToRealm(注册领域);
}
chatrealm.commitTransaction()**
chathistorylist=chatrealm.where(Chat\u history\u pojo.class).beginGroup()
.equalTo(“senderid”,Session.getUserID(getApplicationContext()))
.equalTo(“接收方”,getIntent().getStringExtra(“rid”))
.endGroup()
.或()
.beginGroup()
.equalTo(“receiverid”,Session.getUserID(getApplicationContext()))
.equalTo(“senderid”,getIntent().getStringExtra(“rid”))
.endGroup()
.findAll();
chathisadapter=新的Chat\u history\u适配器(Chat\u history.this,chathistorylist);
chat_history_list.setStackFromBottom(true);
聊天历史记录列表。设置转录模式(聊天历史记录列表。转录模式正常);
chat_history_list.setAdapter(chathisadapter);
/*chatrealm.executeTransaction(新领域.Transaction(){
@凌驾
公共void执行(领域){
常量.l(“称为域更新”);
RealmResults updatehispojo=chatrealm.where(Chat_history_pojo.class).equalTo(“receiverid”,getIntent().getStringExtra(“rid”)).findAll();
用于(聊天室\历史\ pojo updatepojo:updatehispojo){
updatepojo.setReceiver_配置文件(getIntent().getStringExtra(“笠美”));
insertOrUpdate(updatepojo);
chathisadapter.notifyDataSetChanged();
}
}
});*/
setDrawableClickListener(新的DrawableClickListener(){
@凌驾
点击公共空区(DrawablePosition目标){
交换机(目标){
案例权利:
if(sendmessgae.getText().toString().isEmpty()){
常量.t(getApplicationContext(),“请输入消息”);
}否则{
sendmessage();
}
打破
违约:
打破
}
}
});
if(getIntent().hasExtra(“mtype”)){
聊天记录工具栏工具栏图标(getApplicationContext(),getIntent().getStringExtra(“笠美”));
chat_history_toolbar.toolbartext(getIntent().getStringExtra(“rname”);
}否则{
聊天记录工具栏工具栏图标(getApplicationContext(),getIntent().getStringExtra(“笠美”));
chat_history_toolbar.toolbartext(getIntent().getStringExtra(“rname”);
}
chat\u history\u toolbar.which单击(“后退”,新建视图。OnClickListener()){
@凌驾
公共void onClick(视图){
removepushnotification(getApplicationContext());
Intent chathistorycontent=新Intent(getApplicationContext(),Chadim\u Home.class);
ChatHistoryContent.addFlags(意图、标志、活动、清除、顶部);
startActivity(聊天历史内容);
}
});
chat\u history\u toolbar.which单击(“toolbar”,new View.OnClickListener()){
@凌驾
公共void onClick(视图){
Intent chathisintent=新的Intent(getApplicationContext(),Detailed\u friend.class);
chathisintent.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
chathisintent.putExtra(“rname”,getIntent().getStringExtra(“rname”);
chathisintent.putExtra(“笠美”),getIntent().getStringExtra(“笠美”);
getIntent().getStringExtra(“rid”);
星触觉(chathisintent);
}
});
chat\u history\u工具栏。其中单击(“删除”,新建视图。OnClickListener()){
@凌驾
公共void onClick(视图v){
chatrealm.executeTransaction(新领域.Transaction(){
@凌驾
公共无效执行(领域r)
public class Chat_history extends AppCompatActivity {
@BindView(R.id.chat_history_toolbar)
CustomToolbar chat_history_toolbar;
@BindView(R.id.sendmessgae)
CustomEditText sendmessgae;
@BindView(R.id.chat_history_list)
ListView chat_history_list;
Chat_history_Adapter chathisadapter;
RealmResults<Chat_history_pojo> chathistorylist;
Realm chatrealm;
BroadcastReceiver chatreceiver;
public static final String BroadCastAction = "com.parallelspace.parallelspace.Chat_history";
RealmChangeListener chatupdatelisterner = new RealmChangeListener() {
@Override
public void onChange(Object element) {
chathisadapter.addmessage(chathistorylist);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_history);
ButterKnife.bind(this);
Realm.init(getApplicationContext());
setSupportActionBar(chat_history_toolbar);
assert getSupportActionBar() != null;
**chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
RealmQuery<Chat_history_pojo> getLoginData= chatrealm.where(Chat_history_pojo.class).beginGroup().equalTo("receiverid", getIntent().getStringExtra("rid")).endGroup();
RealmResults<Chat_history_pojo> registrationRealm = getLoginData.findAll();
for (Chat_history_pojo chtapojo:registrationRealm) {
chtapojo.setReciever_profile(getIntent().getStringExtra("rimage"));
chatrealm.copyToRealm(registrationRealm);
}
chatrealm.commitTransaction();**
chathistorylist = chatrealm.where(Chat_history_pojo.class).beginGroup()
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.endGroup()
.or()
.beginGroup()
.equalTo("receiverid", Session.getUserID(getApplicationContext()))
.equalTo("senderid", getIntent().getStringExtra("rid"))
.endGroup()
.findAll();
chathisadapter = new Chat_history_Adapter(Chat_history.this, chathistorylist);
chat_history_list.setStackFromBottom(true);
chat_history_list.setTranscriptMode(chat_history_list.TRANSCRIPT_MODE_NORMAL);
chat_history_list.setAdapter(chathisadapter);
/*chatrealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Constant.l("Realm Update Called");
RealmResults<Chat_history_pojo> updatehispojo = chatrealm.where(Chat_history_pojo.class).equalTo("receiverid", getIntent().getStringExtra("rid")).findAll();
for(Chat_history_pojo updatepojo:updatehispojo){
updatepojo.setReciever_profile(getIntent().getStringExtra("rimage"));
chatrealm.insertOrUpdate(updatepojo);
chathisadapter.notifyDataSetChanged();
}
}
});*/
sendmessgae.setDrawableClickListener(new DrawableClickListener() {
@Override
public void onClick(DrawablePosition target) {
switch (target) {
case RIGHT:
if (sendmessgae.getText().toString().isEmpty()) {
Constant.t(getApplicationContext(), "Please Enter Message");
} else {
sendmessage();
}
break;
default:
break;
}
}
});
if (getIntent().hasExtra("mtype")) {
chat_history_toolbar.toolbaricon(getApplicationContext(), getIntent().getStringExtra("rimage"));
chat_history_toolbar.toolbartext(getIntent().getStringExtra("rname"));
} else {
chat_history_toolbar.toolbaricon(getApplicationContext(), getIntent().getStringExtra("rimage"));
chat_history_toolbar.toolbartext(getIntent().getStringExtra("rname"));
}
chat_history_toolbar.Whichonclicked("back", new View.OnClickListener() {
@Override
public void onClick(View view) {
Session.removepushnotification(getApplicationContext());
Intent chathistoryintent = new Intent(getApplicationContext(), Chadim_Home.class);
chathistoryintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(chathistoryintent);
}
});
chat_history_toolbar.Whichonclicked("toolbar", new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chathisintent = new Intent(getApplicationContext(), Detailed_friend.class);
chathisintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
chathisintent.putExtra("rname", getIntent().getStringExtra("rname"));
chathisintent.putExtra("rimage", getIntent().getStringExtra("rimage"));
chathisintent.putExtra("rid", getIntent().getStringExtra("rid"));
startActivity(chathisintent);
}
});
chat_history_toolbar.Whichonclicked("delete", new View.OnClickListener() {
@Override
public void onClick(View v) {
chatrealm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<Chat_history_pojo> deleteresults = realm.where(Chat_history_pojo.class).beginGroup()
.equalTo("senderid", Session.getUserID(getApplicationContext()))
.equalTo("receiverid", getIntent().getStringExtra("rid"))
.endGroup()
.or()
.beginGroup()
.equalTo("receiverid", Session.getUserID(getApplicationContext()))
.equalTo("senderid", getIntent().getStringExtra("rid"))
.endGroup()
.findAll();
deleteresults.deleteAllFromRealm();
}
});
}
});
chatreceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(getIntent().getStringExtra("rid"));
chatupdatepojo.setReceiverid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setSender_profile(Session.getUserimage(getApplicationContext()));
chatupdatepojo.setReciever_profile(getIntent().getStringExtra("rimage"));
chatupdatepojo.setMessage(intent.getStringExtra("msg").replace("$", " "));
chatupdatepojo.setType("text");
if (intent.getStringExtra("type").equals("image")) {
chatupdatepojo.setType("image");
} else if (intent.getStringExtra("type").equals("text")) {
chatupdatepojo.setType("text");
}
chatrealm.commitTransaction();
chatrealm.close();
}
};
IntentFilter intfil = new IntentFilter(BroadCastAction);
registerReceiver(chatreceiver, intfil);
}
@Override
protected void onDestroy() {
unregisterReceiver(chatreceiver);
super.onDestroy();
}
@Override
protected void onStart() {
super.onStart();
chatrealm.addChangeListener(chatupdatelisterner);
Session.pushnotification(getApplicationContext());
}
@Override
protected void onStop() {
super.onStop();
chatrealm.removeChangeListener(chatupdatelisterner);
Session.removepushnotification(getApplicationContext());
}
@OnClick(R.id.camera)
public void camera() {
TedBottomPicker bottomSheetDialogFragment = new TedBottomPicker.Builder(Chat_history.this)
.setOnImageSelectedListener(new TedBottomPicker.OnImageSelectedListener() {
@Override
public void onImageSelected(Uri uri) {
try {
Bitmap sendimage = MediaStore.Images.Media.getBitmap(Chat_history.this.getContentResolver(), uri);
sendimages(Session.getUserID(getApplicationContext()), getIntent().getStringExtra("rid"), sendimage);
} catch (IOException e) {
Constant.l(e.toString());
}
}
})
.setPeekHeight(getResources().getDisplayMetrics().heightPixels / 2)
.create();
bottomSheetDialogFragment.show(getSupportFragmentManager());
}
private void sendimages(String sid, String rid, Bitmap sendbitmap) {
Constant.showloader(Chat_history.this);
String sendumageurl = Constant.psurl + "chatdocument&task=send&sender_id=" + sid + "&reciever_id=" + rid;
Constant.l(sendumageurl);
AndroidNetworking.post(sendumageurl).addBodyParameter("image", Constant.getStringImage(sendbitmap)).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
Constant.l(String.valueOf(response));
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setReceiverid(getIntent().getStringExtra("rid"));
chatupdatepojo.setSender_profile(Session.getUserimage(getApplicationContext()));
chatupdatepojo.setReciever_profile(getIntent().getStringExtra("rimage"));
chatupdatepojo.setMessage(response.getString("url"));
chatupdatepojo.setType("image");
chatrealm.commitTransaction();
chatrealm.close();
}
} catch (JSONException e) {
Constant.l(e.toString());
Constant.dismissloader();
}
Constant.dismissloader();
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
Constant.dismissloader();
}
});
}
@Override
public void onBackPressed() {
Session.removepushnotification(getApplicationContext());
Intent chathistoryintent = new Intent(getApplicationContext(), Chadim_Home.class);
chathistoryintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(chathistoryintent);
}
private void sendmessage() {
String schatsendmesg = Constant.psurl + "chat&task=send&sender_id=" + Session.getUserID(getApplicationContext()) + "&reciever_id=" + getIntent().getStringExtra("rid") + "&message=" + sendmessgae.getText().toString().replace(" ", "$");
Constant.l(schatsendmesg);
AndroidNetworking.get(schatsendmesg).setOkHttpClient(Constant.okClient()).build().getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
try {
if (response.getString("status").equals("Success")) {
chatrealm = Realm.getDefaultInstance();
chatrealm.beginTransaction();
Chat_history_pojo chatupdatepojo = chatrealm.createObject(Chat_history_pojo.class);
chatupdatepojo.setSenderid(Session.getUserID(getApplicationContext()));
chatupdatepojo.setReceiverid(getIntent().getStringExtra("rid"));
chatupdatepojo.setMessage(sendmessgae.getText().toString());
chatupdatepojo.setSender_profile(Session.getUserimage(getApplicationContext()));
chatupdatepojo.setReciever_profile(getIntent().getStringExtra("rimage"));
chatupdatepojo.setType("text");
chatrealm.commitTransaction();
chatrealm.close();
sendmessgae.setText("");
}
} catch (JSONException e) {
Constant.l(e.toString());
}
}
@Override
public void onError(ANError anError) {
Constant.l(anError.toString());
}
});
}
@Override
protected void onResume() {
super.onResume();
Session.pushnotification(getApplicationContext());
}
@Override
protected void onPause() {
super.onPause();
Session.removepushnotification(getApplicationContext());
}
}
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
//Here i am checking the condition
RealmQuery<RegistrationRealm> getLoginData= realm.where(RegistrationRealm.class).equalTo("id",1);
//finding the first set if matches the criteria
RegistrationRealm registrationRealm = getLoginData.findFirst();
//setting the values
registrationRealm.setFirstname(firstname);
registrationRealm.setLastname(lastname);
//Updating the same object with new values
realm.copyToRealm(registrationRealm);
//commiting the current transaction
realm.commitTransaction();
RealmResults<RegistrationRealm> registrationRealm = getLoginData.findAll();