0

ユーザーがログインすると、アプリのアカウント設定アクティビティに移動すると、FrameLayout を介してユーザー名が表示されますが、ログインしていない場合はログイン ボタンが表示されます。これを処理するための PreferenceData クラスは、正しく設定したと思いますが、ログインすると、作成した FrameLayout の代わりにログイン ボタンが表示されます。データベースを使用してユーザー アカウントを保存しています。

これが私のコードです

    package com.fullfrontalgames.numberfighter;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

    public class AccountSettings extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.accountsettings);

            SharedPreferences appPref =
                    getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_Preferences", MODE_PRIVATE);

            String loggedin = PreferenceData.getUserLoggedInStatus(true);




            Button LoginAS = (Button)findViewById(R.id.LoginAS);
            Button Done = (Button)findViewById(R.id.done);
            FrameLayout accountframe = (FrameLayout)findViewById(R.id.AccountFrameLayout);
            TextView accounttv = (TextView)findViewById(R.id.AccountTextView);

            DBAdapter db = new DBAdapter(this);
            db = db.open();



            accountframe.setVisibility(View.GONE);
            LoginAS.setVisibility(View.GONE);


            Done.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent Intent = new Intent(AccountSettings.this,activity_main.class);
                    startActivity(Intent);
                }
            });


            if (accountframe.equals(loggedin))
            {
                accountframe.setVisibility(View.VISIBLE);
                accounttv.setText((CharSequence) appPref);

            } else {

                accountframe.setVisibility(View.GONE);
                LoginAS.setVisibility(View.VISIBLE);
                LoginAS.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        startActivity (new Intent ("com.fullfrontalgames.numberfighter.Login")); 

                    }
                });

            }







        }

    }



    PreferenceData Class

    package com.fullfrontalgames.numberfighter;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.view.View.OnClickListener;


public class PreferenceData {
    static final String PREF_LOGGEDIN_USERNAME = "logged_in_username";
    static final String PREF_USER_LOGGEDIN_STATUS = "logged_in_status";

    public static SharedPreferences getSharedPreferences(Context ctx) {
        return PreferenceManager.getDefaultSharedPreferences(ctx);
    }


    public static void setLoggedInUsername(Context ctx, String Username)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putString(PREF_LOGGEDIN_USERNAME, Username);
        editor.commit();
    }

    public static String getLoggedInUsername(Context ctx)
    {
        return getSharedPreferences(ctx).getString(PREF_LOGGEDIN_USERNAME, "");
    }

    public static void setUserLoggedInStatus(Context ctx, boolean status)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.putBoolean(PREF_USER_LOGGEDIN_STATUS, status);
        editor.commit();
    }

    public static boolean getUserLoggedInStatus(Context ctx)
    {
        return getSharedPreferences(ctx).getBoolean(PREF_USER_LOGGEDIN_STATUS, false);
    }

    public static void clearLoggedInUsername(Context ctx)
    {
        Editor editor = getSharedPreferences(ctx).edit();
        editor.remove(PREF_LOGGEDIN_USERNAME);
        editor.remove(PREF_USER_LOGGEDIN_STATUS);
        editor.commit();
    }


    public static void setUserLoggedInStatus(OnClickListener onClickListener,
            boolean status) {
        // TODO Auto-generated method stub

    }


    public static String getUserLoggedInStatus(boolean b) {
        // TODO Auto-generated method stub
        return null;
    }



    }
4

2 に答える 2

1

You are taking a view (FrameLayout):

FrameLayout accountframe = (FrameLayout)findViewById(R.id.AccountFrameLayout);

and comparing it to to a string:

if (accountframe.equals(loggedin))

can you please explain what are you trying to do? and where is the loggedin string stored?

I think it should be:

if ((CharSequence) appPref.toString().equals(loggedin))
{
...
}

Edit:

if this returns if user logged in or not:

String loggedin = PreferenceData.getUserLoggedInStatus(true);

then you should write something like this:

 if (loggedin.equals("true"))
 {
     accountframe.setVisibility(View.VISIBLE);
     accounttv.setText((CharSequence) appPref);
 }

Second edit: You are getting a Boolean parameter:

 public static boolean getUserLoggedInStatus(Context ctx)
 {
    return getSharedPreferences(ctx).getBoolean(PREF_USER_LOGGEDIN_STATUS, false);
 }

So you should store it into a Boolean parameter, and pass your activity context or this:

boolean loggedin = PreferenceData.getUserLoggedInStatus(YouactivityName.this);

if (loggedin == true)
{
    accountframe.setVisibility(View.VISIBLE);
    accounttv.setText((CharSequence) appPref);
}
于 2013-03-31T01:35:55.687 に答える
1
boolean loggedin = PreferenceData.getUserLoggedInStatus(this);

if (loggedin == true)
 {
     accountframe.setVisibility(View.VISIBLE);
     accounttv.setText((CharSequence) appPref);
 }

これを試してください。これが役に立てば幸いです。

于 2013-03-31T01:53:37.863 に答える