net.evecom.androidecssp.activity.TaskResponseAddActivity.java Source code

Java tutorial

Introduction

Here is the source code for net.evecom.androidecssp.activity.TaskResponseAddActivity.java

Source

/*
 * Copyright (c) 2005, 2014, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 */
package net.evecom.androidecssp.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import net.evecom.androidecssp.R;
import net.evecom.androidecssp.base.AfnailPictureActivity;
import net.evecom.androidecssp.base.BaseActivity;
import net.evecom.androidecssp.base.BaseModel;
import net.evecom.androidecssp.base.ICallback;
import net.evecom.androidecssp.base.UploadPictureActivity;
import net.evecom.androidecssp.bean.FileManageBean;
import net.evecom.androidecssp.bean.TaskResponseInfo;
import net.evecom.androidecssp.util.ShareUtil;
import net.evecom.androidecssp.util.UiUtil;
import net.tsz.afinal.FinalDb;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 *  
 * @author Mars zhang
 * @created 2015-11-12 10:15:43
 */
public class TaskResponseAddActivity extends BaseActivity {

    private EditText titleeditText;
    private EditText contenteditText;
    private EditText keywordeditText;
    private EditText peopleeditText;
    private EditText remarkeditText;

    // private TextView timeView;
    private TextView leveView;

    private ListView imageListView;

    private BaseModel eventInfo;
    private BaseModel projectInfo;
    private BaseModel taskInfo;

    private String[] levestr;

    private FinalDb db;
    /**  */
    private List<FileManageBean> fileList;
    private UploadPictureAdapter uploadPictureAdapter;

