【问题标题】:Value changing to 0 when starting activity开始活动时值更改为 0
【发布时间】:2015-03-05 13:44:43
【问题描述】:

我的应用程序允许用户生成卡路里值(主要活动 2)并将其传递给主要活动以便可以显示,当尝试添加多餐时,它不会添加先前的值并传递新值有意图并将其显示在主要活动中,为什么要这样做?任何帮助将不胜感激,解释更是如此。

主要活动

public class MainActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView calories = (TextView)findViewById(R.id.overall);
        String previouscalories = calories.getText().toString();
        int numold = Integer.parseInt(previouscalories);

        Intent intent = getIntent();
        String newcalories = "";
        if (intent.hasExtra("passedvalue"))
        {
            newcalories = intent.getExtras().getString("passedvalue");

        } else {
            newcalories = "0";  
        }

        int numnew = Integer.parseInt(newcalories);
        int overall = numnew + numold;
        calories.setText("Current Calories:"+ overall);

        Button add = (Button)findViewById(R.id.add_meal);
        Button reset = (Button)findViewById(R.id.reset);
        Button about = (Button)findViewById(R.id.about);

        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
                        startActivity(intent);
            }
        });


        about.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getApplicationContext(),about.class);
                startActivity(intent);
            }
        });
    }





    }

主要活动2

public class MainActivity2 extends ActionBarActivity {


    int sub_weight = 0;

    EditText weight;
    TextView calories;
    Button display, save;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        weight = (EditText)findViewById(R.id.edit_weight);
        calories = (TextView)findViewById(R.id.cal_total);
        display = (Button)findViewById(R.id.display);

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView calories = (TextView)findViewById(R.id.cal_total);
                String g = calories.getText().toString();
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.putExtra("passedvalue", g );
                startActivity(intent);
            }
        });

    }
    public void onRadioButtonClicked(View v){

        boolean checked = ((RadioButton) v).isChecked();

        switch (v.getId()) {
            case R.id.radiopork:
                if (checked)
                    sub_weight = sub_weight + 2;
                break;
            case R.id.radiochicken:
                if (checked)
                    sub_weight = sub_weight + 7;
                break;
            case R.id.radiobeef:
                if (checked)
                    sub_weight = sub_weight + 9;
                break;
            case R.id.radiosalmon:
                if (checked)
                    sub_weight = sub_weight + 13;
                break;
            case R.id.radiocod:
                if (checked)
                    sub_weight = sub_weight + 17;
                break;
            case R.id.radiocereal:
                if (checked)
                    sub_weight = sub_weight + 18;
                break;
            case R.id.radioporridge:
                if (checked)
                    sub_weight = sub_weight + 23;
                break;
            case R.id.radiotoast:
                if (checked)
                    sub_weight = sub_weight + 26;
                break;
            case R.id.radiocrisps:
                if (checked)
                    sub_weight = sub_weight + 29;
                break;
            case R.id.radionoodle:
                if (checked)
                    sub_weight = sub_weight + 33;
                break;


        }

        }
    public void display_calories(View v){

        String m = weight.getText().toString();
        int x =  Integer.parseInt(m);
        int y = x * sub_weight;
        calories.setText(y+"");
        sub_weight = 0;

    }


}

【问题讨论】:

    标签: android android-intent android-studio


    【解决方案1】:

    TextView calories = (TextView)findViewById(R.id.overall);

    每次创建 Activity 时它都不知道它之前的状态,除非你以这种方式编码,所以 calories 不会有任何值,除非你自己在 xml 中添加了一些东西作为默认值(这不会是用户之前输入的内容)。

    如果您想保存用户在创建/删除活动之间输入的内容,请将第二个活动的结果发送回第一个活动(请参阅here)或将值保存在sharedPreference(或数据库,但对于单个值来说,这可能是矫枉过正)。

    【讨论】:

      【解决方案2】:

      将之前的值保存在静态变量中,以便保持该值可重复使用。

      public class MainActivity extends ActionBarActivity {
      private static String previouscalories;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              TextView calories = (TextView)findViewById(R.id.overall);
              int numold = Integer.parseInt(previouscalories);
      
              Intent intent = getIntent();
              String newcalories = "";
              if (intent.hasExtra("passedvalue"))
              {
                  newcalories = intent.getExtras().getString("passedvalue");
      
              } else {
                  newcalories = "0";  
              }
      
              int numnew = Integer.parseInt(newcalories);
              int overall = numnew + numold;
              previouscalories = String.valueOf(overall);
              calories.setText("Current Calories:"+ overall);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-07
        • 2015-02-24
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多