简体   繁体   中英

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?

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:

public final void setText (CharSequence text)

So you could do:

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

but AS usually complains in these cases that you should avoid concatenating strings inside 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:

<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. Other than that I dont know why in your getName() your method wants to return a long and you are actually returning a string. Change long to 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. As far as placement in the Activity or Fragment ( I'm using a Fragment in this example ), I use the follow conventions:

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

// ...
@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.

// ...
@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 . Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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