2014-07-26 18 views
0

我已經在活動A中填充了一個Long數組,並通過putExtra()方法將其傳遞給活動B.通過獲取額外的數據來獲取數據,但只有第一個元素可以在第二個Activity中設置爲TextView活動

public void onClick(View v) { 

     Intent intent1=new Intent(MainActivity.this, DetailsAcitivity.class); 
     intent1.putExtra("bonds", bounds); 
     startActivity(intent1); 
     finish(); 
    } 

而在活動B我已經找回它們:

Bundle extras = getIntent().getExtras(); 
bounds2 = extras.getLongArray("bonds"); 

然後我希望把每個元素在不同的TextView陣列,所以我這樣做:

TextView tvb1; 
TextView tvb2; 
long[] bounds2=new long[10]; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_details); 


    Bundle extras = getIntent().getExtras(); 
    bounds2 = extras.getLongArray("bonds"); 

    tvb1=(TextView) findViewById(R.id.tvWageDetailVal01); 
    tvb1.setTypeface(droidnaskh_regular); 
    String str=String.format("%,d",bounds2[0]); 
    tvb1.setText(str); 

    tvb2=(TextView) findViewById(R.id.tvWageDetailVal02); 
    tvb2.setTypeface(droidnaskh_regular); 
    str=String.format("%,d",bounds2[1]); 
    tvb2.setText(str); 

    Toast toast=Toast.makeText(this, String.valueOf(bounds2[1]), Toast.LENGTH_SHORT); 
    toast.show(); 

現在TextView tvb1的文本設置爲bounds2 [0]值,但tvb2不會!即使Toast消息顯示了bounds2 [1]的正確值,但TextView的文本不會被設置!

回答

0

這不是真正的傳遞價值的方法。使用下面的代碼:

A類:

Intent i = new Intent(this, classa.class); 
Bundle bundle = new Bundle(); 
bundle.putString("value 1", "String value 1"); 
bundle.putString("value 2","String value 2"); 
i.putExtras(bundle); 
startActivity(i); 

類B:

Bundle getData = getIntent().getExtras(); 
String value1= getData .getString("String value 1"); 
String value2= getData .getString("String value 2"); 
textview.setText(value1); 
相關問題