    private String saveResult = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_response_add_activity);
        Intent intent = getIntent();
        eventInfo = (BaseModel) getData("eventInfo", intent);
        projectInfo = (BaseModel) getData("projectInfo", intent);
        taskInfo = (BaseModel) getData("taskInfo", intent);
        init();
        initdata();
    }

    private void init() {
        titleeditText = (EditText) findViewById(R.id.task_response_title_et);
        contenteditText = (EditText) findViewById(R.id.task_response_content_et);
        keywordeditText = (EditText) findViewById(R.id.task_response_keyword_et);
        peopleeditText = (EditText) findViewById(R.id.task_response_people_et);
        remarkeditText = (EditText) findViewById(R.id.task_response_remark_et);

        leveView = (TextView) findViewById(R.id.task_response_leve_tv);

        imageListView = (ListView) findViewById(R.id.task_response_file_list);
    }

    private void initdata() {
        /**  */
        db = FinalDb.create(this);
        db.deleteAll(FileManageBean.class);

        levestr = new String[] { "", "", "" };

        fileList = new ArrayList<FileManageBean>();
        uploadPictureAdapter = new UploadPictureAdapter(getApplicationContext(), fileList);
        imageListView.setAdapter(uploadPictureAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 1: // 
            String filePath = data.getStringExtra("filePath");
            Log.v("mars", filePath);
            manageFileDataAndListView(filePath);
            break;
        case 2: //   listView
            fileList.removeAll(fileList);
            List<FileManageBean> allPictures = db.findAll(FileManageBean.class);
            // listView
            for (FileManageBean item : allPictures) {
                fileList.add(item);
            }
            // listView
            uploadPictureAdapter.notifyDataSetChanged();
            if (null != imageListView) {
                UiUtil.setListViewHeightBasedOnChildren(imageListView);
            }
            break;
        default:
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * bean listView
     * 
     * @param filePath
     */
    private void manageFileDataAndListView(String filePath) {
        if (null != filePath && !filePath.equals("none")) {
            FileManageBean filebean = new FileManageBean();
            filebean.setFile_URL(filePath);
            // 
            List<FileManageBean> fileManageBeanstemp = db.findAllByWhere(FileManageBean.class,
                    "File_URL=\"" + filePath + "\"");
            if (null == fileManageBeanstemp || fileManageBeanstemp.size() == 0) {
                db.save(filebean);
                fileList.removeAll(fileList);
                List<FileManageBean> allPictures = db.findAll(FileManageBean.class);
                // listView
                for (FileManageBean item : allPictures) {
                    fileList.add(item);
                }
                // listView
                uploadPictureAdapter.notifyDataSetChanged();
                if (null != imageListView) {
                    UiUtil.setListViewHeightBasedOnChildren(imageListView);
                }
            } else {
                toast("!", 1);
            }
        } else {
            Log.v("mars", "");
        }
    }

    /**
     * 
     * 
     * 
     * @author Mars zhang
     * 
     */
    public class UploadPictureAdapter extends BaseAdapter implements ListAdapter {
        /** MemberVariables */
        private Context context;
        /** MemberVariables */
        private LayoutInflater inflater;
        /** MemberVariables */
        private List<FileManageBean> list;

        public UploadPictureAdapter(Context context, List<FileManageBean> list) {
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();

        }

        @Override
        public Object getItem(int item) {
            return this.list.get(item);
        }

        @Override
        public long getItemId(int itemId) {
            return itemId;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            if (null == view) {
                view = inflater.inflate(R.layout.file_list_item, null);
            }
            TextView textViewTitle = (TextView) view.findViewById(R.id.file_list_item_tv);
            String s[] = Pattern.compile("/").split(list.get(i).getFile_URL());
            textViewTitle.setText("" + s[s.length - 1]);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(getApplicationContext(), AfnailPictureActivity.class);
                    intent.putExtra("URI", list.get(i).getFile_URL());
                    intent.putExtra("File_Id", list.get(i).getFile_ID());
                    startActivityForResult(intent, 2);

                }
            });
            return view;
        }
    }

    /**
     * 
     */
    private void submit() {
        int index = 0;
        // 
        if (titleeditText.getText().toString().trim().length() == 0) {
            index += 1;
        }
        if (contenteditText.getText().toString().trim().length() == 0) {
            index += 2;
        }
        switch (index) {
        case 1:
            DialogToast("!", new ICallback() {
                @Override
                public Object execute() {
                    return null;
                }
            });
            return;
        case 2:
            DialogToast("!", new ICallback() {
                @Override
                public Object execute() {
                    return null;
                }
            });
            return;
        case 3:
            DialogToast("!", new ICallback() {
                @Override
                public Object execute() {
                    return null;
                }
            });
            return;
        default:
            break;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("planEventTaskResponse.eventid", eventInfo.get("id").toString());
        hashMap.put("planEventTaskResponse.responselevel", leveView.getText().toString());
        hashMap.put("planEventTaskResponse.responsetitle", titleeditText.getText().toString());
        hashMap.put("planEventTaskResponse.remark", remarkeditText.getText().toString());
        hashMap.put("planEventTaskResponse.responsedeptid",
                ShareUtil.getString(getApplicationContext(), "PASSNAME", "orgid", ""));
        hashMap.put("planEventTaskResponse.responsename",
                ShareUtil.getString(getApplicationContext(), "PASSNAME", "usernameCN", ""));
        hashMap.put("planEventTaskResponse.planid", projectInfo.get("planid").toString());
        hashMap.put("planEventTaskResponse.taskid", taskInfo.get("id").toString());
        hashMap.put("planEventTaskResponse.responsecon", contenteditText.getText().toString());
        hashMap.put("planEventTaskResponse.responseid",
                ShareUtil.getString(getApplicationContext(), "PASSNAME", "userid", ""));
        postdata(hashMap);
    }

    private void postdata(final HashMap<String, String> entity) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                try {
                    saveResult = connServerForResultPost("jfs/ecssp/mobile/taskresponseCtr/taskResponseAdd",
                            entity);
                } catch (ClientProtocolException e) {
                    message.what = MESSAGETYPE_02;
                    Log.e("mars", e.getMessage());
                } catch (IOException e) {
                    message.what = MESSAGETYPE_02;
                    Log.e("mars", e.getMessage());
                }
                if (saveResult.length() > 0) {
                    message.what = MESSAGETYPE_01;
                    String responseid = "";
                    try {
                        TaskResponseInfo taskResponseInfo = getTaskResponseInfo(saveResult);
                        if (null != taskResponseInfo) {
                            responseid = taskResponseInfo.getId();
                        }
                    } catch (JSONException e) {
                        Log.e("mars", e.getMessage());
                    }
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("taskresponseId", responseid);
                    postImage(hashMap, fileList, "jfs/ecssp/mobile/taskresponseCtr/taskResponseFileSave");
                } else {
                    message.what = MESSAGETYPE_02;
                }
                Log.v("mars", saveResult);
                saveHandler.sendMessage(message);
            }
        }).start();

    }

    /**
     * json
     * 
     */
    public static TaskResponseInfo getTaskResponseInfo(String jsonString) throws JSONException {
        JSONObject jsonObject = new JSONObject(jsonString);
        TaskResponseInfo taskResponseInfo = new TaskResponseInfo();
        taskResponseInfo.setId(jsonObject.getString("id"));
        taskResponseInfo.setResponsetitle("responsetitle");
        return taskResponseInfo;
    }

    /**
     * 
     */
    private Handler saveHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGETYPE_01://   
                Intent intent = new Intent(getApplicationContext(), ResponseListActivity.class);
                ResponseListActivity.pushData("eventInfo", eventInfo, intent);
                ResponseListActivity.pushData("projectInfo", projectInfo, intent);
                ResponseListActivity.pushData("taskInfo", taskInfo, intent);
                startActivity(intent);
                finish();
                break;
            case MESSAGETYPE_02:
                toast("", 1);
                break;
            default:
                break;
            }
        };
    };

    public void fksj(View view) {

    }

    public void zyjb(View view) {
        Dialog dialog = new AlertDialog.Builder(TaskResponseAddActivity.this)
                .setIcon(R.drawable.qq_dialog_default_icon).setTitle("")
                .setItems(levestr, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        leveView.setText(levestr[which]);
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    public void findpictureonclick(View view) {
        Intent intent = new Intent(getApplicationContext(), UploadPictureActivity.class);
        startActivityForResult(intent, 1);
    }

    public void bc(View view) {
        submit();
    }
}