package com.wiiy.jtb.manager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.wiiy.jtb.R;
import com.wiiy.jtb.common.InternetThread;
import com.wiiy.jtb.common.Param;
import com.wiiy.jtb.entity.PhoneProductType;
import com.wiiy.jtb.entity.ViewHolder;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ManagerSalesmanlistActivity extends Activity{
private static ArrayList<PhoneProductType> userlist; //业务员
private static ArrayList<HashMap<String, Object>> alesmanstate;//加是否选中
private ArrayList<HashMap<String, Object>> salesmanreturns; //加是否选中
private Button confirm; //确定
private Button cancel; //取消
private ListView listview; //布局
MyAdapter adapter;
ViewHolder holder;
private ArrayList<HashMap<String,Object>> salesmanlist; //获得保存选中的业务员
private CheckBox checkall; //全选
private boolean checkallboolean; //保存全选状态
ProgressDialog progressDialog;
private MessageHandler messageHandler;
private String articleid;
private LinearLayout add_name; //隐藏全选
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不显示标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.productdialoglist);
Looper looper = Looper.myLooper();
messageHandler = new MessageHandler(looper);
salesmanreturns = new ArrayList<HashMap<String,Object>>();
salesmanlist = (ArrayList<HashMap<String,Object>>) getIntent().getSerializableExtra("salesmanlist");
checkallboolean = getIntent().getBooleanExtra("checkall",false);
articleid = getIntent().getStringExtra("articleid");
add_name = (LinearLayout) findViewById(R.id.add_name);
add_name.setVisibility(View.VISIBLE);
//全选
checkall = (CheckBox) findViewById(R.id.checkall);
checkall.setChecked(checkallboolean);
checkall.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count=0;
if(checkall.isChecked()){
for (int i = 0; i < userlist.size(); i++) {
count+=1;
MyAdapter.isSelected.put(i, true);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("item_tv", userlist.get(i));
map.put("item_cb", true);
alesmanstate.set(i, map);
}
adapter.notifyDataSetChanged();
}else{
for (int i = 0; i < userlist.size(); i++) {
MyAdapter.isSelected.put(i, false);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("item_tv", userlist.get(i));
map.put("item_cb", false);
alesmanstate.set(i, map);
}
adapter.notifyDataSetChanged();
}
}
});
listview = (ListView) findViewById(R.id.listview);
confirm = (Button) findViewById(R.id.confirm);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(userlist.size()==0){
finish();
}else{
//获得循环listview里的ckeckBox有几个被选中
for(int i = 0; i<listview.getCount(); i++){
LayoutInflater inflater = LayoutInflater.from(ManagerSalesmanlistActivity.this);;
View view = inflater.inflate(R.layout.productdialoglist_item, null);
ViewGroup group = null;
LinearLayout sn = (LinearLayout) adapter.getView(i, view, group);
LinearLayout checd = (LinearLayout) sn.getChildAt(0);
LinearLayout checds = (LinearLayout) checd.getChildAt(1);
CheckBox checkBox = (CheckBox) checds.getChildAt(0);// checkBox在LinearLayout的第二个位置(从gridview布局文件得知),所以用1(位置从0开始)
if(checkBox!=null){
if (checkBox.isChecked()){
salesmanreturns.add((HashMap<String, Object>) adapter.getItem(i));
}
}
}
Intent intent=new Intent();
intent.putExtra("salesmanreturn", salesmanreturns);
intent.putExtra("checkall", checkall.isChecked());
setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent,当然也可以自己定义新的Bundle
ManagerSalesmanlistActivity.this.finish();
}
}
});
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("companyCode", Param.companyCode));
param.add(new BasicNameValuePair("userId", Param.userId));
new InternetThread(param, "leave_queryUnderUser.action", messageHandler, 1).start();
progressDialog= new ProgressDialog(this);
progressDialog.setTitle("提示框");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIcon(R.drawable.icogth);
progressDialog.setMessage("数据加载中...");
progressDialog.setCancelable(false);
progressDialog.show();
}
public void showCheckBoxListView() {
alesmanstate = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < userlist.size(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("item_tv", userlist.get(i));
map.put("item_cb", false);
alesmanstate.add(map);
adapter = new MyAdapter(this,alesmanstate, R.layout.productdialoglist_item,
new String[] { "item_tv", "item_cb" }, new int[] {
R.id.productname, R.id.checkBox1 });
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
ListView listview = (ListView) arg0;
HashMap<String, Object> product1 = (HashMap<String, Object>) listview.getItemAtPosition(position);
PhoneProductType product = (PhoneProductType) product1.get("item_tv");
holder = (ViewHolder) view.getTag();
if(product.getId()!=null&&!product.getId().equals("0")){
holder.cb.toggle();// 在每次获取点击的item时改变checkbox的状态
MyAdapter.isSelected.put(position, holder.cb.isChecked()); // 同时修改map的值保存状态
if(holder.cb.isChecked()){
if(userlist.size()==(count+=1)){
checkall.setChecked(true);
}
}else{
if(userlist.size()>(count-=1)){
checkall.setChecked(false);
}
}
}
}
});
}
}
//为listview自定义适配器内部类
public static class MyAdapter extends BaseAdapter {
public static HashMap<Integer, Boolean> isSelected;
private Context context = null;
private LayoutInflater inflater = null;
private List<HashMap<String, Object>> list = null;
private String keyString[] = null;
private PhoneProductType itemString = null; // 记录每个item中textview的值
private int idValue[] = null;// id值
public MyAdapter(Context context, List<HashMap<String, Object>> list,
int resource, String[] from, int[] to) {
this.context = context;
this.list = list;
keyString = new String[from.length];
idValue = new int[to.length];
System.arraycopy(from, 0, keyString, 0, from.length);
System.arraycopy(to, 0, idValue, 0, to.length);
inflater = LayoutInflater.from(this.context);
init();
}
// 初始化 设置所有checkbox都为未选择
public void init() {
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < list.size(); i++) {
isSelected.put(i, false);
}
}
public int getCount() {
return list.size();
}
public Object getItem(int arg0) {
return list.get(arg0);
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View view, ViewGroup arg2) {
ViewHolder holder = null;
if (holder == null) {
holder = new ViewHolder();
if (view == null) {
view = inflater.inflate(R.layout.productdialoglist_item, null);
}
holder.tv = (TextView) view.findViewById(R.id.productname);
holder.cb = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
HashMap<String, Object> maps = new HashMap<String, Object>();
maps.put("item_tv", userlist.get(position));
maps.put("item_cb", isSelected.get(position));
list.set(position, maps);
HashMap<String, Object> map = list.get(position);
if (map != null) {
itemString = (PhoneProductType) map.get("item_tv");
holder.tv.setText(itemString.getN());
boolean ischeck = (Boolean) map.get("item_cb");
holder.cb.setChecked(ischeck);
}
return view;
}
}
class MessageHandler extends Handler {
public MessageHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
String message = "";
if(msg.arg2==1){
if(msg.arg1==1){
JSONObject jsonObject = (JSONObject) msg.obj;
try {
if(jsonObject.getBoolean("success")){
message = "数据加载成功!";
JSONArray aray = jsonObject.getJSONArray("userList");
userlist = new ArrayList<PhoneProductType>();
for(int i=0; i<aray.length(); i++){
PhoneProductType user = new PhoneProductType();
JSONArray obj = aray.getJSONArray(i);
for(int j=0; j<obj.length(); j++){
user.setId(obj.getString(0));
user.setN(obj.getString(1));
}
userlist.add(user);
}
if(userlist.size()==0){
View inflater = getLayoutInflater().inflate(R.layout.productdialoglist_item, null);
CheckBox check = (CheckBox) inflater.findViewById(R.id.checkBox1);
check.setVisibility(View.GONE);
List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> hash = new HashMap<String, String>();
hash.put("produtctname", "暂无业务员");
list.add(hash);
SimpleAdapter adapters = new SimpleAdapter(ManagerSalesmanlistActivity.this,list , R.layout.productdialoglist_item, new String[]{"produtctname"}, new int[]{R.id.productname});
listview.setAdapter(adapters);
}else{
showCheckBoxListView();
//取传到managerissuemessageactvivity的选中业务员
if(salesmanlist.size()!=0){
for(int i=0; i<salesmanlist.size(); i++){
for(int j=0; j<userlist.size(); j++){
PhoneProductType p = (PhoneProductType) salesmanlist.get(i).get("item_tv");
PhoneProductType ap = (PhoneProductType) userlist.get(j);
if(p.getId().equals(ap.getId())){
HashMap<String, Object> maps = new HashMap<String, Object>();
maps.put("item_tv", userlist.get(j));
maps.put("item_cb", (Boolean)salesmanlist.get(i).get("item_cb"));
alesmanstate.set(j, maps);
MyAdapter.isSelected.put(j, (Boolean)salesmanlist.get(i).get("item_cb"));
count+=1;
}
}
}
}
}
//加载要编辑的业务员
if(articleid!=null){
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("companyCode", Param.companyCode));
param.add(new BasicNameValuePair("articleId", articleid));
new InternetThread(param, "articlereader_queryArticleSalesman.action", messageHandler, 2).start();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
message="数据加载失败!";
}
progressDialog.dismiss();
Toast.makeText(ManagerSalesmanlistActivity.this,message, Toast.LENGTH_SHORT).show();
}
//加载以选择的业务员
if(msg.arg2==2){
if(msg.arg1==1){
JSONObject jsonObject = (JSONObject) msg.obj;
try {
if(jsonObject.getBoolean("success")){
String salesmanid = jsonObject.getString("salesmanid");
String a[] = salesmanid.split("@");
for(int i=0; i<alesmanstate.size(); i++){
PhoneProductType map = (PhoneProductType) alesmanstate.get(i).get("item_tv");
for(int j=0; j<a.length; j++){
if(map.getId().equals(a[j])){
HashMap<String, Object> maps = new HashMap<String, Object>();
maps.put("item_tv", map);
maps.put("item_cb", true);
alesmanstate.set(i, maps);
MyAdapter.isSelected.put(i, true);
adapter.notifyDataSetChanged();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
message="数据加载失败!";
}
}
}
}
}