0

I am a beginner of Android. I want to pass the result from FirstActivity to the SecondActivity as below. How to remove results in intent extra? Or any way to pass the result to SecondActivity and show on the TextView? (I have make a mistake and replace, my main question is how to delete the result, because i want to set another new result in it.)

FirstActivity.java

    public class FirstActivity extends Activity {   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //code...
        try {     
            myDbHelper.createDatabase();
        } 
        catch (IOException ioe) {
        Log.d("Error","Error while createing Database");
        ioe.printStackTrace();
        throw new Error("Unable to create database");
        }   
        try {     
            myDbHelper.openDataBase();     
        }
        catch(SQLException sqle){
            Log.d("Error","Error while Opening Database");
            sqle.printStackTrace();
            throw sqle;     
        }
        send.setOnClickListener( new View.OnClickListener() {  
        public void onClick(View v) {  
        showResult();  
    }  
    });             
    }
    private void showResult() {  
        //...code       
        //checking for slection 
        results = queryData(table, type);               
        Intent intent = new Intent(this,SecondActivity.class);      
        intent.putExtra("results", results);  
        startActivity(intent);
    }
    public String queryData(String table, String type){
        //...
        //do somthing to get result
        return result;
    }
    }  

SecondActivity.java

    public class SecondActivity extends Activity {      
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);                    
        setContentView(R.layout.result_item);        
        TextView tv;        
        tv = (TextView)findViewById(R.id.tv);       
        Bundle extras=getIntent().getExtras();
        String value1=extras.getString("results");      
        tv.setText("Result\n" + value1);
    }
    }
Jemshit
  • 9,501
  • 5
  • 69
  • 106
william lau
  • 39
  • 4
  • 7
  • 2
    Actually your way is right. Try to start your activity with `startActivity(intent);` instead of `startActivityForResult(intent, 0);` – Praveenkumar Oct 10 '12 at 04:32

4 Answers4

0

change startActivityForResult(intent, 0) to startActivity(intent);

like this

private void showResult() {  
    results = queryData(table, type);               
    Intent intent = new Intent(firstActivity.this,SecondActivity.class);      
    intent.putExtra("results", results);  
    startActivity(intent);
}
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
0

Make new intent and pass

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);      
intent.putExtra("results", results);  
startActivity(intent);
Last Warrior
  • 1,307
  • 1
  • 11
  • 20
0

You were done your way of method that pass the data to next Activity is right. But, you made a mistake with starting the activity. Starting an Activity in your code should be like below -

private void showResult() {  
    //...code       
    //checking for slection 
    results = queryData(table, type);               
    Intent intent = new Intent(this,SecondActivity.class);      
    intent.putExtra("results", results);  
    startActivity(intent);  // This is the way to start a new Activity which is in seperate class
}

And, just have a look at below -

  1. How do I pass data between Activities in Android application?

  2. Passing data between activities in Android

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
0

You can do in 2 ways:

1. You can use startActivityForResult(), onActivityResult() from the Activity A, to pass the data on to Activity B, then after some computation, passing the result back to Activity A.

2. Now if you want to just send some data from Activity A to Activity B, and then display it in TextView, then use putExtra() and getExtras()...

Sending from Activity A to B:

Intent i = new Intent(Activity_A.this, Activity_B.class);
i.putExtra("name",Name);
startActivity(i);

Receiving the value on Activity B:

Intent i = getIntent();
String n = i.getExtras().getString("name");
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • This may cause a problem if your putExtra be null, in order to handle that situation you have to check your if(!getExtra().getString.equal(null)),Then do any thing that you want – Ehsan Jul 28 '17 at 07:09