根据公司项目~搭建开发的框架(三)

系统 1559 0

 
根据公司项目~搭建开发的框架(三)
 


效果图
初始


根据公司项目~搭建开发的框架(三)
 
输入数据 Add


根据公司项目~搭建开发的框架(三)
 
显示数据 Query


根据公司项目~搭建开发的框架(三)
 
实现过程的主要代码:
  • Click事件中   case   R.id. add :
                         Person   person = new   Person();
                          person . name = name .getText().toString();
                          person . age =Integer. valueOf ( age .getText().toString());
                          Map   param= new   HashMap ();
                          param.put( "person" ,   person ) ;
                          task   =   new   Task(Config. TASK_ADD , param);
                         MainService. addNewTask ( task );
                          break ;
                   case   R.id. query :           
                          task   =   new   Task(Config. TASK_QUERY ,   null );
                         MainService. addNewTask ( task );
                          break ;
  • 开始执行任务  public   static   void   addNewTask( final   Task ts) {
                   new   Thread() {
                          public   void   run() {
                                mainService .doTask(ts);
                         }
                  }.start();
           }

            public   void   doTask(Task task) {
                   if   (task ==   null )
                          return ;
                   Map   param = task.getTaskParam();
                   ArrayList   list =   new   ArrayList<Object>();
                  Message message =   handler .obtainMessage();
                   int   taskid = task.getTaskID();
                   switch   (taskid) {
                   case   Config. TASK_ONE :
                         String test =   "test" ;
                         message. obj   = test;
                          break ;
                   case   Config. TASK_ADD :
                          personService .insertPerson((Person)param.get( "person" ));
                          break ;
                   case   Config. TASK_QUERY :
                         list= personService .getPersonList();
                         message. obj   = list;
                          break ;
                  }
                  message. what   = task.getTaskID();
                   handler .sendMessage(message);
           }

           
  • PersonService介绍 ,实现增加和查询的功能 public   ArrayList<Person> getPersonList() {

                  Cursor cursor =   dbHelper .query( dbHelper . TB_PERSON ,   new   String[] {   "_id" ,
                                "name" ,   "age"   },   null ,   null ,   null ,   null ,   null ,   null );
                  ArrayList<Person> list =   new   ArrayList<Person>();
                   while   (cursor.moveToNext()) {
                         Person person =   new   Person();
                         person. id   = cursor.getInt(0);
                         person. name   = cursor.getString(cursor.getColumnIndex( "name" ));
                         person. age =cursor.getInt(2);
                         list.add(person);
                  }
                  cursor.close();
                   return   list;
           }
    public   void   insertPerson(Person person) {
                  ContentValues cv =   new   ContentValues();
                  cv.put( "name" , person. name );
                  cv.put( "age" , person. age );
                   dbHelper .insert( dbHelper . TB_PERSON , cv);
           }

  • 返回数据给activity   
          public   Handler   handler   =   new   Handler() {

               @Override
               public   void   handleMessage(Message msg) {
                      super .handleMessage(msg);
                     IActivity ac =   null ;
                     String activityName =   "" ;
                      switch   (msg. what ) {
                      case   Config. TASK_ONE :
                           activityName =   "AndroidTaskActivity" ;
                            break ;
                      case   Config. TASK_QUERY :
                           activityName =   "AndroidTaskActivity" ;
                            break ;
                     }
                     ac = MainService. getActivityByName (activityName);
                     ac.refresh(msg. what , msg. obj );
              }
       };
     

  • 显示在当前activity
      public   void   refresh(Object... param) {
               int   flag = (Integer) param[0];
               switch   (flag) {
               case   Config. TASK_ONE :
                     String str = (String) param[1];
                      et .setText(str);
                      break ;
               case   Config. TASK_QUERY :
                      ArrayList   ls= new   ArrayList ();
                     ls=( ArrayList ) param[1];
                     Person P= new   Person();
                     P=(Person) ls.get(1);
                      et .setText( "name:  " +P. name + "  age:  " +P. age + "  id:  " +P. id );
                      break ;
              
                     
              }

       }

根据公司项目~搭建开发的框架(三)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论