views:

58

answers:

2

Hi,

I use Alert Dialog as Login. So after closing this dialog, any value assigned at dialog show() is lost. how to get back this value? my code is below

private void accessPinCode()
{
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.dialog_login, null);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);                 
    alert.setTitle("Title");  
    alert.setMessage("Enter Pin :");                
    alert.setView(textEntryView);       

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {  
        public void onClick(DialogInterface dialog, int whichButton) {          
            EditText mUserText;
            mUserText = (EditText) textEntryView.findViewById(R.id.txt_password);
            //String strPinCode = mUserText.getText().toString();
            Log.d( TAG, "Pin Value 1 : " + mUserText.getText().toString());               
            strPIN = mUserText.getText().toString();
            Log.d( TAG, "strPIN inside accessPinCode : " + strPIN);
            fPIN= checkPINCode();
            Log.d( TAG, "fPass : " + fPIN);


            return;                  
        }  
    });  

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            return;   
        }
    });

    alert.show();
    Log.d( TAG, "strPIN outside Alert Show : " + strPIN);
}

Based on my code, strPIN and FPIN values are lost. I wanna use those value outside accessPinCode function. how to get?

Actually, I call this function at tabchanged event. If login pass, the user could access another tab. But all've already worked in tab changed event before clicking AlertDialog's Ok button. My tab event like below

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub

                if (tabId.equals("index"))
                {
                    tabHost.setCurrentTab(1);
                    accessPinCode();
                }
                Log.d( TAG, "tabId : "+ tabId );    
            }
        });

Is there any type of Dialog suitable for Login? How to solve?

Thank you.

A: 

Edit: In your negative/positive button you have to call a function from the surrounding class, setting those two parameters with the values gathered in the AlertDialog.

something like:

private String mStrPin;
private float mFPin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

...

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {   
            String strPin = "1234";
            float fPin = 1.234f;
            public void onClick(DialogInterface dialog, int which) {
                loggedIn(strPin, fPin);
            }
    }

...

}
private void loggedIn(String strPin, float fPin) {
    mStrPin = strPin;
    mFPin = fPin;
}
slup
+1 for the first part, -1 for the last part (do you seriously encourage that?).
Felix
A: 

A simplified example:

public interface TextListener {
    void onPositiveResult(CharSequence text);
}

public static AlertDialog getTextDialog(Context ctx,
        final TextListener listener) {
    View view = LayoutInflater.from(ctx).inflate(R.layout.dialog, null);
    final TextView tv = (TextView) view.findViewById(R.id.tv);
    AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
    builder.setView(view);
    //
    builder.setPositiveButton(android.R.string.ok, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onPositiveResult(tv.getText());
        }
    });
    builder.setNegativeButton(android.R.string.cancel, null);
    return builder.create();
}
alex
@ alex, pls re-check my post. I edit again.