passing data between Activities

(To follow along with this, you should understand the basics of starting new activities: Link )

The easiest way to pass data from one activity to another is to create your own custom bundle and pass it to your new class.

First, create two new activities called Search and SearchResults (make sure you add the second one you create to the AndroidManifest.xml file!), and create xml layout files for each. Search's file should look like this:

<?xml version="1.0" encoding="utf-8"?>
< LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
        android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Name:"/>
    <EditText       
        android:id="@+id/edittext"       
android:layout_width="fill_parent"       
android:layout_height="wrap_content"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ID Number:"/>
    <EditText       
        android:id="@+id/edittext2"       
        android:layout_width="fill_parent"       
        android:layout_height="wrap_content"/>
    <Button  
        android:id="@+id/btnSearch" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Search" />
< /LinearLayout>

and SearchResult's should look like this:

<?xml version="1.0" encoding="utf-8"?>
< LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
        android:id="@+id/txtName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/txtState"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data"/>
< /LinearLayout>

Next, we'll override the OnCreate method of Search:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    Button search = (Button) findViewById(R.id.btnSearch);
    search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            
            Intent intent = new Intent(Search.this, SearchResults.class); 
            Bundle b = new Bundle();
            
            EditText txt1 = (EditText) findViewById(R.id.edittext);
            EditText txt2 = (EditText) findViewById(R.id.edittext2);
                       
            b.putString("name", txt1.getText().toString());
            b.putInt("state", Integer.parseInt(txt2.getText().toString())); 
              
            //Add the set of extended data to the intent and start it
            intent.putExtras(b);
            startActivity(intent); 
        }

    });
}

This is very similar to the previous example, except here we're creating our own bundle, adding some key/value pairs to it, and adding it to the intent.

Now, to retrieve the data, we just need to grab the Bundle that was passed to the new Activity and extract our values from it:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_results);

    Bundle b = getIntent().getExtras();
    int value = b.getInt("state", 0);
    String name = b.getString("name");
       
    TextView vw1 = (TextView) findViewById(R.id.txtName);
    TextView vw2 = (TextView) findViewById(R.id.txtState);
       
    vw1.setText("Name: " + name);
    vw2.setText("State: " + String.valueOf(value));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值