Kaboom
Kaboom

Reputation: 684

Android: Checking for logged in account on my app

My android application is using webView for loading the mobile version of the website. Currently I have cookies enabled and javascript and whatnot. Since the login is happening on the website and not on the app client side, I use a cookie called androidlogin to see if they are logged in.

If the cookie is there, they must be logged in, if it isn't, they are not logged in. The issue I am having is when they are logged in I want to show a log out button on my navigation drawer.

Here is the code for the cookies:

...
public String cookie = CookieManager.getInstance().getCookie("site.com");

protected void onCreate(Bundle savedInstanceState) {
    ...
    CookieManager.getInstance().setAcceptCookie(true);
    ...
}
private void addDrawerItems() {
    if(cookie.contains("androidlogin")) {
        String[] osArray = { "item 1", "item 2", "Logout" };
    } else {
        String[] osArray = { "item 1", "item 2" };
    }
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    ...
}

This is getting the error

Error:(76, 88) error: cannot find symbol variable osArray

So my cookie variable is set, I can print it to the log and it does show the cookie as being there, but how can I change what happens in the drawer depending on what the cookie says? Switching from php to java has been a challange to say the least lol.

Thanks

Upvotes: 0

Views: 35

Answers (1)

James McCracken
James McCracken

Reputation: 15766

You're declaring osArray inside the if/else so it is not accessable outside where you're trying to use it. Try this way:

private void addDrawerItems() {
    final String[] osArray;
    if(cookie.contains("androidlogin")) {
        osArray = new String[]{ "item 1", "item 2", "Logout" };
    } else {
        osArray = new String[]{ "item 1", "item 2" };
    }
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    ...
}

Upvotes: 2

Related Questions