开发者

Accessing TextView from another class

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 loadi开发者_C百科ng 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


You should use LayoutInflater :

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


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.


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.


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


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

.........


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