Thinh Vu
Thinh Vu

Reputation: 61

How to get a TextView from other class

Suppose, I have a MainActivity class. This class has a TextView. Then i write a new class to process this TextView. How to get the TextView from the MainActivity to process. Thank you!

public class MainActivity extends Activity {

TextView txtMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtMsg = (TextView) findViewById(R.id.txtMsg);
    new BackgroundAsyncTask(getApplicationContext());}

Another class

public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{

TextView textView;
Context context;
public BackgroundAsyncTask(Context context){
    this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
    // TODO Auto-generated method stub
    textView = (TextView) context.findViewById(R.id.txtMsg); //here has an error }

Upvotes: 0

Views: 1107

Answers (5)

Sergey Shustikov
Sergey Shustikov

Reputation: 15831

I dont know why you need to use textView in doInBackground method. Because doInBackground method do not have an access to UI thread!!! Use all calls of UI elements in onPostExecute,onPreExecute, onProgressUpdate.

findViewById is a method of Activity. so use this instead.

public class MainActivity extends Activity {

TextView txtMsg; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtMsg = (TextView) findViewById(R.id.txtMsg);
    new BackgroundAsyncTask(this);}

Another class

public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{

TextView textView;
Activity context;
public BackgroundAsyncTask(Activity context){
    this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
    // TODO Auto-generated method stub
    textView = (TextView) context.findViewById(R.id.txtMsg); //no errors }

Upvotes: 0

Anuj Sharma
Anuj Sharma

Reputation: 488

Create global instance outside your application.

public class CommonsData{
    public static TextView tvYourTextView;
}

Use this as declaration in the class where you wish to declare it.

CommonsData.tvYourTextView = (TextView) findViewById(R.id.txtMsg);

And use any where within your application scope, don't forget to add a null check before use.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33258

There are many ways. I am explaining two way here...

1) Write your BackgroundAsyncTask class in MainActivity.class instaead of creating new class, so that you can easily use TextView object.

2) Pass TextView object with parameter of BackgroundAsyncTask Constructor.

Like this

Call BackgroundAsyncTask from your MainActivity

new BackgroundAsyncTask(MainActivity.this, textView);

Your BackgroundAsyncTask Constructor

public BackgroundAsyncTask(Context context, TextView textview){
    this.context = context;
    this.textview = textview;
}

Upvotes: 2

WoookLiu
WoookLiu

Reputation: 402

Change the constructor method of Class BackgroundAsyncTask to:

public BackgroundAsyncTask(Context context, TextView textView){
    this.context = context;
    this.textView = textView;
}

And in your onCreate() method:

...
new BackgroundAsyncTask(getApplicationContext(), textView);

Upvotes: 0

Smith Stella
Smith Stella

Reputation: 149

If you want to process the TextView value, you can pass the value as :

public class BackgroundAsyncTask extends AsyncTask<String, String, Void>{

TextView textView;
Context context;
public BackgroundAsyncTask(Context context){
    this.context = context;
}
@Override
protected Void doInBackground(String... params) {
    String textBoxValue= params[0];
}

You can call this as :

new BackgroundAsyncTask(context).execute(textView.getText());

Upvotes: 0

Related Questions