Newer
Older
GasOperationApp / app / src / main / java / com / casic / gasoperation / PersonManagerActivity.java
[wangxitong] on 24 May 2021 11 KB first commit
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.DictTypeModel;
import com.casic.gasoperation.utils.HandlerStatus;
import com.casic.gasoperation.utils.Msg;
import com.casic.gasoperation.utils.MsgCallback;
import com.google.gson.JsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PersonManagerActivity 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;
    public String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.person_manager);
        Bundle bundle=getIntent().getExtras();
        id =bundle.getString("id");
        view = getLayoutInflater().inflate(R.layout.person_manager, 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("")){
                    getPersonList();
                }
            }
        });
        getPersonList();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (arg0 == search) {
                getPersonList();
            }
            else if(arg0 == add){
                Intent intent = new Intent(PersonManagerActivity.this, EditPersonActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("jobId",id);
                bundle.putString("type","新增");
                intent.putExtras(bundle);
                startActivity(intent);
            }
            else if(arg0 == back){
                finish();
            }
        }
    };
    public void getPersonList() {
        wr_areas.removeAllViews();
        thread = new BaseThread(baseHandler)
        {
            @Override
            public void runThread()
            {
                final Message msg = thread.obtainMessage();
                if (msg != null)
                {
                    msg.what = HandlerStatus.HANDLE_PERSONMANAGER;
                    try
                    {
                        ApiClent.personManagerList(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(PersonManagerActivity.this, "未查询到任何项");
                                        return ;
                                    }else{
                                        msg.arg1 = HandlerStatus.HANDLE_OK;
                                        msg.obj = jsonArray;
                                    }
                                } catch (JSONException e) {
                                    // TODO Auto-generated private View view = null;
                                    //    private LinearLayout wr_areas;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();
        }
        PersonManagerActivity.this.finish();
    }

    public void deletePerson(View view, JSONArray json)
    {
        final View v = view;
        final JSONArray jsonArray = json;
        Msg.confirm(PersonManagerActivity.this, "确认删除该作业人员吗?", "删除作业人员", new MsgCallback() {
            @Override
            public void callBack(Boolean result) {
                if (result != null && result.booleanValue()) {
                    int i = (Integer) v.getTag();   //这里的i不能在外部定义,因为内部类的关系,内部类好多繁琐的东西,要好好研究一番
                    try {
                        ApiClent.deletePerson(jsonArray.getJSONObject(i-clicknum).getString("id"),new ApiClent.ClientCallback() {
                            @Override
                            public void onSuccess(Object data)
                            {
                                getPersonList();
                            }
                            @Override
                            public void onFailure(String message) {
                                Msg.showInfo(PersonManagerActivity.this, "删除人员失败!");
                            }
                            @Override
                            public void onError(Exception e) {
                                Msg.showInfo(PersonManagerActivity.this, "删除人员异常!");
                            }
                        });
                    }catch (JSONException e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    @Override
    protected void handleMessage(Message msg)
    {
        if (msg.what == HandlerStatus.HANDLE_PERSONMANAGER)
        {
            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_view3, null);

//                            TextView index = (TextView) llWashingRoomItem.findViewById(R.id.index);
                            TextView name = (TextView) llWashingRoomItem.findViewById(R.id.name);
                            TextView status = (TextView) llWashingRoomItem.findViewById(R.id.status);
                            TextView department = (TextView) llWashingRoomItem.findViewById(R.id.department);
                            TextView phone = (TextView) llWashingRoomItem.findViewById(R.id.phone);
                            ImageView edit = (ImageView) llWashingRoomItem.findViewById(R.id.edit);
                            ImageView delete = (ImageView) llWashingRoomItem.findViewById(R.id.delete);

//                            index.setText(Integer.toString(i+1));
                            name.setText(json.getString("name"));
                            status.setText(json.getString("authorizeName"));
                            department.setText(json.getString("departmentName"));
                            phone.setText(json.getString("phone"));

                            delete.setTag(clicknum+i);
                            delete.setClickable(true);
                            delete.setOnClickListener(new View.OnClickListener(){
                                @Override
                                public void onClick(View v) {
                                    deletePerson(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(PersonManagerActivity.this, EditPersonActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("type","编辑");
                                    bundle.putString("jobId",id);
                                    try {
                                        bundle.putString("data", jsonArray.getJSONObject(i-editnum).toString());
                                    }catch (JSONException e){
                                        e.printStackTrace();
                                    }
                                    intent.putExtras(bundle);
                                    startActivityForResult(intent,0x006);
                                }
                            });
                            wr_areas.addView(llWashingRoomItem);
                    }

                    }catch (Exception e){
                        Msg.showInfo(PersonManagerActivity.this, "获取人员列表时出错!");
                    }
                }
            }
            else
            {
                Msg.showInfo(PersonManagerActivity.this, "获取人员列表时出错!");
            }
            closeProgress();
        }
    }
}