package com.electric.chargingpile.activity; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.os.Bundle; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.electric.chargingpile.R; import com.electric.chargingpile.application.MainApplication; import com.electric.chargingpile.data.RouteInfo; import com.electric.chargingpile.manager.ProfileManager; import com.electric.chargingpile.util.BarColorUtil; import com.electric.chargingpile.util.DBOpenHandlerRoute; import com.electric.chargingpile.util.NetUtil; import com.electric.chargingpile.util.ToastUtil; import com.umeng.analytics.MobclickAgent; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class RoutePlanShowAddressActivity extends Activity implements View.OnClickListener { private ImageView iv_back, iv_jiaohuan; private TextView tv_submit, tv_home, tv_company, tv_go_history; public static TextView tv_start, tv_end; private RelativeLayout rl_usually_address, rl_home, rl_company; private String home_name, home_address, home_jing, home_wei, company_name, company_address, compang_jing, company_wei; private ListView historyList; private List hList = new ArrayList(); // private List> list = new ArrayList>(); private RouteInfo history; private View convertView = null; private RouteAdapter adapter; public static String start_address = "", start_jing = "", start_wei = "", end_address = "", end_jing = "", end_wei = ""; public static String start_name = "我的位置"; public static String end_name = ""; private int i = 0; public static final int REQUSET = 11; private TextView tv_default, tv_dbyd, tv_bmsf, tv_bzgs, tv_gsyx; private LinearLayout ll_setting; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_route_plan_show_address); BarColorUtil.initStatusBarColor(RoutePlanShowAddressActivity.this); initViews(); } private void initViews() { tv_go_history = (TextView) findViewById(R.id.tv_go_history); tv_go_history.setOnClickListener(this); ll_setting = (LinearLayout) findViewById(R.id.ll_setting); ll_setting.setOnClickListener(this); iv_back = (ImageView) findViewById(R.id.iv_back); iv_back.setOnClickListener(this); iv_jiaohuan = (ImageView) findViewById(R.id.iv_jiaohuan); iv_jiaohuan.setOnClickListener(this); tv_start = (TextView) findViewById(R.id.tv_start); tv_start.setOnClickListener(this); tv_end = (TextView) findViewById(R.id.tv_end); tv_end.setOnClickListener(this); tv_end.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // if (tv_end.getText().toString().equals("") ||tv_start.getText().toString().equals("")){ // tv_submit.setBackgroundResource(R.color.huise); // tv_submit.setEnabled(false); // }else { // tv_submit.setBackgroundResource(R.color.lvse); // tv_submit.setEnabled(true); // } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (tv_end.getText().toString().equals("") || tv_start.getText().toString().equals("")) { tv_submit.setBackgroundResource(R.color.huise); tv_submit.setEnabled(false); } else { tv_submit.setBackgroundResource(R.color.lvse); tv_submit.setEnabled(true); } } @Override public void afterTextChanged(Editable s) { Log.e("end==", tv_end.getText().toString()); if (tv_end.getText().toString().equals("") || tv_start.getText().toString().equals("")) { tv_submit.setBackgroundResource(R.color.huise); tv_submit.setEnabled(false); } else { tv_submit.setBackgroundResource(R.color.lvse); tv_submit.setEnabled(true); } } }); tv_submit = (TextView) findViewById(R.id.tv_submit); tv_submit.setOnClickListener(this); rl_usually_address = (RelativeLayout) findViewById(R.id.rl_usually_address); rl_usually_address.setOnClickListener(this); rl_home = (RelativeLayout) findViewById(R.id.rl_home); rl_home.setOnClickListener(this); rl_company = (RelativeLayout) findViewById(R.id.rl_company); rl_company.setOnClickListener(this); tv_home = (TextView) findViewById(R.id.tv_home); tv_company = (TextView) findViewById(R.id.tv_company); historyList = (ListView) findViewById(R.id.lv_history_list); historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // hList } }); convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_searchclear, null); historyList.addFooterView(convertView); TextView tv_clear = (TextView) convertView.findViewById(R.id.tv_searchClear); tv_clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new com.electric.chargingpile.view.AlertDialog(RoutePlanShowAddressActivity.this).builder() .setMsg("您确定要清空搜索历史吗?") .setPositiveButton("确定", new View.OnClickListener() { @Override public void onClick(View v) { DBOpenHandlerRoute db = new DBOpenHandlerRoute(RoutePlanShowAddressActivity.this, ""); db.delTable(); historyList.setVisibility(View.GONE); adapter.notifyDataSetChanged(); } }).setNegativeButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { } }).show(); } }); tv_default = (TextView) findViewById(R.id.tv_default); tv_dbyd = (TextView) findViewById(R.id.tv_dbyd); tv_bmsf = (TextView) findViewById(R.id.tv_bmsf); tv_bzgs = (TextView) findViewById(R.id.tv_bzgs); tv_gsyx = (TextView) findViewById(R.id.tv_gsyx); // showSettingViews(); } private void showSettingViews() { boolean dbyd = ProfileManager.getInstance().getDbyd(getApplicationContext()); boolean bmsf = ProfileManager.getInstance().getBmsf(getApplicationContext()); boolean bzgs = ProfileManager.getInstance().getBzgs(getApplicationContext()); boolean gxyx = ProfileManager.getInstance().getGsyx(getApplicationContext()); // Log.e("dbyd==",ProfileManager.getInstance().getDbyd(getApplicationContext())+""); // Log.e("bmsf==",ProfileManager.getInstance().getBmsf(getApplicationContext())+""); // Log.e("bzgs==",ProfileManager.getInstance().getBzgs(getApplicationContext())+""); // Log.e("gxyx==",ProfileManager.getInstance().getGsyx(getApplicationContext()) +""); if (!dbyd && !bmsf && !bzgs && !gxyx) { tv_default.setVisibility(View.VISIBLE); tv_dbyd.setVisibility(View.GONE); tv_bmsf.setVisibility(View.GONE); tv_bzgs.setVisibility(View.GONE); tv_gsyx.setVisibility(View.GONE); } else { tv_default.setVisibility(View.GONE); tv_dbyd.setVisibility(View.GONE); tv_bmsf.setVisibility(View.GONE); tv_bzgs.setVisibility(View.GONE); tv_gsyx.setVisibility(View.GONE); if (dbyd) { tv_dbyd.setVisibility(View.VISIBLE); } if (bmsf) { tv_bmsf.setVisibility(View.VISIBLE); } if (bzgs) { tv_bzgs.setVisibility(View.VISIBLE); } if (gxyx) { tv_gsyx.setVisibility(View.VISIBLE); } } } private void getSpData() { SharedPreferences mySharedPreferences = getSharedPreferences("routeHomeInfo", Activity.MODE_PRIVATE); home_name = mySharedPreferences.getString("home_name", ""); home_address = mySharedPreferences.getString("home_address", ""); home_jing = mySharedPreferences.getString("home_jing", ""); home_wei = mySharedPreferences.getString("home_wei", ""); SharedPreferences mSharedPreferences = getSharedPreferences("routeCompanyInfo", Activity.MODE_PRIVATE); company_name = mSharedPreferences.getString("company_name", ""); company_address = mSharedPreferences.getString("company_address", ""); compang_jing = mSharedPreferences.getString("company_jing", ""); company_wei = mSharedPreferences.getString("company_wei", ""); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { RoutePlanShowAddressActivity.start_name = "我的位置"; RoutePlanShowAddressActivity.start_address = ""; RoutePlanShowAddressActivity.start_jing = ""; RoutePlanShowAddressActivity.start_wei = ""; RoutePlanShowAddressActivity.end_name = ""; RoutePlanShowAddressActivity.end_address = ""; RoutePlanShowAddressActivity.end_jing = ""; RoutePlanShowAddressActivity.end_wei = ""; finish(); return true; } return super.onKeyDown(keyCode, event); } // private void changeText(int i){ // if (i%2==0){ // tv_start.setText(start_name); // tv_end.setText(end_name); // }else { // tv_start.setText(end_name); // tv_end.setText(start_name); // } // } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) { return; } switch (requestCode) { // 如果是直接从相册获取 case REQUSET: showSettingViews(); default: break; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_go_history: if (MainApplication.isLogin()) { MobclickAgent.onEvent(getApplicationContext(), "0303"); startActivity(new Intent(getApplicationContext(), RouteHistoryListActivity.class)); } else { startActivity(new Intent(getApplicationContext(), LoginActivity.class)); ToastUtil.showToast(getApplicationContext(), "请先登录", Toast.LENGTH_SHORT); } break; case R.id.ll_setting: MobclickAgent.onEvent(getApplicationContext(), "0302", new HashMap().put("type", "")); startActivityForResult(new Intent(RoutePlanShowAddressActivity.this, RoutePreferenceActivity.class), REQUSET); break; case R.id.iv_jiaohuan: // if (tv_end.getText().toString().equals("") || tv_start.getText().toString().equals("")){ // ToastUtil.showToast(getApplicationContext(),"请先输入终点",Toast.LENGTH_SHORT); // }else { i++; String s_start = tv_start.getText().toString(); String s_end = tv_end.getText().toString(); // Log.e("!!!",s_start+"---"+s_end); tv_start.setText(s_end); tv_end.setText(s_start); // } // Toast.makeText(this,i+"",Toast.LENGTH_SHORT).show(); break; case R.id.iv_back: RoutePlanShowAddressActivity.start_name = "我的位置"; RoutePlanShowAddressActivity.start_address = ""; RoutePlanShowAddressActivity.start_jing = ""; RoutePlanShowAddressActivity.start_wei = ""; RoutePlanShowAddressActivity.end_name = ""; RoutePlanShowAddressActivity.end_address = ""; RoutePlanShowAddressActivity.end_jing = ""; RoutePlanShowAddressActivity.end_wei = ""; tv_start.setText(start_name); tv_end.setText(end_name); finish(); break; case R.id.tv_start: i = 0; Intent intent3 = new Intent(this, RoutePlanSelectAddressActivity.class); intent3.putExtra("tag", "start"); startActivity(intent3); break; case R.id.tv_end: i = 0; Intent intent4 = new Intent(this, RoutePlanSelectAddressActivity.class); intent4.putExtra("tag", "end"); startActivity(intent4); break; case R.id.tv_submit: if (!NetUtil.CheckNetwork(getApplicationContext())) { Toast.makeText(getApplicationContext(), "请求失败,当前无网络连接", Toast.LENGTH_SHORT).show(); return; } if (start_name.equals("") || null == start_name) { start_name = "我的位置"; } if (end_name.equals("") || null == end_name) { end_name = "我的位置"; } if (i % 2 == 0) { DBOpenHandlerRoute db = new DBOpenHandlerRoute(RoutePlanShowAddressActivity.this, ""); Log.e("!!!", tv_start.getText().toString() + "---" + tv_end.getText().toString()); db.queryOne(tv_start.getText().toString(), tv_end.getText().toString()); ContentValues values = new ContentValues(); values.put("time", System.currentTimeMillis()); values.put("start_name", start_name); values.put("start_address", start_address); values.put("start_jing", start_jing); values.put("start_wei", start_wei); values.put("end_name", end_name); values.put("end_address", end_address); values.put("end_jing", end_jing); values.put("end_wei", end_wei); db.insert(values); Cursor c = db.query(); System.err.println(c.getCount()); if (c.getCount() <= 0) { return; } historyList.setAdapter(adapter); // adapter.notifyDataSetChanged(); Intent intent = new Intent(this, RoutePlanMapActivity.class); intent.putExtra("start_name", start_name); intent.putExtra("start_address", start_address); intent.putExtra("start_jing", start_jing); intent.putExtra("start_wei", start_wei); intent.putExtra("end_name", end_name); intent.putExtra("end_address", end_address); intent.putExtra("end_jing", end_jing); intent.putExtra("end_wei", end_wei); intent.putExtra("save_id", "0"); startActivity(intent); MobclickAgent.onEvent(getApplicationContext(), "0304"); } else { DBOpenHandlerRoute db = new DBOpenHandlerRoute(RoutePlanShowAddressActivity.this, ""); Log.e("!!!", tv_start.getText().toString() + "---" + tv_end.getText().toString()); db.queryOne(tv_start.getText().toString(), tv_end.getText().toString()); ContentValues values = new ContentValues(); values.put("time", System.currentTimeMillis()); values.put("start_name", end_name); values.put("start_address", end_address); values.put("start_jing", end_jing); values.put("start_wei", end_wei); values.put("end_name", start_name); values.put("end_address", start_address); values.put("end_jing", start_jing); values.put("end_wei", start_wei); db.insert(values); Cursor c = db.query(); System.err.println(c.getCount()); if (c.getCount() <= 0) { return; } historyList.setAdapter(adapter); // adapter.notifyDataSetChanged(); Intent intent = new Intent(this, RoutePlanMapActivity.class); intent.putExtra("start_name", end_name); intent.putExtra("start_address", end_address); intent.putExtra("start_jing", end_jing); intent.putExtra("start_wei", end_wei); intent.putExtra("end_name", start_name); intent.putExtra("end_address", start_address); intent.putExtra("end_jing", start_jing); intent.putExtra("end_wei", start_wei); intent.putExtra("save_id", "0"); startActivity(intent); MobclickAgent.onEvent(getApplicationContext(), "0304"); } // RoutePlanShowAddressActivity.start_name = "我的位置"; // RoutePlanShowAddressActivity.start_address = ""; // RoutePlanShowAddressActivity.start_jing = ""; // RoutePlanShowAddressActivity.start_wei = ""; // RoutePlanShowAddressActivity.end_name = ""; // RoutePlanShowAddressActivity.end_address = ""; // RoutePlanShowAddressActivity.end_jing = ""; // RoutePlanShowAddressActivity.end_wei = ""; // tv_start.setText(start_name); // tv_end.setText(end_name); // finish(); break; case R.id.rl_home: if (!NetUtil.CheckNetwork(getApplicationContext())) { Toast.makeText(getApplicationContext(), "请求失败,当前无网络连接", Toast.LENGTH_SHORT).show(); return; } MobclickAgent.onEvent(getApplicationContext(), "0305"); if (home_name.equals("")) { Intent intent = new Intent(this, RoutePlanSelectAddressActivity.class); intent.putExtra("tag", "home"); startActivity(intent); } else { Intent intent = new Intent(this, RoutePlanMapActivity.class); intent.putExtra("start_name", "我的位置"); intent.putExtra("start_address", ""); intent.putExtra("start_jing", ""); intent.putExtra("start_wei", ""); intent.putExtra("end_name", home_name); intent.putExtra("end_address", home_address); intent.putExtra("end_jing", home_jing); intent.putExtra("end_wei", home_wei); intent.putExtra("save_id", "0"); startActivity(intent); // Log.e("start===",home_jing+"---"+home_wei); // Toast.makeText(this,"规划",Toast.LENGTH_SHORT).show(); } break; case R.id.rl_company: if (!NetUtil.CheckNetwork(getApplicationContext())) { Toast.makeText(getApplicationContext(), "请求失败,当前无网络连接", Toast.LENGTH_SHORT).show(); return; } MobclickAgent.onEvent(getApplicationContext(), "0306"); if (company_name.equals("")) { Intent intent2 = new Intent(this, RoutePlanSelectAddressActivity.class); intent2.putExtra("tag", "company"); startActivity(intent2); } else { Intent intent = new Intent(this, RoutePlanMapActivity.class); intent.putExtra("start_name", "我的位置"); intent.putExtra("start_address", ""); intent.putExtra("start_jing", ""); intent.putExtra("start_wei", ""); intent.putExtra("end_name", company_name); intent.putExtra("end_address", company_address); intent.putExtra("end_jing", compang_jing); intent.putExtra("end_wei", company_wei); intent.putExtra("save_id", "0"); startActivity(intent); // Log.e("start===",compang_jing+"---"+company_wei); // Toast.makeText(this,"规划",Toast.LENGTH_SHORT).show(); // Toast.makeText(this,"规划",Toast.LENGTH_SHORT).show(); } break; case R.id.rl_usually_address: Intent intent1 = new Intent(this, RouteEditAddressActivity.class); startActivity(intent1); break; default: break; } } @Override protected void onResume() { super.onResume(); showSettingViews(); MobclickAgent.onResume(this); getSpData(); if (!home_name.equals("")) { tv_home.setText(home_name); } else { tv_home.setText("请设置家的位置"); } if (!company_name.equals("")) { tv_company.setText(company_name); } else { tv_company.setText("请设置公司的位置"); } // tv_start.setText(start_name); // tv_end.setText(end_name); if (!tv_start.getText().toString().equals("") && !tv_end.getText().toString().equals("")) { tv_submit.setBackgroundResource(R.color.lvse); tv_submit.setEnabled(true); } DBOpenHandlerRoute db = DBOpenHandlerRoute.getInstance(RoutePlanShowAddressActivity.this, ""); Cursor c = db.query(); System.err.println(c.getCount()); if (c.getCount() <= 0) { return; } hList.clear(); while (c.moveToNext()) { history = new RouteInfo(); history.setStart_name(c.getString(c .getColumnIndex("start_name"))); history.setStart_address(c.getString(c .getColumnIndex("start_address"))); history.setStart_jing(c.getString(c .getColumnIndex("start_jing"))); history.setStart_wei(c.getString(c .getColumnIndex("start_wei"))); history.setEnd_name(c.getString(c .getColumnIndex("end_name"))); history.setEnd_address(c.getString(c .getColumnIndex("end_address"))); history.setEnd_jing(c.getString(c .getColumnIndex("end_jing"))); history.setEnd_wei(c.getString(c .getColumnIndex("end_wei"))); hList.add(history); } Log.e("size===", hList.size() + ""); adapter = new RouteAdapter(this, hList); historyList.setAdapter(adapter); } @Override protected void onPause() { super.onPause(); MobclickAgent.onPause(this); } public class RouteAdapter extends BaseAdapter { private Context context; private List list; public RouteAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.route_item, null); viewHolder = new ViewHolder(); viewHolder.name = (TextView) convertView .findViewById(R.id.tv_route); viewHolder.rl_route = (RelativeLayout) convertView.findViewById(R.id.rl_route); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } String s = list.get(position).getStart_name(); String s1 = " — " + list.get(position).getEnd_name(); String html = "" + s + ""; html += "" + s1 + ""; CharSequence charSequence = Html.fromHtml(html); viewHolder.name.setText(charSequence); viewHolder.rl_route.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!NetUtil.CheckNetwork(getApplicationContext())) { Toast.makeText(getApplicationContext(), "请求失败,当前无网络连接", Toast.LENGTH_SHORT).show(); return; } Intent intent = new Intent(getApplicationContext(), RoutePlanMapActivity.class); intent.putExtra("start_name", list.get(position).getStart_name()); intent.putExtra("start_address", list.get(position).getStart_address()); intent.putExtra("start_jing", list.get(position).getStart_jing()); intent.putExtra("start_wei", list.get(position).getStart_wei()); intent.putExtra("end_name", list.get(position).getEnd_name()); intent.putExtra("end_address", list.get(position).getEnd_address()); intent.putExtra("end_jing", list.get(position).getEnd_jing()); intent.putExtra("end_wei", list.get(position).getEnd_wei()); intent.putExtra("save_id", "0"); startActivity(intent); // finish(); } }); return convertView; } class ViewHolder { TextView name; RelativeLayout rl_route; } } }