Access TextView from another class

I have my main boot class loading main.xml, but I'm trying to figure out how to access a TextView from another class that loads information from a database. I would like to publish this information in a TextView.

So far, I have not found useful examples on Google.

EDIT: This is my class that does the database work:

import android.widget.TextView;import android.view.View; public class DBWork{ private View view; ... TextView tv = (TextView) view.findViewById(R.id.TextView01); tv.setText("TEXT ME") 

However, every time I do this, I get a nullpointerexception

+4
source share
6 answers

If I were you, I would save all your UI updates in your main Activity class. You simply get your DBWork class to return the text you want to display to your Activity . So something like:

 public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) view.findViewById(R.id.TextView01); DBWork db = new DBWork("192.168.2.14:8080/DH/DPost";, "test_db"); tv.setText(db.getText()); } } 

Then your db class:

 public class DBWork{ ... public String getText(){ //do stuff return ""; } } 

Please note that any database operations that you perform during onCreate should be as fast as possible, as they work in the user interface thread. If you intend to make lengthy database queries, you should use AsyncTask or create a new Thread and use Handler to update the user interface.

+2
source

You should use LayoutInflater:

  LayoutInflater inflater = getLayoutInflater(); View myView = inflater.inflate(R.layout.main, null); TextView myTextView = (TextView) findViewById(R.id.myTextViewInXml); 
+3
source

You can pass links to all of your controls after you instantiate the class. I personally pass the entire class that has all the controls as public variables.

+1
source

You must define your TextView using xml and assign it an identifier. Example:

 <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:textColor="#FFF" android:gravity="center_vertical|right" android:paddingLeft="5dip" android:paddingRight="5dip" android:text="@string/name" android:id="@+id/myTextViewInXml"/> 

Then you can access it in each class using

  TextView myTextView = (TextView) findViewById(R.id.myTextViewInXml); 
0
source

Right:

  LayoutInflater inflater = getLayoutInflater(); View myView = inflater.inflate(R.layout.main, null); TextView myTextView = (TextView) findViewById(R.id.myTextViewInXml); 

But before you specify which file you want to use:

setContentView (XML file identifier that includes R.id.myTextViewInXml)

Without this, findViewById(R.id.myTextViewInXml); will always return null .

This is an example:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.virtualtour); // before !! progress_bar = (ProgressBar) findViewById(R.id.vt_progress); loading_ly = (RelativeLayout) findViewById(R.id.vt_layout_loading); panorama_description = (TextView) findViewById(R.id.vt_description); virtualtour_relatively = (RelativeLayout) findViewById(R.id.vt_layout_opengl); ......... 
0
source
 TextView bt1 =m_activity.findViewById(R.id.textview1); 

In the above example, where view bt1 belongs to another class. You can access bt1 with an instance of the class and get the identifier. Now do what you want to do with the bt1 instance.

Note. Make Sure bt1 public in another class

0
source

Source: https://habr.com/ru/post/1334418/


All Articles