Ludwig Bossle
Ludwig Bossle

Reputation: 367

Setting the title of the AppBar

As I'm currently working on an app with parse I want to have a list of users and if you tap on one of their names a new activity is launched with their profile. When the a activity is launched the objectId of the parse object is sent to the activity with the intent. Here is my problem: I want the title of the activity to be the username. I found a solution but it's somewhat strange. Maybe someone has a better solution?

Here is my code(which works fine but I have to set the title twice otherwise it doesn't work):

public class UserProfileActivity extends AppCompatActivity {

    String userId;
    String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        userId = getIntent().getStringExtra("userId");

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo("objectId", userId);
        query.setLimit(1);
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() != 0) {
                        for (ParseUser user : objects) {
                            Log.i("AppInfo", user.getUsername());
                            getSupportActionBar().setTitle(user.getUsername());
                            username = user.getUsername();
                        }
                    }
                }
            }
        });
        getSupportActionBar().setTitle(username);
    }
}

Upvotes: 0

Views: 73

Answers (2)

Umair Farooq
Umair Farooq

Reputation: 1704

You are using an async call of parse. And when that call is made, the next line (that is setting the title) is run instantly. At that time the username variable contains null.

And in the done callback of the parse query, you are setting the title again. Actually this is the actual line that is doing work for you.

query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() != 0) {
                        for (ParseUser user : objects) {
                            Log.i("AppInfo", user.getUsername());
                            getSupportActionBar().setTitle(user.getUsername());
                            username = user.getUsername();
                        }
                    }
                }
            }
        });

You can remove this line getSupportActionBar().setTitle(username); after the call and the code will work same but it will set the tile with just setting the title once.

Upvotes: 0

Neerajlal K
Neerajlal K

Reputation: 6828

You don't need to set it twice.

The username will be available inside the done method when the query completes. The setTitle line outside the loop will not work. Remove the setTitle after the findInBackground.

See this code.

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", userId);
query.setLimit(1);
query.findInBackground(new FindCallback<ParseUser>() {
    @Override
    public void done(List<ParseUser> objects, ParseException e) {
        if (e == null) {
            if (objects.size() != 0) {
                for (ParseUser user : objects) {
                    Log.i("AppInfo", user.getUsername());
                    username = user.getUsername();
                    // adding some null checks
                    if(null != getSupportActionBar() && username != null && !username.equals("")){
                        getSupportActionBar().setTitle(username);
                    }
                }
            }
        }
    }
});

getSupportActionBar().setTitle(username);  // remove this line

Upvotes: 2

Related Questions