android {
dataBinding {
enabled = true
}
...........
//注意BaseActivity的泛型
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
protected SV bindingView;
//相当于findViewById();
protected T getView(int id) {
return (T) findViewById(id);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindView();
initViews();
}
//实例化BindingView
protected void bindView() {
bindingView = DataBindingUtil.setContentView(this, getLayoutResId());
}
//得到布局
protected abstract int getLayoutResId();
//实例化
public abstract void initViews();
public abstract void processClick(View v);
public void onClick(View v) {
processClick(v);
}
}
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
protected SV bindingView;
private View view;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = getActivity().getLayoutInflater().inflate(setContent(), container, false);
bindingView = DataBindingUtil.bind(view);
initView();
setData();
return view;
}
protected T getView(int id) {
return (T) view.findViewById(id);
}
/**
* 布局
*/
public abstract int setContent();
/**
* 初始化布局
*/
public abstract void initView();
public abstract void setData();
public abstract void processClick(View v);
@Override
public void onClick(View v) {
processClick(v);
}
}
public class WatingModel {
private String id;
private String doctor_name;
private String patient;
private String department_name;
private String department_area;
private String wait;
private String nowid;
private String nowtime;
private String go_time;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private String status;
public String getNowtime() {
return nowtime;
}
public void setNowtime(String nowtime) {
this.nowtime = nowtime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDoctor_name() {
return doctor_name;
}
public void setDoctor_name(String doctor_name) {
this.doctor_name = doctor_name;
}
public String getPatient() {
return patient;
}
public void setPatient(String patient) {
this.patient = patient;
}
public String getDepartment_name() {
return department_name;
}
public void setDepartment_name(String department_name) {
this.department_name = department_name;
}
public String getDepartment_area() {
return department_area;
}
public void setDepartment_area(String department_area) {
this.department_area = department_area;
}
public String getWait() {
return wait;
}
public void setWait(String wait) {
this.wait = wait;
}
public String getNowid() {
return nowid;
}
public void setNowid(String nowid) {
this.nowid = nowid;
}
public String getGo_time() {
return go_time;
}
public void setGo_time(String go_time) {
this.go_time = go_time;
}
}
几点注意的地方:
public class WaitingDetailsActivity extends BaseActivity {//binding名字是布局名字依照驼峰命名法书写+Binding决定
private TextView title;
private Dialog progressDialog;
private String card_id, id;
private WatingModel.Data model;//实体类
@Override
protected int getLayoutResId() {
return R.layout.activity_waiting;//布局
}
@Override
public void initViews() {
//加载圈
progressDialog = LabelUtils.createLoadingDialog(WaitingDetailsActivity.this, "加载中...", true, 0);
card_id = getIntent().getStringExtra("card_id");
id = getIntent().getStringExtra("id");
title = getView(R.id.title);//对于引入的布局bindingView.无法直接获取其子控件;
getView(R.id.back).setOnClickListener(this);
title.setText("标题");
//bingView.可直接获取控件对象,无需fandViewById();
bindingView.refresh.setOnClickListener(this);
getData();
}
@Override
public void processClick(View v) {
switch (v.getId()) {
case R.id.back:
finish();
break;
}
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
progressDialog.dismiss();
if (msg.what == 1) {
String result = msg.obj.toString();
try {
JSONObject json = new JSONObject(result);
String code = json.getString("code");
boolean status = json.getBoolean("status");
String info = json.getString("info");
if (status) {
JSONArray array = json.getJSONArray("data");
JSONObject jsonObject = array.getJSONObject(0);
Gson gson = new Gson();
model = gson.fromJson(jsonObject.toString(), WatingModel.Data.class);
bindingView.setModel(model);//别忘记这句话,请求到数据后写,很重要,只有在布局中写到实体类,在代码中才有这个方法,方法名是你布局中的name;
} else {
LabelUtils.toast(WaitingDetailsActivity.this, code + info);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else if (msg.what == 3) {
LabelUtils.toast(WaitingDetailsActivity.this, getResources().getString(R.string.quest_fail));
}
return false;
}
});
public void getData() {
progressDialog.show();
HashMap map = new HashMap<>();
map.put("card_id", card_id + "");
map.put("member_id", LoginDataModel.getLoginData().getMember_id() + "");
OkhttpTool.getNetData("请求地址", map).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
if (response.isSuccessful()) {
Message msg = new Message();
msg.what = 1;
msg.obj = result;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
});
}
}