Search in sources :

Example 1 with Doctor

use of com.shiqkuangsan.mycustomviews.bean.Doctor in project CustomViews by AndroidStudy233.

the class RealmTestActivity method onClick.

@Event(value = { R.id.btn_realm_insert, R.id.btn_realm_query, R.id.btn_realm_queryAll, R.id.btn_realm_delete })
private void onClick(View view) {
    switch(view.getId()) {
        case R.id.btn_realm_insert:
            String name = et_name.getText().toString().trim();
            String age = et_age.getText().toString().trim();
            String hospital = et_hospital.getText().toString().trim();
            String skill = et_skill.getText().toString().trim();
            if (!isAllInputValid(name, age, hospital, skill)) {
                ToastUtil.toastShort(this, "以上信息有格式不对,请核对");
                return;
            }
            final Doctor doctor = new Doctor(name, age, hospital, skill);
            realm.executeTransaction(new Realm.Transaction() {

                @Override
                public void execute(Realm realm) {
                    realm.copyToRealmOrUpdate(doctor);
                    refreshUI();
                    ToastUtil.toastShort(RealmTestActivity.this, "插入成功");
                    InputMethodUtil.closeSoftKeyboard(RealmTestActivity.this);
                }
            });
            break;
        case R.id.btn_realm_query:
            final String name2 = et_name.getText().toString().trim();
            if (isNameValid(name2))
                realm.executeTransaction(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        Doctor doc = realm.where(Doctor.class).equalTo("name", name2).findFirst();
                        if (doc != null) {
                            et_name.setText(doc.getName());
                            et_age.setText(doc.getAge());
                            et_hospital.setText(doc.getHospital());
                            et_skill.setText(doc.getSkill());
                            tv_result.setText("查询结果: " + "\t" + "Doctor: " + doc.getName());
                            InputMethodUtil.closeSoftKeyboard(RealmTestActivity.this);
                        } else
                            ToastUtil.toastShort(RealmTestActivity.this, "尚未存储");
                    }
                });
            else
                ToastUtil.toastShort(RealmTestActivity.this, "无效的名称");
            break;
        case R.id.btn_realm_queryAll:
            RealmResults<Doctor> doctors = realm.where(Doctor.class).findAll();
            if (doctors.size() > 0) {
                String text = "查询结果: ";
                for (Doctor doc : doctors) {
                    refreshUI();
                    text += "Doctor: " + doc.getName() + "\n";
                }
                tv_result.setText(text);
                InputMethodUtil.closeSoftKeyboard(RealmTestActivity.this);
            } else
                ToastUtil.toastShort(RealmTestActivity.this, "尚无数据");
            break;
        case R.id.btn_realm_delete:
            final String name3 = et_name.getText().toString().trim();
            if (isNameValid(name3))
                realm.executeTransaction(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        Doctor doc = realm.where(Doctor.class).equalTo("name", name3).findFirst();
                        if (StringUtil.isEmpty(doc.getName()))
                            ToastUtil.toastShort(RealmTestActivity.this, "尚未存储");
                        else {
                            doc.deleteFromRealm();
                            refreshUI();
                            ToastUtil.toastShort(RealmTestActivity.this, "删除成功");
                            InputMethodUtil.closeSoftKeyboard(RealmTestActivity.this);
                        }
                    }
                });
            else
                ToastUtil.toastShort(RealmTestActivity.this, "无效的名称");
            break;
    }
}
Also used : Doctor(com.shiqkuangsan.mycustomviews.bean.Doctor) Realm(io.realm.Realm) Event(org.xutils.view.annotation.Event)

Aggregations

Doctor (com.shiqkuangsan.mycustomviews.bean.Doctor)1 Realm (io.realm.Realm)1 Event (org.xutils.view.annotation.Event)1