android 活动传递数据,在活动之间传递数据 - Android SDK

我已经阅读了以前发布的问题和答案2天,并且我尝试了所有建议的变体以及在清单中将我的launchMode属性设置为“标准”。在活动之间传递数据 - Android SDK

我试图在按下按钮后将数据从我的第二个活动传回我的第一个活动。按下按钮后,第一个活动启动,但它不回到我的onActivityResult()方法。我无法弄清楚为什么会发生这种情况。

下面是从活动二我的代码:

Button btnAdd = (Button) findViewById(R.id.btnAdd);

btnAdd.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//Check that message is printed out to LogCat

System.out.println("hello test1 Activity2");

EditText band = (EditText) findViewById(R.id.txtBand);

band.setFilters(new InputFilter[] {

new InputFilter.LengthFilter(9)

});

EditText album = (EditText) findViewById(R.id.txtAlbum);

album.setFilters(new InputFilter[] {

new InputFilter.LengthFilter(9)

});

final Spinner genre = (Spinner) findViewById(R.id.spin_genre);

TextView selection = (TextView)genre.getSelectedView();

CharSequence strBand = band.getText();

CharSequence strAlbum = album.getText();

CharSequence strGenre = selection.getText();

//Check that we got input values

System.out.println("hello test Activity2- " +

strBand + " - " + strAlbum + " - " + strGenre);

//**********Intent Declaration************

Intent i = new Intent(getApplicationContext(), Activity1.class);

i.putExtra("strBand", strBand);

i.putExtra("strAlbum", strAlbum);

i.putExtra("strGenre", strGenre);

startActivityForResult(i, 0);

setResult(RESULT_OK, i);

finish();

}

});

这里的活动1:

public class Activity1 extends Activity {

/** Called when the activity is first created. */

@SuppressWarnings("deprecation")

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button addAlbum = (Button) findViewById(R.id.btnMain);

addAlbum.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent i = new Intent();

i.setClassName("jorge.jorge.jorge",

"jorge.jorge.jorge.Activity2");

startActivity(i);

}

});

}// end of onCreate()

//******************Callback Method****************************

protected void onActivityResult(int requestCode, int resultCode, Intent data)

{

super.onActivityResult(requestCode, resultCode, data);

//Checks if we got to ActivityResult

System.out.println("hello test 2: We got to Activity1");

if (resultCode == RESULT_OK)

{

Bundle returndata = data.getExtras();

String strAlbum = returndata.getString("strAlbum");

String strBand = returndata.getString("strBand");

String strGenre = returndata.getString("strGenre");

// check to see if we got the variable values from activity2

System.out.println("hello test 2: We got to Activity1 with variables - "

+ strBand + " - " + strAlbum + " - " + strGenre);

//Create table layout to contains views with variable values

TableLayout table = new TableLayout(this);

table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,

LayoutParams.WRAP_CONTENT));

//creates row with parameters

TableRow row = new TableRow(this);

row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT));

//text views to contain variable values

TextView tv1 = new TextView(this);

tv1.setText(strBand);

row.addView(tv1);

TextView tv2 = new TextView(this);

tv2.setText(strAlbum);

row.addView(tv2);

TextView tv3 = new TextView(this);

tv3.setText(strGenre);

row.addView(tv3);

//adds the table row to the table layout

table.addView(row);

}

}// end onActivityResult()

}

我不知道如果我的活动回调不会在代码妥善安置或者如果我没有正确解雇这个意图,或者我没有用正确的方法或者什么来设置回调。我知道这个话题已经讨论过,但我没有想法。谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值