android bundle传递参数,android – 使用bundle将数据从一个活动传递...

我目前正在尝试通过REST API调用获取数据,解析它以获取我需要的信息,然后将该信息传递给新活动.我正在使用loopj.com中的异步HTTP客户端作为REST客户端,然后分别使用以下代码将onClick和onCreate用于当前和未来的活动.

Eclipse没有为我的任何代码传递任何错误,但是当我尝试在模拟器中运行时,在新活动/视图打开时我什么也得不到(即空白屏幕).我试图在我的REST CLIENT中使用不同的URL进行编码,但我仍然没有看到任何内容.我甚至通过在onClick中注释try / catch并更改bundle.putString(“VENUE_NAME”,venueName)中的venueName来取消API调用. to searchTerm.仍然,新视图出现但没有显示任何内容.什么没有通过,或者我忘记了第二个活动显示的是什么名字?

public void onClick(View view) {

Intent i = new Intent(this, ResultsView.class);

EditText editText = (EditText) findViewById(R.id.edit_message);

String searchTerm = editText.getText().toString();

//call the getFactualResults method

try {

getFactualResults(searchTerm);

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//Create the bundle

Bundle bundle = new Bundle();

//Add your data from getFactualResults method to bundle

bundle.putString("VENUE_NAME", venueName);

//Add the bundle to the intent

i.putExtras(bundle);

//Fire the second activity

startActivity(i);

}

第二个活动中应该接收意图和捆绑并显示它的方法:

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//Get the message from the intent

//Intent intent = getIntent();

//String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

//Get the bundle

Bundle bundle = getIntent().getExtras();

//Extract the data…

String venName = bundle.getString(MainActivity.VENUE_NAME);

//Create the text view

TextView textView = new TextView(this);

textView.setTextSize(40);

textView.setText(venName);

//set the text view as the activity layout

setContentView(textView);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

getActionBar().setDisplayHomeAsUpEnabled(true);

}

}

谢谢你的帮助.非常感谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`android.os.Bundle` 是 Android 中的一个类,用于存储和传递数据。在 Android 应用程序中,`Bundle` 对象通常用于在活动之间传递数据,或者在活动恢复时保存和恢复数据。`Bundle` 对象是一个键值对列表,其中键是字符串,值可以是任意类型的 Java 对象,例如字符串、整、布尔值、组等。 `android.os.Bundle` 类位于 Android 框架中的 `android.os` 包中。要在您的 Android 应用程序中使用 `Bundle` 类,您需要在您的 Java 代码中导入它。您可以使用以下代码导入 `Bundle` 类: ```java import android.os.Bundle; ``` 然后您就可以在您的应用程序中创建和使用 `Bundle` 对象了。例如,您可以在一个活动使用以下代码创建一个 `Bundle` 对象并将其传递给另一个活动: ```java // 创建一个 Bundle 对象 Bundle bundle = new Bundle(); // 向 Bundle 对象中添加数据 bundle.putString("name", "John"); bundle.putInt("age", 25); // 创建一个 Intent 对象并将 Bundle 对象传递给另一个活动 Intent intent = new Intent(this, AnotherActivity.class); intent.putExtras(bundle); startActivity(intent); ``` 在另一个活动中,您可以使用以下代码获取传递的 `Bundle` 对象并从中获取数据: ```java // 获取传递Bundle 对象 Bundle bundle = getIntent().getExtras(); // 从 Bundle 对象中获取数据 String name = bundle.getString("name"); int age = bundle.getInt("age"); ``` 因此,`android.os.Bundle` 类是用于在 Android 应用程序中存储和传递数据一个类,它位于 `android.os` 包中,您需要在您的 Java 代码中导入它才能使用它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值