zoukankan      html  css  js  c++  java
  • android TabActivity及全局变量

    1.继承TabActivity代码如下:

    public class TabProject extends TabActivity {
        TabHost th;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tabl_main);
            th=getTabHost();
            HttpData hd = new HttpData();//访问web的类
            List<Map<String, Object>> lists = hd.getPost("w034");//取得数据
            ListMap listmap = (ListMap)getApplication();//得到全局变量实例
            listmap.setLists(lists);//把数据添加到实例中
            //调用以下方法,为tabactivity添加tab
            addTaskTab();
            addNoticeTab();
            addRizhiTab();
            TabWidget tabWidget = th.getTabWidget();
          //设定tab标签的高度
              for (int i = 0; i < tabWidget.getChildCount(); i++)
              {
              tabWidget.getChildAt(i).getLayoutParams().height = 50;
              }
    
    }
        
        public void addTaskTab(){
        //通过新建Activity实现tab Intent intent
    = new Intent(); intent.setClass(TabProject.this,TaskActivity.class); TabSpec ts = th.newTabSpec("task");
         //添加tab的lable ts.setIndicator(getString(R.string.task_project),
    null); ts.setContent(intent); th.addTab(ts); } public void addNoticeTab(){ Intent intent = new Intent(); intent.setClass(TabProject.this, NoticeActivity.class); TabSpec spec = th.newTabSpec("notice"); spec.setIndicator(getString(R.string.notice_task), null); spec.setContent(intent); th.addTab(spec); } public void addRizhiTab(){ Intent intent = new Intent(); intent.setClass(TabProject.this, RizhiActivity.class); TabSpec spec = th.newTabSpec("notice"); spec.setIndicator(getString(R.string.rizhi_project), null); spec.setContent(intent); th.addTab(spec); } }

     TabProject对应的layout  xml其中的控件id都是固定的

    <?xml version="1.0" encoding="utf-8"?>   
    <TabHost   
        xmlns:android="http://schemas.android.com/apk/res/android"   
        android:id="@android:id/tabhost"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent">   
        <LinearLayout   
            android:orientation="vertical"   
            android:layout_width="fill_parent"   
            android:layout_height="fill_parent">   
          
            <TabWidget android:id="@android:id/tabs"   
                android:layout_width="fill_parent"   
                android:layout_height="wrap_content"   
                android:paddingLeft="1dip"   
                android:paddingRight="1dip"   
                android:paddingTop="4dip"   
            />   
               
            <FrameLayout android:id="@android:id/tabcontent"   
                android:layout_width="fill_parent"   
                android:layout_height="0dip"   
                android:layout_weight="1"   
            />        
       </LinearLayout>   
    </TabHost>

    其中的一个tab的activity这个activity继承了ListActivit

    public class NoticeActivity extends ListActivity {
        private List<Map<String, Object>> noticelist= new ArrayList<Map<String,Object>>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            ListMap listmap = (ListMap)getApplication();
            for(int i=0;i<listmap.getLists().size();i++){
                Map<String,Object> map = listmap.getLists().get(i);
                if(map.get("sortname").equals("通知")){
                    noticelist.add(map);
                }
            }
            SimpleAdapter sa = new SimpleAdapter(this,noticelist, R.layout.task_list,new String[]{"taskid","title","status","date"},new int[]{R.id.taskid,R.id.title,R.id.status,R.id.date});
            setListAdapter(sa);
            getListView().setOnLongClickListener(new OnLongClickListener() {
                
                public boolean onLongClick(View v) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });
        }
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
            Log.i("",v.toString()+"----"+noticelist.get(position).get("title")+"++"+id);
            String url = "http://10.0.2.2:8080/om4/json/messageAction_getMessageInfo.do";
            
            NameValuePair nv = new BasicNameValuePair("mesddid",noticelist.get(position).get("taskid").toString());
            //NameValuePair nv2 = new BasicNameValuePair("",noticelist.get(position).get("").toString());
            List<NameValuePair> lists = new ArrayList<NameValuePair>();
            //lists.add(nv2);
            lists.add(nv);
            try {
                HttpEntity httpentity = new UrlEncodedFormEntity(lists,HTTP.UTF_8);
                HttpPost post = new HttpPost(url);
                post.setEntity(httpentity);
                Log.e("jessionid",noticelist.get(position).get("JSESSIONID").toString());
                //JSESSIONID用来传送sessionid用的这里我把sessionid放在了全局变量里面,在这儿取到sessionid传入后台验证
                post.setHeader("Cookie","JSESSIONID="+noticelist.get(position).get("JSESSIONID").toString());
                HttpClient httpclient =new DefaultHttpClient();
                HttpResponse response = httpclient.execute(post);
                
                if(response.getStatusLine().getStatusCode()==200){
                    Log.i("数据返回","请求成功");
                    
                    StringBuffer sb = new StringBuffer();
                    BufferedReader bufferedReader2 = new BufferedReader( 
                            new InputStreamReader(response.getEntity().getContent())); 
                    String str2 = ""; 
                    for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2 
                            .readLine()) { 
                        sb.append(s); 
                    } 
                    Log.e("数据",sb.toString());
                    String success = new JSONObject(sb.toString()).getString("success");
                    Log.e("success",success);
                    if(success=="true"){
                        Intent intent = new Intent();
                        JSONArray jsonarray = new JSONObject(sb.toString()).getJSONArray("data");
                        intent.putExtra("data",jsonarray.opt(0).toString());
                        intent.setClass(NoticeActivity.this,NoticeOrdinary.class);
                        startActivity(intent);
                    }
                }
                
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        
        private List<Map<String,Object>> getdata(){
            List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
            
            return lists;
        }
        
        
    
    }

    Listactivity用的xml文件代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <LinearLayout 
            android:layout_width="fill_parent"
               android:layout_height="fill_parent"
            android:orientation="horizontal"
            >
        <TextView 
            android:id="@+id/taskid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:visibility="gone"
            />
        <TextView 
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            />
        </LinearLayout>
        <LinearLayout 
            android:layout_width="fill_parent"
               android:layout_height="fill_parent"
            android:orientation="horizontal"
            >
        <TextView 
            android:id="@+id/status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:layout_marginRight="20dip"
            />
        <TextView 
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            />
         </LinearLayout>
    </LinearLayout>

    全局变量的实体对象

    package org.activity.entity;
    
    import java.util.List;
    import java.util.Map;
    
    import android.app.Application;
    
    public class ListMap extends Application{
        
        private String jessionid;
        public String getJessionid() {
            return jessionid;
        }
    
        public void setJessionid(String jessionid) {
            this.jessionid = jessionid;
        }
    
        private List<Map<String, Object>> lists;
    
        public List<Map<String, Object>> getLists() {
            return lists;
        }
    
        public void setLists(List<Map<String, Object>> lists) {
            this.lists = lists;
        }
    
    }

    全局变量需要在AndroidManifest.xml的配置

    <application
            android:name="org.activity.entity.ListMap"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >


     

  • 相关阅读:
    Qt 查询字符串数据
    #include <stdint.h>
    滤波器设计-巴特沃尔斯低通滤波设计 转
    小波学习之二(单层一维离散小波变换DWT的Mallat算法C++实现优化)--转载
    机器学习之Bagging与随机森林笔记
    机器学习之决策树笔记
    机器学习之softmax回归笔记
    机器学习之逻辑回归(Logistic)笔记
    机器学习之模型拟合效果的判断笔记
    机器学习最小二乘法笔记
  • 原文地址:https://www.cnblogs.com/b422/p/android_tab.html
Copyright © 2011-2022 走看看