0

I am trying to create a simple grade tracker for android. The way I have it set up is that the user has a list of classes that are in their major. When they click on the title of the class, a new activity starts that has the name of the class, the catalog description, a space to place your grade, and a button to return to the list of classes.

What I would like to do is save the grade number that they input on the second page, and when the button is pressed, on the first page the grade is shown next to the course title.

I have edited to the code to reflect the comments given. This is the code that works!

CoreClasses

TextView eng101, eng101scr;

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

    eng101 = (TextView)findViewById(R.id.eng101);       
    eng101scr = (TextView)findViewById(R.id.eng101CoreScr);

    eng101.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(ClassCore.this, Eng101.class);
            i.putExtra("grades", "eng101" );
            startActivityForResult(i, 1);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(data.getExtras().containsKey("e101FinalScore")){
        eng101scr.setText(data.getStringExtra("e101FinalScore"));

    }

Eng101

public class Eng101 extends Activity {

Button btnSubmit;
EditText userGrade;
String strGrade;

OutsideVariables outside = new OutsideVariables();  
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eng101);

    btnSubmit = (Button)findViewById(R.id.btnE101);
    userGrade = (EditText)findViewById(R.id.eng101Scr);

    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            strGrade = userGrade.getText().toString();
            Intent i = getIntent();
            String msg = i.getStringExtra("grades");
            if(msg.contentEquals("eng101")){
                i.putExtra("e101FinalScore", strGrade);
                setResult(RESULT_OK, i);
                finish();
            }

        }
    });     
    }   
}
David Moran
  • 177
  • 2
  • 12
  • 1
    You should be more specific about errors. That said, I'm guessing this line: `eng101scr.setText(ss);` is throwing a `ResourceNotFoundException`. Change the parameter to a String with `String.valueOf()` or `"" +`. Also, there's no need to start the List Activity again. Instead, start the second Activity from the first with `startActivityForResult()`. – Mike M. May 05 '14 at 05:38
  • I implemented the code you suggested, however I am unable to return to the first activity. I posted the edited code above – David Moran May 07 '14 at 00:03
  • 1
    You would put that in the `onClick()` method for the Button you want to finish the second Activity. You would then override the `onActivityResult()` method in the first Activity to retrieve the result. [This link](http://developer.android.com/training/basics/intents/result.html) might help. – Mike M. May 07 '14 at 00:26
  • 1
    Got it! I posted the working code above. THanks for the help – David Moran May 07 '14 at 00:39

2 Answers2

1

You can pass the values to the previous activity by,

 Intent intent = new Intent(Eng101.this, ClassCore.class);
 intent.putExtra("grade",grade);
 startActivity(new Intent(Eng101.this, ClassCore.class));

and in ClassCore you can get the value grade by,

Bundle extras = getIntent().getExtras();

// get data via the key
if(extras!=null){
  int value1 = intent.getIntExtra("grade", 0);
  if (value1 != null) {
    eng101scr.setText(value1);
  }
}
Sripathi
  • 1,760
  • 15
  • 20
0

Because you're using an instance, each new instance will have it's own storage and values. What you're looking to do is actually much simpler than this, and it's to do everything using static access.

However, the better way to do this would be to create some sort of class like your OutsideVariables and pass an instance into your new activity, that way you're able to access them in both. Take a look at this other post for some more information. How to pass the values from one activity to previous activity

Community
  • 1
  • 1
Christopher Wirt
  • 1,108
  • 1
  • 10
  • 21