• 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" >


     

  • 相关阅读:
    删除:恶意主页
    Winuser.h
    安天磁盘免疫工具研究的初步解答
    C#读写XML文件
    阻止系统关机
    在WebBrowser中屏蔽对话框
    如何用正确的方法写出高质量软件的75条体会
    怪事~
    GRUB4DOS中文自述文档;Grub4dos中文ReadMe
    开始菜单变成的经典样式,XPsuaa样式丢失
  • 原文地址:https://www.cnblogs.com/b422/p/android_tab.html
Copyright © 2020-2023  润新知