安卓activity之間值共用解決辦法,tabhost之間共用父類值

來源:互聯網
上載者:User

標籤:

1.tabhost父類值共用的解決辦法

dianzhanliebiao.java是傳值頁面,zhuyemian.java放的是tabhost,dianzhangaikuang.java是tabhost的具體子布局

 首先dianzhanliebiao.java傳值

   intent.putExtra("id", “1234”);
        intent.setClass(dianzhanliebiao.this, zhuyemian.class);
        dianzhanliebiao.this.startActivity(intent);


zhuyemian.java擷取從dianzhanliebiao.java傳來的值
        intent=getIntent();
        String id=intent.getStringExtra("id");

dianzhangaikuang.java擷取id的方法如下:

        Intent intent = getParent().getIntent();
        String id = intent.getStringExtra("id");

 

2.activity之間的傳值,個人感覺用SharedPreferences比較好,但是需要申明一個全域變數(當然也可以用於tabhost之間的值共用)

一般建一個常量類,我是Constant.java

  在裡面申明 public static SharedPreferences sharedata;

zhuyemian.java傳遞值

   Constant.sharedata =getSharedPreferences("rixin", 0);//第一個參數是儲存時的名稱,第二個參數則是檔案的開啟檔案
        Editor editor=Constant.sharedata.edit();
        editor.putString("id", “1234”);
        editor.commit(); //一點要提交,不然其他頁面不能擷取 

fadianliang.java擷取資料

   Constant.sharedata=getSharedPreferences("rixin", 0);
        String id=Constant.sharedata.getString("id",null);

 

3.字串轉整形

Integer.parseInt(String,10);//字串轉成10進位

Double.parseDouble(String);//字串轉換成double

String.valueOf(100);//整形裝換成String

 數群組轉換為List

  1. String[] arr = new String[] {"str1", "str2"};  
  2. List<String> list = Arrays.asList(arr);  

 List轉換為數組

  1. List<String> list = new ArrayList<String>();  
  2. list.add("str1");  
  3. list.add("str2");  
  4. int size = list.size();  
  5. String[] arr = (String[])list.toArray(new String[size]);//使用了第二種介面,傳回值和參數均為結果  

4.擷取每一個listview的item內容

//擷取每一個item的內容,我是自訂的listview,裡面有Textview控制項
tv_name=(TextView) view.findViewById(R.id.tv_name);
String dzmc=tv_name.getText().toString();

 

 

安卓activity之間值共用解決辦法,tabhost之間共用父類值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.