package com.bigdo.app; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import com.bigdo.common.BDSQLiteHelper; import com.bigdo.common.Login; import com.bigdo.common.RConfig; import com.bigdo.common.RState; import com.bigdo.common.User; import com.bigdo.controls.IXListViewListener; import com.bigdo.controls.XListView; import com.bigdo.util.WSCheck; import com.bigdo.util.WSHelper; import com.bigdo.util.WSResult; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; 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.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class Me_MyOrder extends BaseActivity implements IXListViewListener { XListView order_list; ArrayList<HashMap<String, Object>> data; OrderAdapter ad; TextView no_data; LinearLayout tools_host; Button tools_0, tools_1, tools_2; private int cur_pos = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.me_myorder); View com_title = (View) this.findViewById(R.id.bd_title_host); TextView bd_title_name = (TextView) com_title .findViewById(R.id.bd_title_name); bd_title_name.setText("充值记录"); TextView bd_title_operate = (TextView) com_title .findViewById(R.id.bd_title_operate); bd_title_operate.setText("充 值"); bd_title_operate.setVisibility(View.VISIBLE); bd_title_operate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { goPay(); } }); ImageButton bd_title_back = (ImageButton) com_title .findViewById(R.id.bd_title_back); bd_title_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); data = new ArrayList<HashMap<String, Object>>(); order_list = (XListView) this.findViewById(R.id.order_list); order_list.setPullLoadEnable(false); order_list.setXListViewListener(this); order_list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { arg2--; if (arg2 < 0) { arg2 = 0; } cur_pos = arg2; showBTools(); } }); ad = new OrderAdapter(); order_list.setAdapter(ad); no_data = (TextView) this.findViewById(R.id.order_list_no_data); loadData(); } private void showBTools() { if (tools_host == null) { tools_host = (LinearLayout) findViewById(R.id.b_tools_host); tools_0 = (Button) findViewById(R.id.b_tools_0); tools_1 = (Button) findViewById(R.id.b_tools_1); tools_2 = (Button) findViewById(R.id.b_tools_2); tools_host.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (tools_host.getVisibility() == LinearLayout.GONE) { tools_host.setVisibility(LinearLayout.VISIBLE); } else { tools_host.setVisibility(LinearLayout.GONE); } } }); tools_0.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { tools_host.setVisibility(LinearLayout.GONE); } }); tools_1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { tools_host.setVisibility(LinearLayout.GONE); if (cur_pos >= 0) { goPay(cur_pos); } } }); tools_2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { tools_host.setVisibility(LinearLayout.GONE); if (cur_pos >= 0) { cancelMyOrder(cur_pos); } } }); } if (data != null) { HashMap<String, Object> mp = data.get(cur_pos); if (mp != null) { int status = (Integer) mp.get("status"); if (status == 0) { tools_1.setVisibility(Button.VISIBLE); } else { tools_1.setVisibility(Button.GONE); } } } if (tools_host.getVisibility() == LinearLayout.GONE) { tools_host.setVisibility(LinearLayout.VISIBLE); } else { tools_host.setVisibility(LinearLayout.GONE); } } private void cancelMyOrder(int p) { User user = Login.get(this); if (user != null) { if (data != null) { HashMap<String, Object> mp = data.get(p); if (mp != null) { int orderno = (Integer) mp.get("orderno"); if (orderno >= 0) { JSONObject jparam = new JSONObject(); try { jparam.put("user", user.user); jparam.put("orderno", orderno); } catch (Exception e) { this.messageBox("准备请求参数出错."); return; } showProgress("删除中..."); String url = RConfig.calcelMyOrder(this); String data = jparam.toString(); WSHelper ws = new WSHelper(this, "", false, handler, url, "", WSHelper.getStringEntity(data), 2, 0, 0, 0, WSHelper.POST); ws.start(); Log.e("cancelMyOrder", url); Log.e("cancelMyOrder_data", data); } } } } else { showInfo("请先登录"); } } public void loadData() { getMyOrder(); if (data.size() <= 0) { order_list.startAutoHeightRefresh(1); } } private void goPay() { Intent i = new Intent(); i.setClass(this, PayActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivityForResult(i, 2); } private void startGetMyOrder() { checkHasData(true); User user = Login.get(this); if (user != null) { JSONObject jparam = new JSONObject(); try { jparam.put("user", user.user); } catch (Exception e) { checkHasData(false); this.messageBox("准备请求参数出错."); order_list.stopRefresh(); return; } String url = RConfig.myOrder(this); String data = jparam.toString(); WSHelper ws = new WSHelper(this, "", false, handler, url, "", WSHelper.getStringEntity(data), 0, 0, 0, 0, WSHelper.POST); ws.start(); Log.e("startGetMyOrder", url); Log.e("startGetMyOrder_data", data); } else { showInfo("请先登录"); } } private void checkHasData(boolean isoping) { if (no_data != null) { if (!isoping && data.size() <= 0) { no_data.setVisibility(LinearLayout.VISIBLE); } else { no_data.setVisibility(LinearLayout.GONE); } } } private void goPay(int p) { if (data != null) { HashMap<String, Object> mp = data.get(p); if (mp != null) { int orderno = (Integer) mp.get("orderno"); float amount = (Float) mp.get("amount"); int status = (Integer) mp.get("status"); if (orderno >= 0 && status == 0) { Intent i = new Intent(); i.setClass(this, PayActivity.class); i.putExtra("orderno", orderno); i.putExtra("amount", amount); i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivityForResult(i, 1); return; } } } showInfo("该订单已经支付完成了哦"); } @Override public void onRefresh(XListView xListView, int tag, int requestCode) { startGetMyOrder(); } @Override public void onLoadMore(XListView xListView, int tag, int requestCode) { } private int getMyOrder() { int count = 0; User userObj = Login.get(this); if (userObj != null) { if (data != null && ad != null) { data.clear(); BDSQLiteHelper sqlobj = null; SQLiteDatabase sqlobj_r = null; Cursor cu = null; try { sqlobj = new BDSQLiteHelper(this); sqlobj_r = sqlobj.getReadableDatabase(); String sqlt = "select * from myorder where user = ? order by orderno desc"; cu = sqlobj_r.rawQuery(sqlt, new String[] { userObj.user }); int orderno, status; String user, userdesc, createtime, paytype, notes; float amount = 0; while (cu.moveToNext()) { orderno = cu.getInt(cu.getColumnIndex("orderno")); user = cu.getString(cu.getColumnIndex("user")); userdesc = cu.getString(cu.getColumnIndex("userdesc")); amount = cu.getFloat(cu.getColumnIndex("amount")); status = cu.getInt(cu.getColumnIndex("status")); createtime = cu.getString(cu .getColumnIndex("createtime")); paytype = cu.getString(cu.getColumnIndex("paytype")); notes = cu.getString(cu.getColumnIndex("notes")); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("orderno", orderno); map.put("user", user); map.put("userdesc", userdesc); map.put("amount", amount); map.put("status", status); map.put("createtime", createtime); map.put("paytype", paytype); map.put("notes", notes); data.add(map); count++; } } catch (Exception e2) { messageBox("加载本地缓存充值记录出错"); } try { if (cu != null) { cu.close(); cu = null; } } catch (Exception ex) { } try { if (sqlobj_r != null) { sqlobj_r.close(); sqlobj_r = null; } } catch (Exception ex) { } try { if (sqlobj != null) { sqlobj.close(); sqlobj = null; } } catch (Exception ex) { } if (ad != null) { ad.notifyDataSetChanged(); } checkHasData(false); } } else { showInfo("请先登录"); } return count; } private void update(JSONObject jresult) { if (order_list != null && data != null && ad != null) { boolean addtag = false, deltag = false, uptag = false; if (jresult != null) { BDSQLiteHelper sqlobj = null; SQLiteDatabase sqlobj_w = null; Cursor cu = null; try { sqlobj = new BDSQLiteHelper(this); sqlobj_w = sqlobj.getWritableDatabase(); try { sqlobj_w.delete("myorder", null, null); data.clear(); // ad.notifyDataSetChanged(); } catch (Exception ee) { } JSONArray item = jresult.getJSONArray("item"); if (item != null && item.length() > 0) { int add_count = 0, update_count = 0; try { int lg = item.length(); int orderno, status; String user, userdesc, createtime, paytype, notes; float amount = 0; long c = 0; for (int i = 0; i < lg; i++) { JSONObject vo = (JSONObject) item.opt(i); if (vo != null) { orderno = vo.getInt("orderno"); user = vo.getString("user"); userdesc = vo.getString("userdesc"); status = vo.getInt("status"); amount = (float) vo.getDouble("amount"); createtime = vo.getString("createtime"); paytype = vo.getString("paytype"); notes = vo.getString("notes"); ContentValues cv = new ContentValues(); cv.put("orderno", orderno); cv.put("user", user); cv.put("userdesc", userdesc); cv.put("status", status); cv.put("amount", amount); cv.put("createtime", createtime); cv.put("paytype", paytype); cv.put("notes", notes); cu = sqlobj_w .rawQuery( "select orderno from myorder where orderno = ? ", new String[] { orderno + "" }); c = cu.getCount(); try { if (cu != null) { cu.close(); } } catch (Exception ex) { } cu = null; if (c <= 0) { c = sqlobj_w.insert("myorder", "", cv); add_count++; if (c > 0) { addtag = true; } } else { cv.remove("orderno"); c = sqlobj_w.update("myorder", cv, "orderno = ?", new String[] { orderno + "" }); update_count += c; if (c > 0) { uptag = true; } } cv = null; } } } catch (Exception e) { messageBox("缓存充值记录数据出错."); } String msg = ""; if (add_count > 0) { msg = "亲!新加载了 " + add_count + " 条充值记录"; } if (update_count > 0) { if (msg.length() > 0) { msg += ","; } else { msg = "亲! "; } msg = "更新了 " + update_count + " 条充值记录."; } if (msg.length() > 0) { this.showInfo(msg); } } else { showInfo("亲!没有新充值记录了哦."); } } catch (Exception e) { } try { if (cu != null) { cu.close(); cu = null; } } catch (Exception ex) { } try { if (sqlobj_w != null) { sqlobj_w.close(); sqlobj_w = null; } } catch (Exception ex) { } try { if (sqlobj != null) { sqlobj.close(); sqlobj = null; } } catch (Exception ex) { } } } } @SuppressLint("HandlerLeak") protected Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.arg1 == RState.WSCall) { WSResult result = (WSResult) msg.getData().getParcelable( WSResult.ResultKey); JSONObject jresult = WSCheck.Json(Me_MyOrder.this, result); if (result.RequestCode == 0) { if (order_list != null) { update(jresult); getMyOrder(); order_list.stopRefresh(); } } else if (result.RequestCode == 2) { hiddenProgress(); cancelFinish(jresult); } } } }; private void cancelFinish(JSONObject jresult) { if (jresult != null) { BDSQLiteHelper sqlobj = null; SQLiteDatabase sqlobj_w = null; try { sqlobj = new BDSQLiteHelper(this); sqlobj_w = sqlobj.getWritableDatabase(); int orderno = jresult.getInt("orderno"); int c = 0; try { c = sqlobj_w.delete("myorder", "orderno = ?", new String[] { orderno + "" }); if (c > 0 && data != null && ad != null) { int lg = data.size(); for (int i = 0; i < lg; i++) { HashMap<String, Object> mp = data.get(i); if (mp != null) { if (orderno == (Integer) mp.get("orderno")) { data.remove(i); ad.notifyDataSetChanged(); break; } } } } } catch (Exception ee) { } if (c > 0) { messageBox("删除成功"); } checkHasData(false); } catch (Exception e) { } try { if (sqlobj_w != null) { sqlobj_w.close(); sqlobj_w = null; } } catch (Exception ex) { } try { if (sqlobj != null) { sqlobj.close(); sqlobj = null; } } catch (Exception ex) { } } } public class OrderAdapter extends BaseAdapter { private LayoutInflater mInflater; public OrderAdapter() { this.mInflater = LayoutInflater.from(Me_MyOrder.this); } @Override public int getCount() { if (data != null) { return data.size(); } return 0; } @Override public Object getItem(int arg0) { if (data != null) { return data.get(arg0); } return null; } @Override public long getItemId(int arg0) { return arg0; } String createtime, paytype; float amount = 0; int status = 0, orderno = 0; @Override public View getView(int arg0, View arg1, ViewGroup arg2) { Holder holder = null; if (arg1 == null) { holder = new Holder(); arg1 = mInflater.inflate(R.layout.me_myorder_item, null); holder.createtime = (TextView) arg1 .findViewById(R.id.me_myorder_item_createtime); holder.amount = (TextView) arg1 .findViewById(R.id.me_myorder_item_amount); holder.status = (TextView) arg1 .findViewById(R.id.me_myorder_item_status); holder.paytype = (TextView) arg1 .findViewById(R.id.me_myorder_item_paytype); holder.orderno = (TextView) arg1 .findViewById(R.id.me_myorder_item_orderno); arg1.setTag(holder); } else { holder = (Holder) arg1.getTag(); } Map<String, Object> mp = data.get(arg0); createtime = mp.get("createtime") != null ? (mp.get("createtime") + "") : ""; amount = mp.get("amount") != null ? ((Float) mp.get("amount")) : 0; status = mp.get("status") != null ? ((Integer) mp.get("status")) : 0; orderno = mp.get("orderno") != null ? ((Integer) mp.get("orderno")) : 0; paytype = mp.get("paytype") != null ? (mp.get("paytype") + "") : ""; holder.createtime.setText(createtime); holder.amount.setText(amount + ""); if ("alipay".equals(paytype)) { paytype = "支付宝"; } else { paytype = "[未知]"; } holder.orderno.setText(orderno + ""); holder.paytype.setText(paytype); if (status == 0) { holder.status.setTextColor(0xfff08300); holder.status.setText("等待支付"); } else if (status == 1) { holder.status.setTextColor(0x77f08300); holder.status.setText("支付完成,等待结算"); } else if (status == 2) { holder.status.setTextColor(0xff53a000); holder.status.setText("交易完成"); } else { holder.status.setTextColor(0x77222222); holder.status.setText("[未知]"); } return arg1; } } private class Holder { TextView createtime, amount, paytype, status, orderno; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 1) { if (order_list != null) { order_list.startAutoHeightRefresh(1); } } } @Override public void onClearData() { // TODO Auto-generated method stub } @Override public void onVideoBroadcast(Intent intent) { // TODO Auto-generated method stub } }