从另一个类访问 TextView

发布于 2024-10-10 09:08:33 字数 387 浏览 1 评论 0原文

我的主启动类正在加载 main.xml,但我试图弄清楚如何从另一个正在从数据库加载信息的类访问 TextView。我想将该信息发布到 TextView。

到目前为止,我还没有在谷歌上找到任何有用的例子。

编辑: 这是我的类正在执行数据库工作:

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

但是,每次我这样做时都会遇到空指针异常

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

放肆 2024-10-17 09:08:33

您应该使用 LayoutInflater :

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

You should use LayoutInflater :

        LayoutInflater inflater = getLayoutInflater();
        View myView = inflater.inflate(R.layout.main, null);
        TextView myTextView = (TextView) findViewById(R.id.myTextViewInXml);
倾听心声的旋律 2024-10-17 09:08:33

如果我是您,我会将所有 UI 更新保留在您的主 Activity 类中。您只需让 DBWork 类返回您想要显示到 Activity 的文本。因此,类似于:

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

然后您的数据库类:

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

请注意,您在 onCreate 期间执行的任何数据库操作都应该尽可能快,因为它们在 UI 线程中运行。如果您要执行长时间运行的数据库查询,那么您应该使用 AsyncTask 或创建一个新的 Thread 并使用 Handler 来更新 UI 。

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.

苦妄 2024-10-17 09:08:33

创建类实例后,您可以将引用传递给所有控件。我个人将所有控件都作为公共变量传递给整个类。

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.

柠檬色的秋千 2024-10-17 09:08:33

您必须使用 xml 定义 TextView 并为其指定一个 id。示例:

<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"/>

那么您可以使用以下方式在每个类中访问它

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

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);
冷…雨湿花 2024-10-17 09:08:33

是的:

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

但在您必须指定要使用的文件之前:

setContentView(包含 R.id.myTextViewInXml 的 XML 文件的 ID)

如果没有,findViewById(R.id.myTextViewInXml); 总是会返回 null

这是一个例子:

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

.........

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

.........
戴着白色围巾的女孩 2024-10-17 09:08:33
TextView bt1 =m_activity.findViewById(R.id.textview1);

在上面的示例中,视图 bt1 来自另一个类。您可以使用类实例访问 bt1 并获取 id。现在,对 bt1 实例执行您想要执行的操作。

注意:确保 bt1 在另一个类中是公开的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文