问题
I have developed one android application.
This is my first activity:
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);
}
});
This is my 2nd activity:
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));
Here i have to run the app means am getting $45.32 on first activity. I have to pass these $45.32 to next activity means am getting null.
How can i clear these error.please help me how can i pass the value from 1st activity to next activity.
回答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 );
回答2:
change getIntent().getExtras().getString(total)
To:
getIntent().getStringExtra("GrandTotal");
Or
String total = in.getStringExtra("GrandTotal");
grandtotal.setText("Welcome ," + total);
回答3:
Either store the value in shared preferences in one activity and get the value from same preference in another activity.
Storing in preferences
SharedPreferences preferences=getSharedPreferences(preferencename, MODE_PRIVATE);
Editor preferenceEditor=preferences.edit();
preferenceEditor.putString(key,value);
preferenceEditor.putInt(key,value);
preferenceEditor.commit();
Getting from preferences
preferences=getSharedPreferences(config_prop.getConfigValue("sharedprefs"), MODE_PRIVATE);
String str=preferences.getString(key, defaultValue);
Also you can pass the values using intents.
回答4:
Just change
grandtotal.setText("Welcome ,"+getIntent().getExtras().getString(total));
to
grandtotal.setText("Welcome ,"getIntent().getExtras().getString(GrandTotal));
in second activity.
And remove
String s= getIntent().getStringExtra(mGrandTotal);
It should be
i.putExtra("GrandTotal", mGrandTotal);
from first activity on button click event. Because mGrandTotal is local activity variable. So you don't need to get it from intent
来源:https://stackoverflow.com/questions/13987269/pass-the-value-from-activity-to-next-activity-in-android