I am trying to write a code in Android to pass a value from a button click to another class. I am getting 0 value in the Log and i am not finding any technique to send the proper value.
Please go through my below code :
Java Code:
MainActivity.java
package com.example.mybuttonvalue;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity 
{
Activity activity;
Context cont;
public static int mysum;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btn = (Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            Intent intent = new Intent(MainActivity.this,MySum.class);
            intent.putExtra("key",5);
            startActivityForResult(intent,2); 
        }
    });
}
@Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2  
               if(requestCode==2)  
                     {  
                     }  
 } }
MySum.java
 package com.example.mybuttonvalue;
 import android.app.Activity;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.Intent;
 import android.view.View;
 import android.widget.Button;
 public class MySum extends Activity
 {
    Activity activity;
    Context context;
    int value = getIntent().getIntExtra("key",0); // 0 is default vlaue
    public MySum(Activity activity,Context cont)
    {
        this.activity = activity;
        this.context = cont;
    }
    public void check(final int x)
    {
        final Dialog dia = new Dialog(context);
        dia.requestWindowFeature(dia.getWindow().FEATURE_NO_TITLE); 
        dia.setContentView(R.layout.check);
        Button btn = (Button) dia.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {
                 int no = 12+x;
                 myvalue(no);
                 Intent intent= getIntent();  
                  intent.putExtra("result",x);  
                  setResult(2,intent);  
                 dia.dismiss();
            }
        });
        dia.show();
    }
    public String myvalue(int x)
    {
         return String.valueOf(x);
    }
}
Log Report
05-18 23:04:29.603: D/My Value :(10375): 0
This is the above code , how can i get 17 in the Log. I should get the result from MySum class to MainActivity class.
Please let me know , suggest me some good solution.
 
    