我将快递公司的相关信息,直接放在了res/raw/companyinfos.json,文件里面,然后在应用启动的时候先创建数据库,然后在选择快递公司画面的时候就读取信息。目前信息不多,但是如果信息再增加的话,会出现进入选择快递公司画面的时候数据加载缓慢,空顿的现象。因此我本来想把所有操作放在MyApplication.java中。

创建数据库

/*** 加载数据流程:第一次安装该应用的时候,就创建数据库 并且解析数据插入数据库* * * SD卡 创建数据库目录* * @throws Exception*/public static void loadDatabase(Context context) throws Exception {// 第一次运行应用程序时,加载数据库到data/data/当前包的名称/database/<db_name>File dir = new File("data/data/" + context.getPackageName()+ "/databases");if (!dir.exists() || !dir.isDirectory()) {dir.mkdir();}File file = new File(dir, "mypackage");// file.delete() ;if (!file.exists()) {// 获得封装.db文件的InputStream对象InputStream is = context.getResources().openRawResource(R.raw.mypackage);FileOutputStream fos = new FileOutputStream("data/data/"+ context.getPackageName() + "/databases/mypackage");byte[] buffer = new byte[7168];int count = 0;// 开始复制.db文件while ((count = is.read(buffer)) > 0) {fos.write(buffer, 0, count);}fos.close();is.close();}}

读取数据到数据库

/*** 解析json数据插入数据库* * @param context* */public static void readData2Db(Context context) {InputStream in = context.getResources().openRawResource(R.raw.companyinfos);try {// 将in读入reader 中BufferedReader br = new BufferedReader(new InputStreamReader(in,"GBK"));StringBuffer buffer = new StringBuffer("");String tem = "";while ((tem = br.readLine()) != null) {buffer.append(tem);}br.close();JSONObject myjson = new JSONObject(buffer.toString());JSONArray jsonarr = myjson.getJSONArray("companyinfos");cls_companyinfo companyinfo = new cls_companyinfo();for (int i = 0; i < jsonarr.length(); i++) {JSONObject njson = jsonarr.getJSONObject(i);String compName = njson.getString("name");String compId = njson.getString("id");String helpinfo = njson.getString("helpinfo");String phoneNumber = njson.getString("phonenumber");companyinfo.info_cd = companyinfo.getMaxIndexNo(Common.dbh);companyinfo.name = compName;companyinfo.id = compId;companyinfo.count = "0";companyinfo.helpInfo = helpinfo;companyinfo.phoneNumber = phoneNumber;companyinfo.addData(Common.dbh);}Common.writeConfig(context, Common.HASLOAD_DATABASE, "1");// 加载数据库完成,写入成功标志位} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (JSONException e) {e.printStackTrace();}}

数据:

companyinfos.json

{"companyinfos":[{"name":"EMS","id":"ems","helpinfo":"","phonenumber":"查询电话:11183"},{"name":"申通快递","id":"shentong","helpinfo":"申通单号由12位数字组成,常见以268*、368*、58*等开头","phonenumber":"400-889-5543"},{"name":"邮政包裹","id":"youzhengguonei","helpinfo":"","phonenumber":"11185"},{"name":"圆通速递","id":"yuantong","helpinfo":"圆通单号由10位字母数字组成,常见以6*、8*及V*等开头","phonenumber":"021-69777888"},{"name":"顺丰速运","id":"shunfeng","helpinfo":"顺丰单号由12位数字组成,常以电话区号后3位开头","phonenumber":"4008-111-111"},{"name":"中通速递","id":"zhongtong","helpinfo":"中通单号由12位数字组成,常见以2008**、6**、010等开头","phonenumber":"400-827-0270"},{"name":"韵达快递","id":"yunda","helpinfo":"","phonenumber":"400-821-6789"},{"name":"宅急送","id":"zhaijisong","helpinfo":"","phonenumber":"400-6789-000"},{"name":"天天快递","id":"tiantian","helpinfo":"","phonenumber":"400-188-8888"},{"name":"中邮物流","id":"zhongyouwuliu","helpinfo":"一般是以A或E开头,字母+数字组成,查询时CNPL无需输入","phonenumber":"11183"},{"name":"AAE-中国件","id":"aae","helpinfo":"","phonenumber":""},{"name":"安信达","id":"anxindakuaixi","helpinfo":"","phonenumber":""},{"name":"BHT","id":"bht","helpinfo":"","phonenumber":""},{"name":"百福东方","id":"baifudongfang","helpinfo":"","phonenumber":""},{"name":"CCES(国通快递)","id":"cces","helpinfo":"","phonenumber":""},{"name":"DHL-中国件","id":"dhl","helpinfo":"","phonenumber":"800-810-8000"},{"name":"DPEX","id":"dpex","helpinfo":"","phonenumber":""},{"name":"D速快递","id":"dsukuaidi","helpinfo":"","phonenumber":""},{"name":"大田物流","id":"datianwuliu","helpinfo":"大田物流单号一般是由以6*开头10位纯数字组成","phonenumber":""},{"name":"德邦物流","id":"debangwuliu","helpinfo":"","phonenumber":"400-830-5555"},{"name":"FedEx-国际件","id":"fedex","helpinfo":"","phonenumber":"400-886-1888"},{"name":"飞康达","id":"feikangda","helpinfo":"","phonenumber":""},{"name":"广东邮政","id":"guangdongyouzhengwuliu","helpinfo":"","phonenumber":""},{"name":"能达速递","id":"ganzhongnengda","helpinfo":"","phonenumber":"400-620-1111"},{"name":"汇通快运","id":"huitongkuaidi","helpinfo":"百世汇通单号由12-14位数字字母组成,常见以50*开头","phonenumber":"400-956-5656"},{"name":"天地华宇","id":"tiandihuayu","helpinfo":"","phonenumber":""},{"name":"佳怡物流","id":"jiayiwuliu","helpinfo":"","phonenumber":""},{"name":"佳吉快运","id":"jiajiwuliu","helpinfo":"","phonenumber":"400-820-5566"},{"name":"急先达","id":"jixianda","helpinfo":"","phonenumber":""},{"name":"京广速递","id":"jinguangsudikuaijian","helpinfo":"","phonenumber":""},{"name":"快捷速递","id":"kuaijiesudi","helpinfo":"","phonenumber":""},{"name":"龙邦速递","id":"longbanwuliu","helpinfo":"","phonenumber":""},{"name":"联昊通","id":"lianhaowuliu","helpinfo":"","phonenumber":"0769-88620000"},{"name":"联邦快递","id":"lianbangkuaidi","helpinfo":"","phonenumber":""},{"name":"民航快递","id":"minghangkuaidi","helpinfo":"","phonenumber":""},{"name":"全一快递","id":"quanyikuaidi","helpinfo":"","phonenumber":"400-663-1111"},{"name":"全晨快递","id":"quanchenkuaidi","helpinfo":"","phonenumber":""},{"name":"全日通","id":"quanritongkuaidi","helpinfo":"","phonenumber":""},{"name":"全际通","id":"quanjitong","helpinfo":"","phonenumber":""},{"name":"盛辉物流","id":"shenghuiwuliu","helpinfo":"","phonenumber":""},{"name":"速尔快递","id":"suer","helpinfo":"速尔的快递单号由12位数字组成的","phonenumber":"400-158-9888"},{"name":"三态速递","id":"santaisudi","helpinfo":"","phonenumber":""},{"name":"TNT","id":"tnt","helpinfo":"","phonenumber":""},{"name":"天地华宇","id":"tiandihuayu","helpinfo":"","phonenumber":"400-808-6666"},{"name":"UPS","id":"ups","helpinfo":"","phonenumber":""},{"name":"万家物流","id":"wanjiawuliu","helpinfo":"","phonenumber":""},{"name":"鑫飞鸿","id":"xinhongyukuaidi","helpinfo":"","phonenumber":""},{"name":"CCES(国通快递)","id":"cces","helpinfo":"","phonenumber":""},{"name":"星晨急便","id":"xingchengjibian","helpinfo":"","phonenumber":""},{"name":"新邦物流","id":"xinbangwuliu","helpinfo":"","phonenumber":""},{"name":"信丰物流","id":"xinfengwuliu","helpinfo":"","phonenumber":""},{"name":"亚风速递","id":"yafengsudi","helpinfo":"","phonenumber":"400-1111-119"},{"name":"源伟丰","id":"yuanweifeng","helpinfo":"","phonenumber":""},{"name":"优速物流","id":"youshuwuliu","helpinfo":"","phonenumber":""},{"name":"运通中港","id":"yuntongkuaid","helpinfo":"","phonenumber":""},{"name":"邮政国际","id":"youzhengguoji","helpinfo":"","phonenumber":""},{"name":"越丰物流","id":"yuefengwuliu","helpinfo":"","phonenumber":""},{"name":"远成物流","id":"yuanchengwuliu","helpinfo":"","phonenumber":""},{"name":"中铁快运","id":"zhongtiewuliu","helpinfo":"","phonenumber":"95572"},{"name":"中邮物流","id":"zhongyouwuliu","helpinfo":"","phonenumber":""}]
}

先看下图:

这个画面参照了一个牛人写的,具体地址:http://blog.csdn.net/xiaanming/article/details/12684155

主类:SelectCompanyActivity

package com.xiangxm.checkpackage;import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;import com.xiangxm.cls.cls_companyinfo;
import com.xiangxm.myapp.MyApplication;
import com.xiangxm.sortlistview.CharacterParser;
import com.xiangxm.sortlistview.ClearEditText;
import com.xiangxm.sortlistview.PinyinComparator;
import com.xiangxm.sortlistview.SideBar;
import com.xiangxm.sortlistview.SideBar.OnTouchingLetterChangedListener;
import com.xiangxm.sortlistview.SortAdapter;
import com.xiangxm.sortlistview.SortModel;
import com.xiangxm.utils.Common;/*** 选择快递公司* @author **/
public class SelectCompanyActivity extends Activity implements OnClickListener {private ListView sortListView;private SideBar sideBar;private TextView dialog;private SortAdapter adapter;private ClearEditText mClearEditText;/*** 汉字转换成拼音的类*/private CharacterParser characterParser;private List<SortModel> SourceDateList;/*** 根据拼音来排列ListView里面的数据类*/private PinyinComparator pinyinComparator;private Button backBtn;private Context mcontext;List<cls_companyinfo> companyInfoList = null;private itemOnclickCallBack onclickCallBack;private MyApplication myApplication;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.selectcompany_layout);myApplication = MyApplication.getInstance();companyInfoList = new ArrayList<cls_companyinfo>();mcontext = this;initViews();}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case 0:// 刷新UIbreak;}}};private void initViews() {backBtn = (Button) findViewById(R.id.backBtn);backBtn.setOnClickListener(this);// 实例化汉字转拼音类characterParser = CharacterParser.getInstance();pinyinComparator = new PinyinComparator();sideBar = (SideBar) findViewById(R.id.sidrbar);dialog = (TextView) findViewById(R.id.dialog);sideBar.setTextView(dialog);// 设置右侧触摸监听sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {@Overridepublic void onTouchingLetterChanged(String s) {if (s != null) {// 该字母首次出现的位置int position = adapter.getPositionForSection(s.charAt(0));if (position != -1) {sortListView.setSelection(position);}}}});sortListView = (ListView) findViewById(R.id.country_lvcountry);sortListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 这里要利用adapter.getItem(position)来获取当前position所对应的对象Toast.makeText(getApplication(),((SortModel) adapter.getItem(position)).getName(),Toast.LENGTH_SHORT).show();// view.setBackgroundColor(Color.parseColor("#E0EEEE"));SortModel model = (SortModel) adapter.getItem(position);Bundle bundle = new Bundle();bundle.putString("companyName", model.getName());bundle.putString("companyId", model.getCompanyId());Intent intent = new Intent();intent.putExtras(bundle);setResult(8, intent);overridePendingTransition(R.anim.push_left_out,R.anim.push_left_in);SelectCompanyActivity.this.finish();}});// 动画效果    AnimationSet set = new AnimationSet(true);Animation animation = new AlphaAnimation(0.0f, 1.0f);animation.setDuration(50);set.addAnimation(animation);animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);animation.setDuration(100);set.addAnimation(animation);LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);sortListView.setLayoutAnimation(controller);mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);// 根据输入框输入值的改变来过滤搜索mClearEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {// 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表filterData(s.toString());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {}});companyInfoList = cls_companyinfo.getCompanyInfoList(Common.dbh);// companyInfoList.addAll(myApplication.getCompanyInfoList()) ;SourceDateList = filledData(companyInfoList);// 数据源// 根据a-z进行排序源数据Collections.sort(SourceDateList, pinyinComparator);adapter = new SortAdapter(SelectCompanyActivity.this, SourceDateList);sortListView.setAdapter(adapter);changeBtnBackground();// 修改按钮背景色}/*** 为ListView填充数据* * @param date* @return*/private List<SortModel> filledData(List<cls_companyinfo> list) {List<SortModel> mSortList = new ArrayList<SortModel>();int size = list.size();for (int i = 0; i < size; i++) {SortModel sortModel = new SortModel();sortModel.setName(list.get(i).name);sortModel.setCompanyId(list.get(i).id);// 汉字转换成拼音String pinyin = characterParser.getSelling(list.get(i).name);String sortString = pinyin.substring(0, 1).toUpperCase();// 正则表达式,判断首字母是否是英文字母if (sortString.matches("[A-Z]")) {sortModel.setSortLetters(sortString.toUpperCase());} else {sortModel.setSortLetters("#");}mSortList.add(sortModel);}return mSortList;}/*** 根据输入框中的值来过滤数据并更新ListView* * @param filterStr*/private void filterData(String filterStr) {List<SortModel> filterDateList = new ArrayList<SortModel>();if (TextUtils.isEmpty(filterStr)) {filterDateList = SourceDateList;} else {filterDateList.clear();for (SortModel sortModel : SourceDateList) {String name = sortModel.getName();if (name.indexOf(filterStr.toString()) != -1|| characterParser.getSelling(name).startsWith(filterStr.toString())) {filterDateList.add(sortModel);}}}// 根据a-z进行排序Collections.sort(filterDateList, pinyinComparator);adapter.updateListView(filterDateList);}public void setOnclickCallBack(itemOnclickCallBack onclickCallBack) {this.onclickCallBack = onclickCallBack;}/*** 接口用于回调* * @author * */public interface itemOnclickCallBack {void itemOnClick(HashMap<String, String> map);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.backBtn) {// 回退this.finish();// overridePendingTransition(R.anim.push_left_out,// R.anim.push_left_in);}}/*** 修改按钮背景图片*/public void changeBtnBackground() {class MyBtnOnTouch implements OnTouchListener {int drawable[] = null;@Overridepublic boolean onTouch(View v, MotionEvent event) {drawable = new int[] { R.drawable.back_button_selected,R.drawable.back_button_normal };switch (event.getAction()) {case MotionEvent.ACTION_DOWN:v.setBackgroundResource(drawable[0]);break;case MotionEvent.ACTION_UP:v.setBackgroundResource(drawable[1]);break;}return false;}}backBtn.setOnTouchListener(new MyBtnOnTouch());}}

查询快递中,快递公司数据信息的问题相关推荐

  1. mysql查询集合中的特定数据_快速查询List中指定的数据

    时间:2017/5/15 作者:李国君 题目:快速查询List中指定的数据 背景:当List中保存了大量的数据时,用传统的方法去遍历指定的数据肯定会效率低下,有一个方法就是类似于数据库查询那样,根据索 ...

  2. 查询数据库中的表结构信息

    查询数据库中的表结构信息 SELECT COLUMN_NAME 列名, COLUMN_TYPE 数据类型, DATA_TYPE 字段类型, CHARACTER_MAXIMUM_LENGTH 长度, I ...

  3. pandas使用query函数查询dataframe中某一个数据列在指定数据范围的数据行(rows where value is between two values in dataframe)

    pandas使用query函数查询dataframe中某一个数据列在指定数据范围的数据行(rows where value is between two values in dataframe) 目录

  4. mysql 查询数据库中所有表的信息

    查询数据库中的所有表格信息: SELECT table_name, table_comment, create_time, update_time FROM information_schema.ta ...

  5. 怎样在微信中清理数据存储空间?经验技巧!如何在微信中清除缓存数据信息?

    现在微信已经成为我们大家生活中的一款主流聊天软件了,在生活中我们都会使用微信来与朋友家人之间进行聊天互动,在工作中使用微信发布信息与客户同事之间进行交流沟通,在我们大家使用微信的同时,会产生许多的缓存 ...

  6. 查询数据库中的重复数据记录

    ========第一篇========= 在一张表中某个字段下面有重复记录,有很多方法,但是有一个方法,是比较高效的,如下语句: select data_guid from adam_entity_d ...

  7. 科创板公司数据信息爬取

    最近随着首批科创板的公司正式开市交易,一百多家科创公司的信息也逐渐披露与完善,虽然购买的要求很高,限制也较大,但是...嗯...买不起就是买不起~虽然没条件买,但是深入了解下各个公司的数据信息还是可以 ...

  8. AirSim (六) ---理解篇: Airsim中的IMU数据信息

    目录 1 理解好IMU的输出数据 2 AirSim中如何生成IMU数据 3 AirSim中如何读取IMU数据 4. IMU的数据 in AirSim and ROS IMU Message 1 理解好 ...

  9. java数据查询_Java中的大量数据查询

    问题描述:在通常的三层构架下,客户通过Browser请求Web服务器查询数据库,而查询结果是上千条甚至是上百万条记录,要求查询 问题描述:在通常的三层构架下,客户通过Browser请求Web服务器查询 ...

最新文章

  1. matlab单机无限大系统_基于MATLAB的单机无穷大系统短路故障分析_吕鹏
  2. JS的表单序列化,数组去重,判断数组是否重复等方法
  3. Swift之String的简单实用
  4. 【docker】docker虚拟容器的使用大全
  5. 现代软件工程讲义 源代码管理
  6. springboot转发http请求_Spring Boot2 系列教程(八)Spring Boot 中配置 Https
  7. python perl 正则_为什么说perl的正则表达式功能比Python强大
  8. dsp java_GitHub - GrayOxygen/JavaDsp: 数字信号处理(DSP)方面的Java封装,包含常用的一些处理方法,如滤波、信号变换等等。...
  9. java linux 时间戳转时间_Java时间和时间戳的相互转换
  10. python传输视频文件_以字节形式将视频文件流式传输到stdin
  11. 使用Netflix Archaius进行配置管理
  12. 计算机网络密码用户名是什么,宽带连接的用户名和密码是什么
  13. 程序员转行量化交易可行吗?
  14. Python之条件竞争
  15. 【ORACLE】ORACLE IMPDP导入提示ORA-01918:user‘XXX’ does not exist
  16. 逆向教程2 某信营业厅
  17. Python4班平均成绩统计_2021小升初必看!成都各个初中成绩如何?师资咋样?不同成绩的孩子该怎么准备?...
  18. 【木棍问题——回溯与分支限界算法(使用C语言解决)】
  19. 百病之源五脏为根(国粹---gt;中医;在此提倡自然疗法!)
  20. 2.1spring cloud 环境配置

热门文章

  1. 小米机器人扫地机贴纸_小米扫地机亲测,真的好用
  2. 10.WebRTC实现网页录音功能
  3. 杀疯了!本科大神毕设:3D虚拟主播软件!独创AR直播!支持OBS!开源!
  4. 铂链获美国顶级区块链投资基金 Draper Dragon Digital Fund投资
  5. Idea中Translation翻译插件失败问题解决
  6. DISPO与EKGRP 的关系(MRP控制者和采购组)
  7. shell 中 ()、(())、[]、[[]]、{} 的作用
  8. Tuxera NTFS for Mac 2022新版本安装
  9. 苹果信号100就无服务器,iPhone12双卡无信号是什么情况?iPhone12无信号解决方法...
  10. Oracle表空间及表分区