Accessing TextView from another class

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I've got my main startup class loading main.xml but I'm trying to figure out how to access the TextView from another class which is loading information from a database. I would like to publish that information to the TextView.

So far I've not found any helpful examples on Google.

EDIT: This is my class that is doing 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")

Yet, everytime I do that I get a nullpointerexception

回答1:

If I were you I'd keep all your UI updates within your main Activity class. You just get your DBWork class to return the text you want displayed 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 you perform during onCreate should be as quick as possible as they are running in the UI thread. If you are going to perform long running database queries then you should use an AsyncTask or create a new Thread and use a Handler to update the UI.



回答2:

You should use LayoutInflater :

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


回答3:

You can pass the references to all your controls after you created your class instance. I personally pass the whole class that has all controls as public variables.



回答4:

You havew to define your TextView using xml and giving it an id. 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 cann access it in every class using

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


回答5:

It's right:

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

But before you have to specify what file you want use by:

setContentView(id of XML file which include R.id.myTextViewInXml)

Without that the findViewById(R.id.myTextViewInXml); alway will 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);  .........


回答6:

TextView bt1 =m_activity.findViewById(R.id.textview1);

In the above example where view bt1 is from another class.you can access bt1 by using class instance and get the id.Now,do what ever you want do with bt1 instance.

Note: Make Sure bt1 is public in another class



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!