-1

I'm a beginner of Java. Since I wanted to retrieve back the EditText value from Expenses class, I used the Intent Class to store the data and then retrieve it in the Result class. But when I run the Eclipse program, it stops.

Expenses class

builder.setPositiveButton("Add",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog,
    int which) {
        // TODO Auto-generated method stub
        if(s.equals("Food")){
            value = et1.getText().toString();
            food = Integer.parseInt(value);
            description = et2.getText().toString();
            categorySelected = true;
            i.putExtra("eFood",food);
            et1.getText().clear();
            et2.getText().clear();
        }
}

Result class

TextView tv;
int eFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Intent i = getIntent();
    tv = (TextView)findViewById(R.id.textView2);
    eFood = i.getIntExtra("eFood",0);
    tv.setText(eFood);
}
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
Jian Wai
  • 11
  • 4

2 Answers2

0

Try implementing it this way:-

String hiString = "hi";
Intent i = new Intent(this, ActivityTwo.class);
//Create the bundle
Bundle b = new Bundle();
//Add your data to bundle
b.putString(“stuff”, hiString);
i.putExtras(b);
startActivity(i);

And in the resulting activity:

Bundle bundle = getIntent().getExtras();
String text= bundle.getString("stuff");

Hope this helps.

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42
karan sandhu
  • 171
  • 6
0

Don't assign an integer to setText. Convert it to a String before assigning.

String efoodStr = "" + eFood;
tv.setText(efoodStr);
TextView tv;
int eFood;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Intent i = getIntent();
    tv = (TextView)findViewById(R.id.textView2);
    eFood = i.getIntExtra("eFood",0);
    String efoodStr = "" + eFood;
    tv.setText(efoodStr);
}