package com.casic.gasoperation; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Message; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.casic.gasoperation.config.ApiClent; import com.casic.gasoperation.utils.BaseThread; import com.casic.gasoperation.utils.HandlerStatus; import com.casic.gasoperation.utils.Msg; import com.casic.gasoperation.utils.MsgCallback; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class DeviceManagerActivity extends BaseActivity { private View view = null; private LinearLayout wr_areas; private BaseThread thread; private EditText searchText; private ImageView search,add,back; public int clicknum = 880000; public int editnum = 770000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.device_list); view = getLayoutInflater().inflate(R.layout.device_list, null); search = (ImageView) findViewById(R.id.search); search.setOnClickListener(onClickListener); add = (ImageView) findViewById(R.id.add); add.setOnClickListener(onClickListener); back = (ImageView) findViewById(R.id.back); back.setOnClickListener(onClickListener); wr_areas=(LinearLayout) findViewById(R.id.wr_areas); searchText = (EditText) findViewById(R.id.search_adress); searchText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if(searchText.getText().equals("")){ getDeviceList(); } } }); getDeviceList(); } private View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View arg0) { if (arg0 == search) { getDeviceList(); } else if(arg0 == add){ Intent intent = new Intent(DeviceManagerActivity.this, AddDeviceActivity.class); startActivity(intent); } else if(arg0 == back){ finish(); } } }; public void getDeviceList() { wr_areas.removeAllViews(); thread = new BaseThread(baseHandler) { @Override public void runThread() { final Message msg = thread.obtainMessage(); if (msg != null) { msg.what = HandlerStatus.HANDLE_DEVICEMANAGER; try { ApiClent.deviceManagerList(searchText.getText().toString(),1,100,new ApiClent.ClientCallback() { @Override public void onSuccess(Object data) { try { JSONObject jsonObject = new JSONObject(data.toString()); JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("rows"); if (jsonArray.length()== 0) { Msg.showInfo(DeviceManagerActivity.this, "未查询到任何项"); return ; }else{ msg.arg1 = HandlerStatus.HANDLE_OK; msg.obj = jsonArray; } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onFailure(String message) { msg.arg1 = HandlerStatus.HANDLE_FAIL; } @Override public void onError(Exception e) { msg.arg1 = HandlerStatus.HANDLE_ERROR; } }); } catch (Exception e) { msg.arg1 = HandlerStatus.HANDLE_ERROR; e.printStackTrace(); } thread.sendMessage(msg); } } }; showProgress("设备列表查询中。。。", "设备列表"); thread.start(); } @Override protected void onCancelProgress(DialogInterface arg0) { if (thread != null) { thread.interrupt(); } DeviceManagerActivity.this.finish(); } public void deleteDevice(View view, JSONArray json) { final View v = view; final JSONArray jsonArray = json; Msg.confirm(DeviceManagerActivity.this, "确认删除该设备吗?", "删除设备", new MsgCallback() { @Override public void callBack(Boolean result) { if (result != null && result.booleanValue()) { int i = (Integer) v.getTag(); //这里的i不能在外部定义,因为内部类的关系,内部类好多繁琐的东西,要好好研究一番 try { ApiClent.deleteDevice(jsonArray.getJSONObject(i-clicknum).getString("id"),new ApiClent.ClientCallback() { @Override public void onSuccess(Object data) { getDeviceList(); } @Override public void onFailure(String message) { Msg.showInfo(DeviceManagerActivity.this, message); } @Override public void onError(Exception e) { Msg.showInfo(DeviceManagerActivity.this, "删除设备异常!"); } }); }catch (JSONException e){ e.printStackTrace(); } } } }); } @Override protected void handleMessage(Message msg) { if (msg.what == HandlerStatus.HANDLE_DEVICEMANAGER) { if (msg.arg1 == HandlerStatus.HANDLE_OK) { if (msg.obj != null) { try { final JSONArray jsonArray = (JSONArray) msg.obj; for(int i=0;i<jsonArray.length();i++){ JSONObject json = jsonArray.getJSONObject(i); LinearLayout llWashingRoomItem = new LinearLayout(view.getContext()); llWashingRoomItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); llWashingRoomItem = (LinearLayout) getLayoutInflater().inflate(R.layout.table_view4, null); TextView name = (TextView) llWashingRoomItem.findViewById(R.id.name); TextView status = (TextView) llWashingRoomItem.findViewById(R.id.status); TextView code = (TextView) llWashingRoomItem.findViewById(R.id.code); ImageView edit = (ImageView) llWashingRoomItem.findViewById(R.id.edit); ImageView delete = (ImageView) llWashingRoomItem.findViewById(R.id.delete); name.setText(json.getString("jobName")); status.setText(json.getString("status")); code.setText(json.getString("deviceGroup")); delete.setTag(clicknum+i); delete.setClickable(true); delete.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { deleteDevice(v,jsonArray); } }); edit.setTag(editnum+i); edit.setClickable(true); edit.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { int i = (Integer) v.getTag(); //这里的i不能在外部定义,因为内部类的关系,内部类好多繁琐的东西,要好好研究一番 Intent intent = new Intent(); intent.setClass(DeviceManagerActivity.this, EditDeviceActivity.class); Bundle bundle = new Bundle(); try { bundle.putString("devcode", jsonArray.getJSONObject(i-editnum).getString("deviceGroup")); }catch (JSONException e){ e.printStackTrace(); } intent.putExtras(bundle); startActivityForResult(intent,0x006); } }); wr_areas.addView(llWashingRoomItem); } }catch (Exception e){ Msg.showInfo(DeviceManagerActivity.this, "获取设备列表时出错!"); } } } else { Msg.showInfo(DeviceManagerActivity.this, "获取设备列表时出错!"); } closeProgress(); } } }