Evgenij Reznik
Evgenij Reznik

Reputation: 18614

How to set text in TextView?

I have 2 Activities, this is the layout from the second one (extract):

<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" />

The corresponding values are being parsed from JSON in the first Activity and the function setValues within the second Activity is called:

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

act2.setValues(user, city);

In the second Activity the code looks as follows:

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);
}

This function is being called and the "user" is being returned, but the text in the TextViews is not changed.

With Runnable:

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);
}

Unfortunately the result is still the same: The TextViews remain empty.

Upvotes: 0

Views: 211

Answers (2)

Andr&#233;s Pachon
Andr&#233;s Pachon

Reputation: 838

Make sure the setValues() method is running on the UIThread. See: Painless Threading and Processes and Threads.

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15699

can't here we use intent in your scenario ?

android using intent....

Vogella Ariticle

in activity 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);

In activity 2 - in onCreate finction

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
}

Upvotes: 1

Related Questions