0

我有 2 个活动,这是第二个活动的布局(摘录):

<TextView
            android:id="@+id/user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
<TextView
            android:id="@+id/city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

在第一个 Activity 中从 JSON 解析相应的值,并调用第二个 Activity 中的函数 setValues:

String user = oj.getString("user");
String city = oj.getString("city");

act2.setValues(user, city);

在第二个 Activity 中,代码如下所示:

user = (TextView) findViewById(R.id.user);
city = (TextView) findViewById(R.id.city);

public void setValues(String user, String city) {
    Log.d("user", user); // THIS IS BEING RETURNED
    this.user.setText(user);
    this.city.setText(city);
}

正在调用此函数并返回“用户”,但TextViews 中的文本没有更改。

使用可运行:

Intent intent = new Intent(this, Act2.class);
startActivity(intent);
run();

public void run() {
    Log.d("run", "it's running"); // IS RETURNED
    desc.setValues(user, city);
}


// SECOND ACTIVITY
public void setValues(String user, String city) {
    Log.d("user", user); // IS RETURNED

    this.user.setText(user);
    this.city.setText(city);
}

不幸的是,结果仍然是一样的:TextViews 仍然是空的。

4

2 回答 2

1

在这里我们不能在您的场景中使用意图吗?

android使用意图....

沃格拉文章

在活动 1-

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");

startActivity(i);

在活动 2 中 - 在 onCreate 函数中

Bundle extras = getIntent().getExtras();

if (extras == null) {
        return;
        }
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
    // Do something with the data
}
于 2012-05-30T12:17:24.843 回答
0

确保该setValues()方法在UIThread上运行。请参阅:无痛线程进程和线程

于 2012-05-30T12:24:24.283 回答