如何处理从单个形式的两个包中检索数据

时间:2015-02-20 04:08:31

标签: java android

大家好,如果你愿意帮助我的话。我一直坚持如何从另一个活动中检索捆绑数据。基本上我有两个活动,就是当我按下第一个活动上的按钮时,它将继续第二个活动,然后设置稍后在第一个活动上传递的字符串值。我做的是我使用bundle来放置字符串值。这里我有3种形式。我将bundle值从1st表单发送到second,并将bundle值从3rd返回到second。  我的问题是如何处理第二种形式的两个捆绑(活动)。

2 个答案:

答案 0 :(得分:0)

在按钮点击的第一个活动中,您可以执行以下操作:

Intent in = new Intent(activtiy1, secondact.class);
Bundle b = new Bundle();
b.putString("key", "string_to_pass");
.
.
.
in.putextras(b);
startActivity(in);

现在在第二项活动中,您必须获得捆绑项目:

Bundle b=getIntent().getExtras();
String str =  b.getString("key");

希望它有所帮助。

答案 1 :(得分:0)

 you have first create ui for first activity like this

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".FirstActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="77dp"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="110dp"
            android:layout_marginLeft="38dp"
            android:layout_toRightOf="@+id/button1"
            android:text="Button 2" />

    </RelativeLayout>



    ui of second activity like this 




    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="second actvity" />

    </LinearLayout>



activity first code where i set index with intent for both button click(you also put string double ling and message with intent)


package com.example.teststart;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity implements OnClickListener{
Button b1,b2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        b1=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id=v.getId();
        switch(id){
        case R.id.button1:
            Intent i1=new Intent(FirstActivity.this,secondactivity.class);
            i1.putExtra("one", 1);
            startActivity(i1);
        break;
        case R.id.button2:
            Intent i2=new Intent(FirstActivity.this,secondactivity.class);
            i2.putExtra("one", 2);
            startActivity(i2);
            break;
        }
    }


}




and second activity i get that integer value to identify which button is clicked and fire operation on that condition


package com.example.teststart;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class secondactivity extends Activity{
    TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactvity);
    tv=(TextView)findViewById(R.id.textView1);
    Bundle extras = getIntent().getExtras(); 
     int index = extras.getInt("one");
     if(index==1){
         tv.setText("nformation-A "+index);
     }else if(index==2){
         tv.setText("nformation-B "+index);
     }

}
}