仿淘宝收货地址,本地数据库详解手机开发

谁说咱们攻城狮不能写出既幽默又能懂的博客呢,本人想推出一系列博文,可以给刚接触Android开发的做一个参考,也可以与接触Android已久的各路大神比较一下,本人喜欢交流,如果有写得不好的地方,欢迎大家指出.如果有更好的效果或者功能,希望大家多多指点,互相学习可以共同进步.

仿淘宝收货地址,本地数据库详解手机开发

说了这么多,现在进入今天的猪蹄,推出<<暴走漫画系列–高仿淘宝收货地址>>,有网购经验的同学应该对这个很熟悉,在淘宝的Android客户端里面,有一个功能是设置收货地址,不清楚的同学可以自己下载个淘宝客户端,自己添加一下看看.


本人一向喜欢贴图,这样有图有真相,才能说服每个人,废话不多说先上自己程序启动画面,然后运行界面,最后一张是淘宝客户端对照图.

仿淘宝收货地址,本地数据库详解手机开发 图片震楼 仿淘宝收货地址,本地数据库详解手机开发 启动界面 仿淘宝收货地址,本地数据库详解手机开发运行界面 仿淘宝收货地址,本地数据库详解手机开发淘宝界面

淘宝客户端无法录屏所以这里只能上静态的,大家可以自己打开淘宝客户端看看.

下面进行详细的代码讲解:

(一)本人在程序启动的时候开了一条后台服务landDivideServeice,目的是为了将全国的省市县总共4,189条数据插入到手机数据库里面去,原本的数据是保存在7个txt里面的,由于放在txt里面,读取操作很不方便,所以我把它放到数据库,在后面的地区筛选很快就出来,这个比淘宝还快哦,不信你们可以试一下.

这里要注意的一点是landDivideServeice是继承IntentService的,不是直接继承Service的.

IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service一样,同时,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。

简单的说IntentService类似于一条后台线程,但操作完成时,我们不用手动去关闭Serveice,他会自动关闭.MyIntentService继承IntentService,之后必须实现一个无参数的构造方法MyIntentService(),然后还要重写onHandleIntent(Intent intent);此后所有的后台操作即可在onHandleIntent(Intent intent)执行;

  1. public class MyIntentService extends IntentService {  
  2.   
  3.     //必须实现的  
  4.     public MyIntentService() {  
  5.         super(“MyIntentService”);  
  6.         // TODO Auto-generated constructor stub  
  7.     }  
  8.   
  9.     @Override  
  10.     protected void onHandleIntent(Intent arg0) {  
  11.         // TODO Auto-generated method stub  
  12.         //具体的操作  
  13.     }  
  14.   
  15. }  

仿淘宝收货地址,本地数据库详解手机开发 so ga…

IntentService详细解析:

http://blog.csdn.net/ryantang03/article/details/8146154

http://blog.csdn.net/flowingflying/article/details/7616333


(二)在程序启动的第一个activity里,我弄了一条倒计时线程,时间总共是20秒,目的是想为数据写入数据库争取多一些时间,如果你进入地址筛选界面,看到数据不全或者报错,是因为数据还没有写完.

实现倒计时功能主要是继承CountDownTimer类,重写onTick和onFinsh这两个方法,onFinish()中的代码是计时器结束的时候要做的事情;onTick(Long m)中的代码是你倒计时开始时要做的事情,构造方法TimeCount()中的两个参数中,前者是倒计的时间数,后者是倒计每秒中间 的间隔时间,都是以毫秒为单位.例如要倒计时30秒,每秒中间间隔时间是1秒,两个参数可以这样写TimeCount(20000,1000).

  1. TimeCount time = new TimeCount(200001000);  
  2. time.start();  

  1. /* 定义一个倒计时的内部类 */  
  2.     class TimeCount extends CountDownTimer {  
  3.         public TimeCount(long millisInFuture, long countDownInterval) {  
  4.             super(millisInFuture, countDownInterval);  
  5.         }  
  6.   
  7.         @Override  
  8.         public void onFinish() {  //倒计时执行结束时操作  
  9.             next = 0;  
  10.         }  
  11.   
  12.         @Override  
  13.         public void onTick(long millisUntilFinished) {  //倒计执行时操作  
  14.         }  
  15.     }  

