1. 主Activity
package com.avicsafety.dangerous_chemicals;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import com.avicsafety.lib.Adapter.CommonAdapter;
import com.avicsafety.lib.Adapter.ViewHolder;
import com.avicsafety.lib.CustomView.AvicButton;
import com.avicsafety.lib.CustomView.AvicSelect;
import com.avicsafety.lib.tools.DialogCreator;
import com.avicsafety.model.I_list;
import com.avicsafety.model.M_MajorHazard;
import com.avicsafety.model.M_MajorHazardLast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.AlteredCharSequence;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
@ContentView(R.layout.activity_major_last)
public class MajorHazardLastActivity extends BaseActivity {
@ViewInject(R.id.list)
private ListView myListView;
@ViewInject(R.id.btn_add)
private Button btnAdd;
@ViewInject(R.id.btn_jisuan)
private Button btnJisuan;
@ViewInject(R.id.as_number)
private AvicSelect as_number;
private Dialog dg_number;
private CommonAdapter
private List
private Float P_result;
@Override
protected void InitializeData() {
super.InitializeData();
myListView.setAdapter(mAdapter = new CommonAdapter
getApplicationContext(), list, R.layout.activity_major_delete) {
@Override
public void convert(ViewHolder helper, M_MajorHazard item) {
helper.setText(R.id.tv_name, item.getName());
DecimalFormat fnum = new DecimalFormat("##0.00");
String dd=fnum.format(item.getQuantum());
helper.setText(R.id.et_num, dd);
}
});
}
//跳转到添加页面
@Override
protected void InitializeEvent() {
super.InitializeEvent();
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(MajorHazardLastActivity.this, MajorHazardActivity2.class);
startActivityForResult(intent, 1000);
}
});
//计算是否构成重大危险源,涉及到公式计算
btnJisuan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Float all_result = 0f;
P_result = 0f;
for (int i = 0; i < list.size(); i++) {
M_MajorHazard model = list.get(i);
if ("无标准".equals(model.getThresholdQuantity())) {
} else {
// EditText et = (EditText)
// layout.getChildAt(1)
Float show = model.getQuantum();
// Float value = Float.valueOf(show);
Float co = Float.valueOf(model.getThresholdQuantity());
Float pp = Float.valueOf(model.getCorrectionCoefficient());
Float result = show / co;
Float p = result*pp;
all_result += result;
P_result+=p;
}
// sb计算公式拼写
// sb.append("");
}
// ll1.setVisibility(View.VISIBLE);
// text_result1.setText(all_result+"");
String re = "计算结果:"+all_result;
if(all_result>=1){
// text_result2.setText("已构成重大危险源");
re+=" 已构成重大危险源\n";
// ll3.setVisibility(View.VISIBLE);
//计算过程
Float a;
if("".equals(as_number.getValue())){
a = 0f;
}else{
a = Float.valueOf(as_number.getValue());
}
Float b = a*P_result;
//结果
re=re+"\n重大危险源级别: "+getLevel(b);
}else{
re+=" 未构成重大危险源\n";
// text_result2.setText("未构成重大危险源");
// ll3.setVisibility(View.GONE);
}
new AlertDialog.Builder(oThis).setTitle("计算结果")//设置对话框标题
.setMessage(re)//设置显示的内容
.setPositiveButton("确定",null).show();
// EditText et=(EditText) findViewById(R.id.et_num);
// ListSJ l=new ListSJ();
// l.setValue(11);
//
//
// String num=et.getText().toString();
// l.setNumber(Integer.valueOf(num));
// int a=l.getValue();
// int c=a*Integer.valueOf(num);
//
// String show=String.valueOf(c);
//
// Toast.makeText(MainActivity.this, show, 1).show();;
}
});
//LISTVIEW,输入、保存、限制小数点两位等
as_number.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(dg_number==null){
String[] key = oThis.getResources().getStringArray(R.array.outnumber);
String[] value = new String[]{"2.0","1.5","1.2","1.0","0.5"};
dg_number = DialogCreator.singleChoiceDailog(oThis, key, as_number, value);
}
dg_number.show();
}
});
//已添加危化品LISTVIEW点击功能
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
final int pos = position;
LayoutInflater factory = LayoutInflater.from(oThis);// 提示框
final View view2 = factory.inflate(R.layout.alert_input_et1,
null);// 这里必须是final的
final EditText et = (EditText) view2
.findViewById(R.id.edit_text);// 获得输入框对象
// et.addTextChangedListener(new TextWatcher()
// {
// public void afterTextChanged(Editable edt)
// {
// String temp = edt.toString();
// int posDot = temp.indexOf(".");
// if (posDot <= 0) return;
// if (temp.length() - posDot - 1 > 2)
// {
// edt.delete(posDot + 3, posDot + 4);
// }
// }
//
// public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
//
// public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
// });
// et.setFilters(new InputFilter[]{new com.avicsafety.lib.tools.MyInputFilter(2)});
float f = list.get(position).getQuantum();
DecimalFormat fnum = new DecimalFormat("##0.00");
String dd=fnum.format(f);
et.setText(dd);
AlertDialog.Builder builder = new AlertDialog.Builder(oThis);
builder.setTitle("存在量(单位:吨)")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(view2).setNegativeButton("取消", null);
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
float t;
if(null == et.getText().toString()||et.getText().toString().equals("")){
t = 0f;
}else{
t = Float.valueOf(et.getText().toString());
}
list.get(pos).setQuantum(t);
mAdapter.setmDatas(list);
mAdapter.notifyDataSetChanged();
}
});
builder.show();
//LISTVIEW 长按获得删除属性
}
});
myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent, View view,
int position, long id) {
final int pos = position;
AlertDialog dailog = DialogCreator.singleChoiceDailog(oThis, new String[]{"删除"}, new DialogCreator.OnSelectionChangedListener() {
@Override
public void onChanged(String key, String value) {
if(key.equals("删除")){
list.remove(pos);
mAdapter.setmDatas(list);
mAdapter.notifyDataSetChanged();
}
}
});
dailog.show();
return true;
}
});
}
//弹出结果对话框
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
// TODO Auto-generated method stub
super.onActivityResult(arg0, arg1, arg2);
if (arg0 == 1000 && arg1 == RESULT_OK) {
M_MajorHazard mm = (M_MajorHazard) arg2.getSerializableExtra("data");
add(mm);
}
}
protected void add(M_MajorHazard model) {
list.add(model);
mAdapter.notifyDataSetChanged();
}
private String getLevel(Float num){
if(num >=100f){
return "一级";
}else if(num<100f &&num>=50f){
return "二级";
}else if(num<50f &&num>=10f){
return "三级";
}else{
return "四级";
}
}
}
2.主页面xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/FFFFFF"
android:orientation="vertical" >
android:layout_height="50dp"
android:orientation="horizontal" >
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="添加危化品"
android:textColor="#ffffff"
app:buttonColor="Blue" />
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="计算"
android:textColor="#ffffff"
app:buttonColor="Blue" />
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_margin="5dp"
app:select_hint="@string/GEN_please_choose"
app:select_lable="厂外可能暴露人员数量" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >
3.添加页面Activity
package com.avicsafety.dangerous_chemicals;
import java.util.ArrayList;
import java.util.List;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.avicsafety.dao.D_Major_Hazard;
import com.avicsafety.lib.Adapter.CommonAdapter;
import com.avicsafety.lib.Adapter.ViewHolder;
import com.avicsafety.model.M_MajorHazard;
import com.avicsafety.service.MajorManager;
@ContentView(R.layout.activity_add_major_hazard)
public class MajorHazardActivity2 extends BaseActivity {
@ViewInject(R.id.editText1)
private EditText editText1;
@ViewInject(R.id.button_add)
private Button button_add;
@ViewInject(R.id.major_list)
private ListView major_list;
private CommonAdapter
private List
@Override
protected void InitializeData() {
button_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String key = editText1.getText().toString();
refrechPage(key);
}
});
dataList = new ArrayList
//显示LISTVIEW列表,显示项目
major_list.setAdapter(mAdapter = new CommonAdapter
getApplicationContext(), dataList, R.layout.item_major_hazard) {
@Override
public void convert(ViewHolder helper, M_MajorHazard item) {
helper.setText(R.id.major_hazard_name, item.getName());
helper.setText(R.id.major_hazard_anotherName,
item.getAnotherName());
}
});
major_list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println();
Intent intent = new Intent();
intent.setClass(oThis, MajorHazardActivity.class);
intent.putExtra("data", dataList.get(position));
setResult(RESULT_OK, intent);
finish();
}
});
refrechPage("");
}
// 更新listview页面
private void refrechPage(String key) {
// 更新list
// 查数据库 datalist = dao.
MajorManager manager = new MajorManager();
dataList = manager.getList(key);
// for(int i=0;i<6;i++){
// dataList.add(this.getModel());
// }
mAdapter.setmDatas(dataList);
mAdapter.notifyDataSetChanged();
}
}
4.添加页面xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/FFFFFF"
android:orientation="vertical" >
android:layout_height="45dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp" >
android:paddingRight="10dp"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/bg_edittext"
android:ems="10"
android:hint="请输入危化品名称"
android:textSize="17sp" >
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:layout_marginLeft="5dp"
android:text="搜索"
android:textColor="#ffffff"
android:textSize="12dp"
app:buttonColor="Blue" />
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
5. 主页面已添加LISTVIEW的XML
android:layout_height="60dp"
android:layout_margin="8dp"
android:background="@color/FFFFFF"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_function_01" />
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/imageView1"
android:gravity="center_vertical"
android:lines="1"
android:text="tv_name"
android:textColor="#000000"
android:textSize="18sp" />
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignLeft="@+id/tv_name"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp"
android:layout_marginRight="10dp"
android:background="@drawable/bg_edittext"
android:gravity="center_vertical"
android:hint="请输入存在量"
android:paddingLeft="3dp"
android:textColor="#000000"
android:textSize="16sp" />
6.model
package com.avicsafety.model;
import java.io.Serializable;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
@Table(name = "major_hazard")
public class M_MajorHazard implements Serializable {
@Column(name = "id", isId = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "anotherName")
private String anotherName;
@Column(name = "thresholdQuantity")
private String thresholdQuantity;
@Column(name = "correctionCoefficient")
private int correctionCoefficient;
private Float quantum;
public Float getQuantum() {
if(null == quantum){
return 0f;
}else{
return quantum;
}
}
public void setQuantum(Float quantum) {
if(null == quantum){
this.quantum = 0f;
}else{
this.quantum = quantum;
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAnotherName() {
return anotherName;
}
public void setAnotherName(String anotherName) {
this.anotherName = anotherName;
}
public String getThresholdQuantity() {
return thresholdQuantity;
}
public void setThresholdQuantity(String thresholdQuantity) {
this.thresholdQuantity = thresholdQuantity;
}
public int getCorrectionCoefficient() {
return correctionCoefficient;
}
public void setCorrectionCoefficient(int correctionCoefficient) {
this.correctionCoefficient = correctionCoefficient;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.name;
}
}
7.dao
package com.avicsafety.dao;
import java.util.List;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import com.avicsafety.model.M_MajorHazard;
//LISTVIEW通过名字模糊查询
public class D_Major_Hazard{
public List
List
return list;
}
//LISTVIEW通过ID精确查找
public M_MajorHazard getModel(DbManager db,int id) throws DbException{
M_MajorHazard model = db.selector(M_MajorHazard.class).where("id","=",id).findFirst();
return model;
}
}