2

私は1つの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);

        }
    });

これは私の2番目のアクティビティです。

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を次のアクティビティに渡す必要があります。つまり、nullになります。

これらのエラーをクリアするにはどうすればよいですか。最初のアクティビティから次のアクティビティに値を渡す方法を教えてください。

4

4 に答える 4

2

変更getIntent().getExtras().getString(total) 先:

getIntent().getStringExtra("GrandTotal"); 

または

String total = in.getStringExtra("GrandTotal");
grandtotal.setText("Welcome ," + total);
于 2012-12-21T09:18:46.110 に答える
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 );
于 2012-12-21T10:07:50.957 に答える
0

変更するだけ

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

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

2番目のアクティビティで。

そして削除します

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

そのはず

i.putExtra("GrandTotal", mGrandTotal);

ボタンクリックイベントの最初のアクティビティから。mGrandTotalはローカルアクティビティ変数であるためです。だからあなたはそれを意図から得る必要はありません

于 2012-12-21T09:18:24.703 に答える
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);

また、インテントを使用して値を渡すこともできます。

于 2012-12-21T09:19:57.803 に答える