(三)剩下的主要是一些界面的切换跟数据的读取,只要思路正确是不会被搞混的.

  1. landDivideDB = LandDivideDB.getInstance(getBaseContext());  
  2.         List<LandDivide> landDivide = landDivideDB.queryAddress(“superior=?”new String[]{“1”});  
  3.         Iterator<LandDivide> iterator = null;  
  4.         if(landDivide!=null){  
  5.             iterator = landDivide.iterator();  
  6.   
  7.             while(iterator.hasNext()){  
  8.                 LandDivide l = iterator.next();  
  9.                 sheng.add(l.getName());  
  10.             }  
  11.         }else{  
  12.             return;  
  13.         }  

  1. mListView1.setOnItemClickListener(new OnItemClickListener() {  
  2.   
  3.             @Override  
  4.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  5.                     long arg3) {  
  6.                 // TODO Auto-generated method stub  
  7.                 mLinearLayout1.setVisibility(View.GONE);  
  8.                 mLinearLayout2.setVisibility(View.VISIBLE);  
  9.                 mLinearLayout3.setVisibility(View.GONE);  
  10.                   
  11.                 shi.clear();  
  12.                 String name = sheng.get(position);  
  13.                 String code = null;  
  14.                 shengStr = name;  
  15.                 shengTxt2.setText(name);  
  16.                   
  17.                 List<LandDivide> landDivide = landDivideDB.queryAddress(“name=?”new String[]{name});  
  18.                 Iterator<LandDivide> iterator= landDivide.iterator();  
  19.                 while(iterator.hasNext()){  
  20.                     LandDivide l = iterator.next();  
  21.                     code = l.getCode();  
  22.                     break;  
  23.                 }  
  24.                   
  25.                 List<LandDivide> landDivide_2 = landDivideDB.queryAddress(“superior=?”new String[]{code});  
  26.                 Iterator<LandDivide> iterator_2= landDivide_2.iterator();  
  27.                 while(iterator_2.hasNext()){  
  28.                     LandDivide l = iterator_2.next();  
  29.                     shi.add(l.getName());  
  30.                 }  
  31.                   
  32.                 shiAdapter.notifyDataSetChanged();  
  33.                 quAdapter.clear();  
  34.                 quAdapter.notifyDataSetChanged();  
  35.             }  
  36.         });  
  37.           
  38.         mListView2.setOnItemClickListener(new OnItemClickListener() {  
  39.   
  40.             @Override  
  41.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  42.                     long arg3) {  
  43.                 // TODO Auto-generated method stub  
  44.                   
  45.                 qu.clear();  
  46.                  
  47.                 String name = shi.get(position);  
  48.                 String code = null;  
  49.                   
  50.                 shiStr = name;  
  51.                 shengTxt3.setText(shengStr);  
  52.                 shiTxt3.setText(name);  
  53.                   
  54.                 List<LandDivide> landDivide = landDivideDB.queryAddress(“name=?”new String[]{name});  
  55.                   
  56.                     Iterator<LandDivide> iterator= landDivide.iterator();  
  57.                     while(iterator.hasNext()){  
  58.                         LandDivide l = iterator.next();  
  59.                         code = l.getCode();  
  60.                         break;  
  61.                     }  
  62.                       
  63.                       
  64.                 List<LandDivide> landDivide_2 = landDivideDB.queryAddress(“superior=?”new String[]{code});  
  65.                 if(landDivide_2!=null){  
  66.                     Iterator<LandDivide> iterator_2= landDivide_2.iterator();  
  67.                     while(iterator_2.hasNext()){  
  68.                         LandDivide l = iterator_2.next();  
  69.                         qu.add(l.getName());  
  70.                     }  
  71.                 }  
  72.                   
  73.                   
  74.                   
  75.                  quAdapter.notifyDataSetChanged();  
  76.                    
  77.                  if(qu.size()<1){  
  78.                         mLinearLayout1.setVisibility(View.GONE);  
  79.                         mLinearLayout2.setVisibility(View.VISIBLE);  
  80.                         mLinearLayout3.setVisibility(View.GONE);  
  81.                        
  82.                      Intent i = new Intent(AddressChoose.this,BuyAddress.class);  
  83.                      i.putExtra(“address”, shengStr+“,”+shiStr);  
  84.                      startActivity(i);  
  85.                      finish();  
  86.                        
  87.                  }else{  
  88.                        
  89.                         mLinearLayout1.setVisibility(View.GONE);  
  90.                         mLinearLayout2.setVisibility(View.GONE);  
  91.                         mLinearLayout3.setVisibility(View.VISIBLE);  
  92.                        
  93.                      mListView3.setVisibility(View.VISIBLE);  
  94.                      topView3.setText(“请选择  县区/其他…”);  
  95.                  }  
  96.             }  
  97.         });  
  98.           
  99.         mListView3.setOnItemClickListener(new OnItemClickListener() {  
  100.   
  101.             @Override  
  102.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  103.                     long arg3) {  
  104.                 // TODO Auto-generated method stub  
  105.                   
  106.                 String name = qu.get(position);  
  107.                   
  108.                 quStr = name;  
  109.                   
  110.                 Intent i2 = new Intent(AddressChoose.this,BuyAddress.class);  
  111.                 i2.putExtra(“address”, shengStr+” “+shiStr+” “+quStr);  
  112.                 startActivity(i2);  
  113.                 finish();  
  114.             }  
  115.         });  

