2011-04-17 62 views
2

我是新来的android编程,我很确定这是我需要的。我有一个页面,加载开始时有一个微调,一些编辑文本框和两个按钮。一个按钮清除盒子并重置微调器。另一个按钮应该加载一个新的活动(我猜),我需要什么是在EditTexts和微调加载的。Android:将数据传递给新的活动

现在我已经把它烤面包,所以我可以测试一切都被正确抓住。这里是我的活动我现在有:

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class directoryApp extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.searchscreen); 

     //Declaring these so I can use them when the button is clicked. 
     final Spinner depts = (Spinner) findViewById(R.id.dept); 
     final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item); 
     deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     final EditText fname = (EditText) findViewById(R.id.fname); 
     final EditText lname = (EditText) findViewById(R.id.lname); 
     final EditText aim = (EditText) findViewById(R.id.aim); 
     final EditText phone = (EditText) findViewById(R.id.phone); 
     depts.setAdapter(deptsAdapter); 
     for(int i = 0 ; i < fillSpinner().length ; i++){ 
      deptsAdapter.add(fillSpinner()[i]); 
     } 


     //Search Button on click, makes a "toast" message with grabbed web string and whatever was entered into the fields. 
     findViewById(R.id.search).setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       //Here is where the search button does it's thing. 
       Toast.makeText(BNYDirectory.this, fname.getText() + " " + lname.getText() + " " + aim.getText() + " " + phone.getText() + " " + depts.getSelectedItem() + " " + deptIDs()[(int)depts.getSelectedItemId()], Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

我需要通过fname.getText(),lname.getText(),aim.getText(),phone.getText(),和deptIDs()(INT )depts.getSelectedItemId()](所有我作为字符串获取)。

去下一个页面(我将用这些来获得结果和显示)的最佳方式是什么?也可以点击搜索,我可以从网上获取结果并将其放入一个字符串数组中,并且只传递该数组,但我宁愿不这样做。

我可以使用的任何示例?

回答

7

您创建一个Intent,指定第二个活动的类名称。然后,您将额外的数据添加到意图中,并将每条数据与一个唯一字符串关联起来,作为查找数据的关键字。新活动可以使用相同的密钥来检索数据。有关示例代码,请参阅资源文档常用任务部分中的Opening a New Screen部分。

+2

谢谢,我不知道常见的任务部分! – 2011-04-17 17:03:18

相关问题