简体   繁体   English

使用数据库值设置 TextView

[英]Setting a TextView with a database value

What is the best practice to set text for a textview with a given string and some value from my database?使用给定字符串和数据库中的某些值为 textview 设置文本的最佳做法是什么?

My MainActivity :我的主要活动

MyModel model;

TextView title = (TextView) findViewById(R.id.tvTitle);
title.setText("Username: ", model.getName());

My Model我的模特

private String name;

public String getName() {
    return name;
}

I couldn't find a solution so far.到目前为止我找不到解决方案。

The method setText() has several signatures, but the one you need is:方法setText()有几个签名,但您需要的是:

public final void setText (CharSequence text) public final void setText(CharSequence文本)

So you could do:所以你可以这样做:

title.setText("Username: " + model.getName());

but AS usually complains in these cases that you should avoid concatenating strings inside setText() ,但是在这些情况下, AS通常会抱怨您应该避免在setText()连接字符串,
so what you can do is:所以你可以做的是:

String str = "Username: " + model.getName();
title.setText(str);

Also you should consider to store literal values like "Username: " in resources like:您还应该考虑在资源中存储诸如"Username: "类的文字值:

<string name="username">Username:</string>

and use it like this:并像这样使用它:

String str = getResources().getString(R.string.username) + model.getName();
title.setText(str);

Creating getters and setters are a good practice, which you can automatically generate in Android Studio after defining the variables for the model.创建 getter 和 setter 是一个很好的做法,您可以在为模型定义变量后在 Android Studio 中自动生成它们。 Other than that I dont know why in your getName() your method wants to return a long and you are actually returning a string.除此之外我不知道为什么在你的getName()你的方法想要返回一个 long 而你实际上返回一个字符串。 Change long to Stringlong更改为String

like this :像这样 :

  public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

Everything others have said about concatenating strings outside of the setText method is valid.其他人所说的关于在 setText 方法之外连接字符串的所有内容都是有效的。 As far as placement in the Activity or Fragment ( I'm using a Fragment in this example ), I use the follow conventions:至于在 Activity 或 Fragment 中的位置(我在本例中使用的是 Fragment ),我使用以下约定:

Declare my class fields in the class body outside of my methods ( for class-wide access )在我的方法之外的类主体中声明我的类字段(用于类范围的访问

public class MyFragment extends Fragment {

    private TextView titleView;

    public MyFragment() {
    //Constructor
    }
    // and so on ...
}

Then, find my TextView reference once the Fragment is inflated, and if needed immediately, set its value然后,在 Fragment 膨胀后找到我的 TextView 引用,如果需要立即设置其值

// ...
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    View view = inflater.inflate(R.layout.fragment_container, container, false);

    titleView = view.findViewById(R.id.title);
    String titleStr = "Username: " + model.getName();
    titleView.setText(titleStr);

    return view;
}
// ...

If I'm expecting the database value in question to change ( for example the user updating their username in settings ) then I may also wish to make use of certain lifecycle methods that trigger when a Fragment ( or Activity ) resumes after being suspended, but is not completely rebuilt.如果我希望有问题的数据库值发生变化(例如用户在设置中更新他们的用户名),那么我可能还希望利用某些生命周期方法,这些方法在 Fragment(或 Activity )在暂停后恢复时触发,但是没有完全重建。

// ...
@override
public void onResume() {

    titleStr = "Username: " + model.getName();
    titleView.setText(titleStr);

}
// ...

Here's a link regarding the Android Activity Lifecycle if you're not familiar with it, and here's a good rundown on Fragments .如果您不熟悉,这里有一个关于Android Activity Lifecycle的链接,这里有一个关于Fragments的很好的纲要。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM