这里是我的空间,是我用来记录点滴的沃土...

Android:在不同的Activity之间进行数据的共享

  最近一直做一个项目,开始的时候需求中没有涉及到主子表之间的关系,因为自己也是初学Android太多太多的无奈。需求基本上做完了,这下经理又开始让我来完善自己项目中的不足,项目需求虽然没有涉及到,但是说这只是这次需求没有涉及到,这种业务指定会有的,无奈之下,开始完善需求。

  完善的时候遇到了一个这样的问题,就是主Activity中有些值需要传递到别的Activity在子Activity中进行一些赋值操作之后然后再主表中进行一提交。(因为公司在做手机系统的时候考虑到手机系统要与平台进行融合,这样传输的数据格式必须符合一定的格式。传输的时候是以XML个进行传输的,贴上一个格式代码:

View Code
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <NewDataSet>
        <UDOC_OTHERCOMPANY1 diffgr:id="UDOC_OTHERCOMPANY11" msdata:rowOrder="0" A="1" ID="6500000036500387158" C="111111" N="11111111" USRCODE="11111111" SHORTNAME="" CLSTATE="-1" EDUCATION="-1" EDUCATION_SYSCODE="" EDUCATION_USRNAME="" GENDER="0" GENDER_ENUM="男" IDNUMBER="" MARITALSTATUS="-1" RESIDENCEADDRESS="" AREAID="-1" AREAID_SYSCODE="" AREAID_USRNAME="" COMPANY="1000000000000000001" COMPANY_SYSCODE="C001" COMPANY_USRNAME="合肥中建集团" STATE="1" STATE_ENUM="有效客户" INVALIDREASON="-1" INVALIDREASON_C="" INVALIDREASON_N="" EID="1000000000000000000" EID_SYSCODE="S001" EID_USRNAME="管理员" EDT="2012-05-22" LASTMODIFY="-1" LASTMODIFY_SYSCODE="" LASTMODIFY_USRNAME="" WAY="-1" WAY_C="" WAY_N="" WAYSUB="-1" WAYSUB_C="" WAYSUB_N="" STARTEDYEAR="-1" CLASSPID="-1" COMPLEVEL="-1" COMPLEVEL_C="" COMPLEVEL_N="" EMPID="-1" EMPID_SYSCODE="" EMPID_USRNAME="" VICESALEDIRECTOR="-1" VICESALEDIRECTOR_SYSCODE="" VICESALEDIRECTOR_USRNAME="" ORDERPOSSIBILITY="-1" ORDERPOSSIBILITY_C="" ORDERPOSSIBILITY_N="" BUYPOSSIBILITY="-1" BUYPOSSIBILITY_C="" BUYPOSSIBILITY_N="" IMPORTANCE="-1" IMPORTANCE_C="" IMPORTANCE_N="" HEAT="-1" INFOSIMPOSIBLE="-1" ORIG="-1" ORIG_SYSCODE="" ORIG_USRNAME="" MARK="-1" MARK_N="" MARK_C="" MOBILE="15965874562" PHONE="" FAX="" EMAIL="" QQ="" MOBILE2="" PHONE2="" FAX2="" MSN="" PROV="-1" PROV_C="" PROV_N="" CITYT="-1" CITYT_C="" CITYT_N="" TOWN="-1" TOWN_C="" TOWN_N="" COMPTYPE="" TYPE="11" TYPE_ENUM="普通客户" DELIVERMODE="-1" DELIVERMODE_C="" DELIVERMODE_N="" SHORTCODE="" DEPTID="1000000000000000001" DEPTID_SYSCODE="C001" DEPTID_USRNAME="合肥中建集团" COMPCODE="C001/" SHIPLEADTIME="0" INVOICETYPE="0" INVOICETYPE_ENUM="增值税专用发票" CTYPE="0" CTYPE_ENUM="个人" SHIPID="-1" SHIPID_C="" SHIPID_N="" MAPCOMP="-1" MAPCOMP_C="" MAPCOMP_N="" PARENTID="-1" PARENTID_C="" PARENTID_N="" HEATEXPLAIN="" SCOPE="-1" SCOPE_USRNAME="" SCOPE_SYSCODE="" STATUS="-1" MERIT="-1" CONLEVEL="-1" SELLLIST="-1" SELLLIST_C="" SELLLIST_N="" FRCLSTATE="-1" FRCLSTATE_C="" FRCLSTATE_N="" LACREATEDATE="2012-05-22" EMPCOUNT="0" YTURNOVER="0" REGADDRESS="" RECADDR="" CORPORATION="" CORPPHONE="" TRADETYPE="0" TRADETYPE_ENUM="内销" DEALRANGE="" LICENSENUM="" TAXREGNUM="" TAXR="17" NATION="-1" NATION_C="" NATION_N="" POSTCODE="" LINKMANID="-1" LINKMANID_C="" LINKMANID_N="" CORPADDRESS="" LINKPOSTCODE="" WEBSITE="" IS_COMMAN_TAXPAYER="false" MM="" PRICECLASS="-1" PRICECLASS_C="" PRICECLASS_N="" ISCREDIT="false" CREDITAMT="0" CREDITLEVEL="" ISMONTHCLOSE="false" ENTRUSTSTRHID="-1" ENTRUSTSTRHID_C="" ENTRUSTSTRHID_N="" BUSINESSTYPE="0" BUSINESSTYPE_ENUM="普通销售" PAYTERMID="" ISPLANT="false" PAYMODE="-1" PAYMODE_STLTYPECODE="" PAYMODE_STLTYPENAME="" CURR="0" CURR_SYSCODE="" CURR_USRNAME="" PAYUPDATE="0" SENDADDR="" LASTPURAMT="0" LASTDEALAMT="0" MAXPURAMT="0" TOTALNETAMT="0" BALANCE="0" BOUNCINGCOUNT="0" RID="1000000000000000000" RID_SYSCODE="S001" RID_USRNAME="管理员" RDT="2012-05-22" CONTACT="-1" BOOLEMAIL="-1" BOOLNOTE="-1" SHARELIST="-1" CLASSPID_C="" CLASSPID_N="" SERVICEDIRECTOR="-1" SERVICEDIRECTOR_SYSCODE="" SERVICEDIRECTOR_USRNAME="" SERVICEVICEDIRECTOR="-1" SERVICEVICEDIRECTOR_SYSCODE="" SERVICEVICEDIRECTOR_USRNAME="" PROVINCE1="-1" PROVINCE1_C="" PROVINCE1_N="" CITY1="-1" CITY1_C="" CITY1_N="" COUNTY1="-1" COUNTY1_C="" COUNTY1_N="" TOWN1="" ADDRESS1="" ZIPCODE1="" PROVINCE2="-1" PROVINCE2_C="" PROVINCE2_N="" CITY2="-1" CITY2_C="" CITY2_N="" COUNTY2="-1" COUNTY2_C="" COUNTY2_N="" TOWN2="" ADDRESS2="" ZIPCODE2="" PROVINCE3="-1" PROVINCE3_C="" PROVINCE3_N="" CITY3="-1" CITY3_C="" CITY3_N="" COUNTY3="-1" COUNTY3_C="" COUNTY3_N="" TOWN3="" ADDRESS3="" ZIPCODE3="" PROVINCE4="-1" PROVINCE4_C="" PROVINCE4_N="" CITY4="-1" CITY4_C="" CITY4_N="" COUNTY4="-1" COUNTY4_C="" COUNTY4_N="" TOWN4="" ADDRESS4="" ZIPCODE4="" DISTRIBUTORALIAS="" DISTRIBUTOR="" HISID="" COMPCLASSID="-1" COMPCLASSID_C="" COMPCLASSID_N="" INFOPERSON="-1" INFOPERSON_C="" INFOPERSON_N="" INFOPERSONMOBILE="" BRANCH="-1" BRANCH_SYSCODE="" BRANCH_USRNAME="" SALEDEPT="-1" SALEDEPT_SYSCODE="" SALEDEPT_USRNAME="" MARKETINGCENTER="-1" MARKETINGCENTER_SYSCODE="" MARKETINGCENTER_USRNAME=""/>
        <UDOC_OTHERCOMPANYLINK diffgr:id="UDOC_OTHERCOMPANYLINK1" msdata:rowOrder="0" A="1" ID="6500000036500387159" ISLINK="0" LINKMAN="" C="111111" N="111111" SEX="1" SEX_ENUM="男" DEPT="" FAX="" EMAIL="" POSTCODE="" CORPADDRESS="" ADDRESS="" INFO="" ROLE="" PHONE="" MOBILE="" MM="" MID="6500000036500387158" TITLE="" MARRSTATE="-1" CLIENT="6500000036500387158" CLIENT_C="111111" CLIENT_N="11111111" PRIN="-1" PRIN_SYSCODE="" PRIN_USRNAME="" PRDEPT="-1" PRDEPT_SYSCODE="" PRDEPT_USRNAME="" IMPORTANCE="-1" CONTACTNOTE="" NEXUSLEVEL="-1" SUPERIOR="0" SUPERIOR_C="" SUPERIOR_N="" PURROLE="-1" HOMEPHONE="" MSN="" QQ="" BOOLEMAIL="1" BOOLEMAIL_ENUM="否" BOOLNOTE="1" BOOLNOTE_ENUM="否" EID="1000000000000000000" EID_SYSCODE="S001" EID_USRNAME="管理员" CREATEDATE="2012-05-22" CUSRELATION="-1" CUSRELATION_C="" CUSRELATION_N="" LINKMANID_N="1111111"/>
    </NewDataSet>
</diffgr:diffgram>

 

必须生成这种格式的数据平台才能保存成功,这样在界面生成之后进行了数据录入了之后,就得生成这种格式的xml,解释下XML  “UDOC_OTHERCOMPANY1”是主表的数据(这个数据需要在MainActivity中进行生成)“UDOC_OTHERCOMPANYLINK ”这个则是子表的值,可能是多个这样的节点,也就是子表可能是多条数据(这个数据需要在子表的Activity中进行赋值生成),考虑到这我想到的是程序中需要有个一个变量在这个两个或是多个Activity中进行共享,行到这里我想到了两个解决方法:

一,在MainActivity中声明一个公共的static变量,在子Activity中可以访问MainActivity该变量(共享的这个变量是一个DOM对象)。

代码:

public class TwoActivityShareDataActivity extends Activity {
    /** Called when the activity is first created. */
        public static TwoActivityShareDataActivity instance = null;//声明的变量


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        instance = this;//变量赋值
    }

}
//第二个Activity中进行访问
public
class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TwoActivityShareDataActivity.instance.document= "Do Something"; finish(); } }

方法2:通过Application Context进行变量共享

首先,建立一个继承自Application的类,将要共享的变量在其中声明:

代码:

import android.app.Application;

public class ApplicationTrans extends Application {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

注意:需要注意的是,写完这个类之后需要在AndroidManifest.xml中在Application节点中进行设置

<application
        android:name=".ApplicationTrans"//必须设置
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TwoActivityShareDataActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" >
        </activity>
    </application>

 

 

 

 

接下来在MainActivity中进行操作:

((ApplicationTrans) getApplication()).setValue("我是通过Application传递的值");//赋值操作

在第二个Activity中获得该值:就行用这个值进行操作了

((ApplicationTrans) getApplication()).getValue();
posted @ 2012-05-23 14:41  wsx2miao  Views(8243)  Comments(2Edit  收藏  举报
这里是我的空间,是我用来记录点滴的沃土...