-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleActivity.java
147 lines (132 loc) · 5.75 KB
/
SimpleActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.reone.kvstorage;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.reone.kvstoragelib.KVStorage;
import java.text.MessageFormat;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class SimpleActivity extends AppCompatActivity {
@BindView(R.id.et_demo_key)
EditText etDemoKey;
@BindView(R.id.et_demo_value)
EditText etDemoValue;
@BindView(R.id.tv_result)
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
ButterKnife.bind(this);
}
@OnClick({R.id.btn_save, R.id.btn_get, R.id.btn_delete, R.id.btn_get_all, R.id.btn_clear})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_save:
String demoKey = getEditText(etDemoKey);
String demoValue = getEditText(etDemoValue);
if (TextUtils.isEmpty(demoKey)) {
showShortToast("demo Key值为空");
}
if (TextUtils.isEmpty(demoValue)) {
showShortToast("demo value值为空");
}
KVStorage.rxSave(demoKey, demoValue)
.subscribe(new AsyncObserver<Boolean>() {
@Override
public void onSuccess(Boolean result) {
showShortToast("储存成功,返回:" + result);
}
@Override
public void onError(Throwable e) {
showShortToast("储存失败," + e.getMessage());
tvResult.setText(e.getMessage());
}
});
break;
case R.id.btn_get:
demoKey = getEditText(etDemoKey);
KVStorage.rxGet(demoKey)
.subscribe(new AsyncObserver<String>() {
@Override
public void onSuccess(String result) {
showShortToast("查找成功,返回:" + result);
tvResult.setText(result);
}
@Override
public void onError(Throwable e) {
showShortToast("查找失败," + e.getMessage());
tvResult.setText(e.getMessage());
}
});
break;
case R.id.btn_delete:
demoKey = getEditText(etDemoKey);
KVStorage.rxRemove(demoKey)
.subscribe(new AsyncObserver<Integer>() {
@Override
public void onSuccess(Integer result) {
showShortToast("删除成功,返回:" + result);
tvResult.setText(MessageFormat.format("delete {0}line", result));
}
@Override
public void onError(Throwable e) {
showShortToast("删除失败," + e.getMessage());
tvResult.setText(e.getMessage());
}
});
break;
case R.id.btn_get_all:
KVStorage.rxGetAllKeys()
.subscribe(new AsyncObserver<List<String>>() {
@Override
public void onSuccess(List<String> result) {
showShortToast("获取成功");
StringBuilder sb = new StringBuilder();
int count = result.size();
for (int i = 0; i < count; i++) {
sb.append(result.get(i));
sb.append("\n");
}
tvResult.setText(sb.toString());
}
@Override
public void onError(Throwable e) {
showShortToast("获取失败," + e.getMessage());
tvResult.setText(e.getMessage());
}
});
break;
case R.id.btn_clear:
KVStorage.rxClear()
.subscribe(new AsyncObserver<Integer>() {
@Override
public void onSuccess(Integer result) {
showShortToast("清除成功");
tvResult.setText(MessageFormat.format("clear {0}line", result));
}
@Override
public void onError(Throwable e) {
showShortToast("清除失败," + e.getMessage());
tvResult.setText(e.getMessage());
}
});
break;
}
}
private String getEditText(EditText editText) {
try {
return editText.getText().toString().trim();
} catch (Exception ignore) {
}
return "";
}
private void showShortToast(String talk) {
MyApplication.talk(talk);
}
}