user8075840
user8075840

Reputation:

Setting TextView in Another Class

I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...

Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)

    yes = (Button) findViewById(R.id.finalYes);
    yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent intent = getIntent();
            String text = intent.getStringExtra("New Text");
            t = (TextView) findViewById(R.id.abusedOrNah);
            t.setText(text);
            Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
            startActivity(myIntent);
        }
    });

This is how I have the static variable t defined in my highrisk activity class...

HighRiskActivity(this is where I want the text to be set and displayed)

public static TextView t;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.high_risk);

    Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
    myIntent.putExtra("Text", "New Text");
    startActivity(myIntent);

Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)

Upvotes: 0

Views: 248

Answers (2)

Falke Design
Falke Design

Reputation: 11338

You can use Bounds, to get the the data to the new Activity and set the text from there:

Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);

And in the Question13Activity.class:

Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");

t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);

UPDATE:

Use it so:

HighRiskActivity.class:

 yes = (Button) findViewById(R.id.finalYes);
    yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
            myIntent.putExtra("TestKey", "My new Text");
            startActivity(myIntent);
        }
    });

Question13Activity.class:

TextView t;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");

t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);

Upvotes: 1

bem22
bem22

Reputation: 277

In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.

Unless a class does have a reference of your context, you cannot call 'findViewById()'

To do that, you use Context as a parameter for custom class constructor.

Just after you have set the context will you be able to call

TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
        txtView.setText("foo");
}

Upvotes: 0

Related Questions