2012-12-21 35 views
2

我開發了一個android應用程序。將活動中的值傳遞給android中的下一個活動

這是我的第一個活動:

public class ViewCartActivity extends Activity { 

String mGrandTotal; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewcartactivity); 

    ListView mLstView1 = (ListView) findViewById(R.id.listView1); 
    TextView mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue); 
    Button mBtnSubmit = (Button) findViewById(R.id.mBtnSubmit); 

    ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(
      ViewCartActivity.this); 

    mLstView1.setAdapter(mViewCartAdpt); 

    if (Constants.mItem_Detail.size() > 0) { 
     Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0) 
       .get(SingleMenuItem.KEY_TOTAL)); 
     for (int i = 1; i < Constants.mItem_Detail.size(); i++) { 
      mGTotal = mGTotal 
        + Double.parseDouble(Constants.mItem_Detail.get(i).get(
          SingleMenuItem.KEY_TOTAL)); 
     } 

     mGrandTotal = String.valueOf(mGTotal); 
     mTxtViewGrandTotal.setText("$" + mGrandTotal); 
    } 

    mBtnSubmit.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      String s= getIntent().getStringExtra(mGrandTotal); 
      Intent i = new Intent(getApplicationContext(), CustomerLogin.class); 
      i.putExtra("GrandTotal", s); 
       startActivity(i); 

     } 
    }); 

這是我的第二個活動:

setContentView(R.layout.customer_login); 

    Intent in = getIntent(); 
     String total = in.getStringExtra(mGrandTotal); 
    TextView grandtotal = (TextView) findViewById(R.id.grand_total); 
    grandtotal.setText("Welcome ,"+getIntent().getExtras().getString(total)); 

這裏,我已經運行應用程序的手段對我第一次活動得到$ 45.32。 我必須通過這些$ 45.32到下一個活動意味着變空。

我該如何清除這些錯誤。請幫助我如何將第一個活動的值傳遞給下一個活動。

+0

第2次活動中「mGrandTotal」的值是多少? –

+0

爲什麼你把這一行「String s = getIntent()。getStringExtra(mGrandTotal);」在開始意圖之前的第一個活動?去掉它。 –

+0

檢查編輯的答案 –

回答

1
FirstActivity 

public class ViewCartActivity extends Activity { 

String mGrandTotal; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewcartactivity); 

    ListView mLstView1 = (ListView) findViewById(R.id.listView1); 
    TextView mTxtViewGrandTotal = (TextView) findViewById(R.id.mTxtViewGrandTotalValue); 
    Button mBtnSubmit = (Button) findViewById(R.id.mBtnSubmit); 

    ViewCartAdapter mViewCartAdpt = new ViewCartAdapter(
      ViewCartActivity.this); 

    mLstView1.setAdapter(mViewCartAdpt); 

    if (Constants.mItem_Detail.size() > 0) { 
     Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0) 
       .get(SingleMenuItem.KEY_TOTAL)); 
     for (int i = 1; i < Constants.mItem_Detail.size(); i++) { 
      mGTotal = mGTotal 
        + Double.parseDouble(Constants.mItem_Detail.get(i).get(
          SingleMenuItem.KEY_TOTAL)); 
     } 

     mGrandTotal = String.valueOf(mGTotal); 
     mTxtViewGrandTotal.setText("$" + mGrandTotal); 
    } 

    mBtnSubmit.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


      Intent i = new Intent(getApplicationContext(), CustomerLogin.class); 
      i.putExtra("GrandTotal", mGrandTotal); 
       startActivity(i); 

     } 
    }); 


SecondActivity 

setContentView(R.layout.customer_login); 

    Bundle b = getIntent().getExtras(); 
     String total = b.getString("GrandTotal"); 
    TextView grandtotal = (TextView) findViewById(R.id.grand_total); 
    grandtotal.setText("Welcome ," + total); 
+0

我已經改變了我的代碼。但是隻能獲得空值yar – user1897014

+0

檢查此行String total = b.getString(「GrandTotal」); – User

+0

感謝god.it工作成功。請更新答案。 – user1897014

0

只要改變

grandtotal.setText("Welcome ,"+getIntent().getExtras().getString(total)); 

grandtotal.setText("Welcome ,"getIntent().getExtras().getString(GrandTotal)); 

在第二個活動。

,並刪除

String s= getIntent().getStringExtra(mGrandTotal); 

應該

i.putExtra("GrandTotal", mGrandTotal); 

從按鈕的Click事件的第一項活動。 因爲mGrandTotal是本地活動變量。所以,你不需要來自意圖得到它

+0

我已經改變了我的code.but現在也獲得空值只yar – user1897014

+0

@ user1897014,請發佈logcat輸出 –

+1

我chnaged第二activity.String總= b.getString(「GrandTotal」 );現在我得到了答案。謝謝你留在我身邊 – user1897014

2

變化getIntent().getExtras().getString(total) 要:

getIntent().getStringExtra("GrandTotal"); 

或者

String total = in.getStringExtra("GrandTotal"); 
grandtotal.setText("Welcome ," + total); 
+0

我已經改變了code.but現在alos只有相同的錯誤 – user1897014

+0

你可以發佈logcat,並突出顯示哪一行給出了錯誤? – Nermeen

+0

這是我的第一項活動:http://pastie.org/5560655這是我的第二項活動:http:// pastie。org/5560390這是我的第三個活動:http://pastie.org/5560392在這裏我必須通過mGrandTotal值是從第二個活動傳遞到第三個activity.how我可以do.am獲得空值 – user1897014

0

無論是在共享偏好值存儲在一個活動,並從價值另一項活動中的同樣偏好。

偏好

  SharedPreferences preferences=getSharedPreferences(preferencename, MODE_PRIVATE); 
      Editor preferenceEditor=preferences.edit(); 

      preferenceEditor.putString(key,value); 
      preferenceEditor.putInt(key,value); 
      preferenceEditor.commit(); 

從喜好

  preferences=getSharedPreferences(config_prop.getConfigValue("sharedprefs"), MODE_PRIVATE); 
     String str=preferences.getString(key, defaultValue); 

獲取存儲您也可以使用意圖傳遞的值。

相關問題