仿淘宝收货地址,本地数据库详解手机开发仿淘宝收货地址,本地数据库详解手机开发仿淘宝收货地址,本地数据库详解手机开发仿淘宝收货地址,本地数据库详解手机开发

(四)这个是选择好地址之后的界面,主要还是界面的转换跟焦点的获取

有时候setFocusable(true)跟setFocusableInTouchMode(true)并不能使控件获得焦点,要再之前加上一句requestFocus(),这样控件就可以获得焦点.

仿淘宝收货地址,本地数据库详解手机开发

  1. OnFocusChangeListener focusChanger = new OnFocusChangeListener() {  
  2.   
  3.         @Override  
  4.         public void onFocusChange(View v, boolean hasFocus) {  
  5.             // TODO Auto-generated method stub  
  6.             myAddress.setStreet(jiequText.getText().toString());  
  7.             myAddress.setName(nameText.getText().toString());  
  8.             myAddress.setPhone(phoneText.getText().toString());  
  9.   
  10.             switch (v.getId()) {  
  11.             case R.id.my_set_buyaddress_jiequ:  
  12.                 if (!hasFocus && myAddress.getStreet().length() > 0) {  
  13.                     jiequText.setVisibility(View.GONE);  
  14.                     jiequLinear.setVisibility(View.VISIBLE);  
  15.   
  16.                     jiequTextView.setText(myAddress.getStreet());  
  17.                 }  
  18.   
  19.                 if (hasFocus) {  
  20.                     jiequText.setSelectAllOnFocus(true);  
  21.                 }  
  22.                 break;  
  23.             case R.id.my_set_buyaddress_name:  
  24.                 if (!hasFocus && myAddress.getName().length() > 0) {  
  25.                     nameText.setVisibility(View.GONE);  
  26.                     nameLinear.setVisibility(View.VISIBLE);  
  27.   
  28.                     nameTextView.setText(myAddress.getName());  
  29.                 }  
  30.   
  31.                 if (hasFocus) {  
  32.                     nameText.setSelectAllOnFocus(true);  
  33.                 }  
  34.                 break;  
  35.             case R.id.my_set_buyaddress_phone:  
  36.                 if (!hasFocus && myAddress.getPhone().length() > 0) {  
  37.                     phoneText.setVisibility(View.GONE);  
  38.                     phoneLinear.setVisibility(View.VISIBLE);  
  39.   
  40.                     phoneTextView.setText(myAddress.getPhone());  
  41.                 }  
  42.                 if (hasFocus) {  
  43.                     phoneText.setSelectAllOnFocus(true);  
  44.                 }  
  45.                 break;  
  46.   
  47.             default:  
  48.                 break;  
  49.             }  
  50.         }  
  51.     };  
  52.   
  53.     OnClickListener click = new OnClickListener() {  
  54.   
  55.         @Override  
  56.         public void onClick(View v) {  
  57.             // TODO Auto-generated method stub  
  58.             switch (v.getId()) {  
  59.             case R.id.my_set_buyaddress_sheng_linear:  
  60.                 Intent i = new Intent(BuyAddress.this, AddressChoose.class);  
  61.                 i.putExtra(“Boolean”“aaa”);  
  62.                 startActivity(i);  
  63.                 break;  
  64.             case R.id.my_set_buyaddress_jiequ_linear:  
  65.                 jiequText.setVisibility(View.VISIBLE);  
  66.                 jiequLinear.setVisibility(View.GONE);  
  67.   
  68.                 jiequText.setFocusable(true);  
  69.                 jiequText.setFocusableInTouchMode(true);  
  70.   
  71.                 jiequText.requestFocus();  
  72.                 break;  
  73.             case R.id.my_set_buyaddress_name_linear:  
  74.                 nameText.setVisibility(View.VISIBLE);  
  75.                 nameLinear.setVisibility(View.GONE);  
  76.   
  77.                 nameText.setFocusable(true);  
  78.                 nameText.setFocusableInTouchMode(true);  
  79.   
  80.                 nameText.requestFocus();  
  81.                 break;  
  82.             case R.id.my_set_buyaddress_phone_linear:  
  83.                 phoneText.setVisibility(View.VISIBLE);  
  84.                 phoneLinear.setVisibility(View.GONE);  
  85.   
  86.                 phoneText.setFocusable(true);  
  87.                 phoneText.setFocusableInTouchMode(true);  
  88.   
  89.                 phoneText.requestFocus();  
  90.                 break;  
  91.             case R.id.my_set_buyaddress_address_btn:  
  92.                 myAddress.setStreet(jiequText.getText().toString());  
  93.                 myAddress.setName(nameText.getText().toString());  
  94.                 myAddress.setPhone(phoneText.getText().toString());  
  95.                   
  96.                 if (myAddress.getPhone().length() > 0) {  
  97.                     phoneText.setVisibility(View.GONE);  
  98.                     phoneLinear.setVisibility(View.VISIBLE);  
  99.                     phoneTextView.setText(myAddress.getPhone());  
  100.                 }  
  101.                 postBtn.requestFocus();  
  102.   
  103.                 postBtn.setFocusable(true);  
  104.                 postBtn.setFocusableInTouchMode(true);  
  105.   
  106.                 if (myAddress.getProvinces().length() < 1 || myAddress.getStreet().length() < 1  
  107.                         || myAddress.getName().length() < 1 || myAddress.getPhone().length() < 1) {  
  108.                     Toast.makeText(getBaseContext(), “请完整填写收货人资料”0).show();  
  109.                     return;  
  110.                 }  
  111.                   
  112.                 myAddress.setStatus(checkBox.isChecked());  
  113.                 AddressDB addressDB = AddressDB.getInstance(getBaseContext());  
  114.                   
  115.                 if(checkBox.isChecked()){  
  116.                     List<AddressInfo> list = addressDB.queryAddress();  
  117.                     if(list!=null){  
  118.                         Iterator<AddressInfo> iterator = list.iterator();  
  119.                         while(iterator.hasNext()){  
  120.                             AddressInfo a = iterator.next();  
  121.                             a.setStatus(false);  
  122.                             addressDB.updeteAddress(a);  
  123.                         }  
  124.                     }  
  125.                       
  126.                 }  
  127.                   
  128.                   
  129.                 if (addressinfo != null) {  
  130.                     if(addressDB.updeteAddress(myAddress)){  
  131.                         Toast.makeText(getBaseContext(), “修改收货地址成功”, Toast.LENGTH_LONG).show();  
  132.                     }else{  
  133.                         Toast.makeText(getBaseContext(), “修改收货地址失败”, Toast.LENGTH_LONG).show();  
  134.                     }  
  135.                 } else {  
  136.                       
  137.                     SimpleDateFormat format = new SimpleDateFormat(  
  138.                             “yyyyMMddHHmmss”);  
  139.                     Date date = new Date();  
  140.                     String id = format.format(date);  
  141.                     myAddress.setId(id);  
  142.                       
  143.                     if(addressDB.insertAddress(myAddress)){  
  144.                         Toast.makeText(getBaseContext(), “添加收货地址成功”, Toast.LENGTH_LONG).show();  
  145.                     }else{  
  146.                         Toast.makeText(getBaseContext(), “添加收货地址失败”, Toast.LENGTH_LONG).show();  
  147.                     }  
  148.                 }  
  149.                   
  150.                     Intent intent = new Intent(BuyAddress.this, PersonAddress.class);  
  151.                     startActivity(intent);  
  152.                     finish();  
  153.                   
  154.                   
  155.                 break;  
  156.                   
  157.             case R.id.download_layout1:  
  158.                 finish();  
  159.                 break;  
  160.             default:  
  161.                 break;  
  162.             }  
  163.         }  
  164.     };  
仿淘宝收货地址,本地数据库详解手机开发
仿淘宝收货地址,本地数据库详解手机开发
仿淘宝收货地址,本地数据库详解手机开发
仿淘宝收货地址,本地数据库详解手机开发
仿淘宝收货地址,本地数据库详解手机开发
更多的代码解析,大家可以下载我的demo,看一下哦
最后就这样了哈,有什么不懂的或者更好的建议的,可以提出来哈,欢迎大家指教


小弟不才,参加了CSDN的博客大赛,在这里想请各位帮个忙,投下您尊贵的一票,您说都看到这里了,是不是,底下就是投票的位置了,就顺便投一下嘛(如果底下没有投一票的按钮,请点击下面的链接,进入投票页面,拉到页面底下即可投票). 

仿淘宝收货地址,本地数据库详解手机开发

投票地址:http://vote.blog.csdn.net/Article/Details?articleid=32936187

仿淘宝收货地址,本地数据库详解手机开发

源码地址: http://download.csdn.net/detail/chillax_li/7531409


原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5595.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论