2012-04-27 7 views
0

こんにちは私はhere.userクリックボタンを一度他のアクティビティを意味する私は1つのアクションを実行する必要がある、ボタンは、私は別のアクションを実行する必要があることを意味します。私はアクティビティ1で2つのアクティビティを取る1私は1つのボタンを持って、私はアクティビティ1ボタンで1つのカウント変数を使用してtreidをクリックしてカウント変数を増やし、putextraを使用してここでacticity2にそのカウント値を得る私は同じactivty1に戻ってくる場合、私はまた、カウントは '1'だけです、それはincresedされていませんどのようにその問題を解決するためにどのようなアイデアを私に示唆している。androidの別のクラスに1つのクラスのトランスペアレントな変数値を取得する方法

Activity1 .class: 
public class Activity1 extends Activity implements OnClickListener{ 
Button b1,b2; 
    int countk4=0; 

    SharedPreferences share; 
    String bol3; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.home1); 
    b1=(Button)findViewById(R.id.button1); 
    b1.setOnClickListener(this); 
    } 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if(v==b1) 
    { 
     countk4++; 
     share=getSharedPreferences("shared", MODE_WORLD_WRITEABLE); 
     SharedPreferences.Editor editor=share.edit(); 

     editor.putInt("roll", countk4); 

     editor.commit(); 

     show(); 
     Intent i=new Intent(Activity1 .this,Activity2.class); 
     i.putExtra("k", 1); 
     i.putExtra("k1", 13); 
     i.putExtra("ks", val1); 
     startActivity(i); 
    } 
} 
Integer val1,val2; 
private void show() { 
    // TODO Auto-generated method stub 
    share=getSharedPreferences("shared", MODE_WORLD_READABLE); 

    val1=share.getInt("roll",0); 

    } 
     } 

    Activity2 .class: 
    public class Activity2 extends Activity implements OnClickListener{ 
Button back; 
     int countkv; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.home2); 
    back=(Button)findViewById(R.id.button1); 
    back.setOnClickListener(this); 
      countkv = getIntent().getIntExtra("ks", 0); 

    if(countkv =1) 
    { 
    Toast.makeText(getApplicationContext(), "hai.......i am first", Toast.LENGTH_LONG).show(); 
    } 

if(countkv =2) 
{ 
    Toast.makeText(getApplicationContext(), "hai.......1 am second", Toast.LENGTH_LONG).show(); 
    } 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    if(v==back) 
    { 
     Intent i=new Intent(Activity2.this,Activity1.class); 


     startActivity(i); 
    } 


} 
} 
} 
but above every time first toast message displayed becz countkv is every time 1 only.. 

答えて

0

受信側では、Bundleというオブジェクトを作成します。

Bundle myBundle = new Bundle(); // Declare above 
    String temp; 

    myBundle = getIntent().getIntExtra(); 
    temp = myBundle.getString("ks"); // temp will have the val1. 

希望するとすぐに入手できます。

+0

あなたの応答のためのthaksここで私はativity1からactivity2へ値を得ていますprblmは、再びactivity2に戻りますactivity1に時間カウント変数refereshedので、カウント '0'の時間がそこにあります、 '1'もう一度ボタンをクリックしても、カウントは1に増えます.2は私のprblmです – user1089640

+0

@ user1089640:変数をPublic Static修飾子として宣言します。このリンクを試してください - > http://www.roseindia.net/java/beginners/howtoaccessstaticmethod.shtml – Bhavin

+0

私はpublic int countk4を使用しています。私はgetingしていません。しかし、今はpublic static int countk4を使用しています。結果 – user1089640

関連する問題