use of org.xutils.view.annotation.Event in project xUtils3 by wyouflf.
the class HttpFragment method onTest2Click.
// 上传多文件示例
@Event(value = R.id.btn_test2)
private void onTest2Click(View view) {
RequestParams params = new RequestParams("http://192.168.0.13:8080/upload");
// 加到url里的参数, http://xxxx/s?wd=xUtils
params.addQueryStringParameter("wd", "xUtils");
// 添加到请求body体的参数, 只有POST, PUT, PATCH, DELETE请求支持.
// params.addBodyParameter("wd", "xUtils");
// 使用multipart表单上传文件
params.setMultipart(true);
params.addBodyParameter("file", new File("/sdcard/test.jpg"), // 如果文件没有扩展名, 最好设置contentType参数.
null);
try {
params.addBodyParameter("file2", new FileInputStream(new File("/sdcard/test2.jpg")), "image/jpeg", // 测试中文文件名
"你+& \" 好.jpg");
// InputStream参数获取不到文件名, 最好设置, 除非服务端不关心这个参数.
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
x.http().post(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
}
});
}
use of org.xutils.view.annotation.Event 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;
}
}
use of org.xutils.view.annotation.Event in project xUtils3 by wyouflf.
the class ViewInjectorImpl method injectObject.
@SuppressWarnings("ConstantConditions")
private static void injectObject(Object handler, Class<?> handlerType, ViewFinder finder) {
if (handlerType == null || IGNORED.contains(handlerType)) {
return;
}
// 从父类到子类递归
injectObject(handler, handlerType.getSuperclass(), finder);
// inject view
Field[] fields = handlerType.getDeclaredFields();
if (fields != null && fields.length > 0) {
for (Field field : fields) {
Class<?> fieldType = field.getType();
if (/* 不注入静态字段 */
Modifier.isStatic(field.getModifiers()) || /* 不注入final字段 */
Modifier.isFinal(field.getModifiers()) || /* 不注入基本类型字段 */
fieldType.isPrimitive() || /* 不注入数组类型字段 */
fieldType.isArray()) {
continue;
}
ViewInject viewInject = field.getAnnotation(ViewInject.class);
if (viewInject != null) {
try {
View view = finder.findViewById(viewInject.value(), viewInject.parentId());
if (view != null) {
field.setAccessible(true);
field.set(handler, view);
} else {
throw new RuntimeException("Invalid @ViewInject for " + handlerType.getSimpleName() + "." + field.getName());
}
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);
}
}
}
}
// end inject view
// inject event
Method[] methods = handlerType.getDeclaredMethods();
if (methods != null && methods.length > 0) {
for (Method method : methods) {
if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPrivate(method.getModifiers())) {
continue;
}
// 检查当前方法是否是event注解的方法
Event event = method.getAnnotation(Event.class);
if (event != null) {
try {
// id参数
int[] values = event.value();
int[] parentIds = event.parentId();
int parentIdsLen = parentIds == null ? 0 : parentIds.length;
// 循环所有id,生成ViewInfo并添加代理反射
for (int i = 0; i < values.length; i++) {
int value = values[i];
if (value > 0) {
ViewInfo info = new ViewInfo();
info.value = value;
info.parentId = parentIdsLen > i ? parentIds[i] : 0;
method.setAccessible(true);
EventListenerManager.addEventMethod(finder, info, event, handler, method);
}
}
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);
}
}
}
}
// end inject event
}
use of org.xutils.view.annotation.Event in project xUtils3 by wyouflf.
the class DbFragment method onTestDb2Click.
@Event(R.id.btn_test_db2)
private void onTestDb2Click(View view) {
tv_db_result.setText("wait...");
x.task().run(new // 异步执行
Runnable() {
@Override
public void run() {
DbManager db = x.getDb(daoConfig);
String result = "";
List<Parent> parentList = new ArrayList<Parent>();
for (int i = 0; i < 1000; i++) {
Parent parent = new Parent();
parent.setAdmin(true);
parent.setDate(new java.sql.Date(1234));
parent.setTime(new Date());
parent.setEmail(i + "_@qq.com");
parentList.add(parent);
}
long start = System.currentTimeMillis();
for (Parent parent : parentList) {
try {
db.save(parent);
} catch (DbException ex) {
ex.printStackTrace();
}
}
result += "插入1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
start = System.currentTimeMillis();
try {
parentList = db.selector(Parent.class).orderBy("id", true).limit(1000).findAll();
} catch (DbException ex) {
ex.printStackTrace();
}
result += "查找1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
start = System.currentTimeMillis();
try {
db.delete(parentList);
} catch (DbException ex) {
ex.printStackTrace();
}
result += "删除1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
// 批量插入
parentList = new ArrayList<Parent>();
for (int i = 0; i < 1000; i++) {
Parent parent = new Parent();
parent.setAdmin(true);
parent.setDate(new java.sql.Date(1234));
parent.setTime(new Date());
parent.setEmail(i + "_@qq.com");
parentList.add(parent);
}
start = System.currentTimeMillis();
try {
db.save(parentList);
} catch (DbException ex) {
ex.printStackTrace();
}
result += "批量插入1000条数据:" + (System.currentTimeMillis() - start) + "ms\n";
try {
parentList = db.selector(Parent.class).orderBy("id", true).limit(1000).findAll();
db.delete(parentList);
} catch (DbException ex) {
ex.printStackTrace();
}
final String finalResult = result;
x.task().post(new // UI同步执行
Runnable() {
@Override
public void run() {
tv_db_result.setText(finalResult);
}
});
}
});
}
use of org.xutils.view.annotation.Event in project xUtils3 by wyouflf.
the class ImageFragment method onImageItemClick.
@Event(value = R.id.lv_img, type = AdapterView.OnItemClickListener.class)
private void onImageItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this.getActivity(), BigImageActivity.class);
intent.putExtra("url", imageListAdapter.getItem(position).toString());
this.getActivity().startActivity(intent);
}
Aggregations