如何发送Intent Extra?

时间:2012-10-10 04:28:50

标签: android

我是Android的初学者。我想将结果从FirstActivity传递给SecondActivity,如下所示。如何删除意图的结果?或者将结果传递给SecondActivity并在TextView上显示的任何方法? (我犯了一个错误并替换,我的主要问题是如何删除结果,因为我想在其中设置另一个新结果。)

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);
    }
    }

4 个答案:

答案 0 :(得分:0)

startActivityForResult(intent, 0)更改为startActivity(intent);

像这样

private void showResult() {  
    results = queryData(table, type);               
    Intent intent = new Intent(firstActivity.this,SecondActivity.class);      
    intent.putExtra("results", results);  
    startActivity(intent);
}

答案 1 :(得分:0)

制作新意图并传递

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);      
intent.putExtra("results", results);  
startActivity(intent);

答案 2 :(得分:0)

您已经完成了将数据传递到下一个Activity的方法。但是,你在启动活动时犯了一个错误。在代码中启动Activity应如下所示 -

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
}

而且,请看下面的内容 -

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

  2. Passing data between activities in Android

答案 3 :(得分:0)

您可以通过以下两种方式完成:

1。您可以使用活动A中的startActivityForResult()onActivityResult()将数据传递给活动B,然后经过一些计算,将结果传回活动A.

2. 现在,如果您只想将一些数据从活动A发送到活动B,然后在TextView中显示,那么请使用putExtra()getExtras() ..

从活动A发送到B:

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

接收活动B上的值

Intent i = getIntent();
String n = i.getExtras().getString("name");