1

I have tried following code in which i am trying to create session of user until it logout the application. The problem is i am trying to send information of user to the page profile.java. I tried using intent extras but it failed. Any suggestions please.

login.java

public class login extends AppCompatActivity {
    private FunTubeOperations FunTubeDBOperations;
    private DatabaseWrapper db;
    private SessionManager session;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
        final AlertDialog ad = new AlertDialog.Builder(this).create();
        Button login = (Button) findViewById(R.id.login);
        Button reg = (Button) findViewById(R.id.reg);
        FunTubeDBOperations = new FunTubeOperations(this);
        FunTubeDBOperations.open();
       // SQLite database handler
        db = new DatabaseWrapper(getApplicationContext());

        // Session manager
       session = new SessionManager(getApplicationContext());

        // Check if user is already logged in or not
        if (session.isLoggedIn()) {


            // User is already logged in. Take him to main activity
            Intent intent = new Intent(login.this, profile.class);
            //intent.putExtra("name", user);
            startActivity(intent);
            finish();
        }
       try {
          login.setOnClickListener(new View.OnClickListener() {
           public void onClick(View arg0) {
             EditText email = (EditText) findViewById(R.id.email);
             EditText password = (EditText) findViewById(R.id.password);
              if (email != null && password !=null) {
               String em=email.getText().toString();
               String pwd=password.getText().toString();
               String user=  FunTubeDBOperations.loginUsers(em, pwd);
                  if(user!=null)
                    {
                   session.setLogin(true);
                   Intent intent = new Intent(login.this, profile.class);
                   intent.putExtra("name", user);
                   startActivity(intent);
                   finish();
                   }}
            });

        } catch (Exception er) {
            ad.setMessage(er.toString());
        }
    }
}

DataBaseWrapper.java

 public class DatabaseWrapper extends SQLiteOpenHelper {
    private static final String TAG = DatabaseWrapper.class.getSimpleName();


    //For user table
    public static final String FUNTUBE = "User";
    public static final String FUNTUBE_ID = "_id";
    public static final String FUNTUBE_UNAME = "_username";
    public static final String FUNTUBE_EMAIL = "_email";
    public static final String FUNTUBE_PASSWORD = "_password";
    public static final String FUNTUBE_FNAME = "_fname";
    public static final String FUNTUBE_LNAME = "_lname";
    public static final String FUNTUBE_PHONE = "_phone";
    public static final String FUNTUBE_COUNTRY = "_country";
    public static final String FUNTUBE_GENDER = "_gender";
    public static final String FUNTUBE_VIDEOPATH = "_videopath";
    public static final String FUNTUBE_BDAY = "_bday";
    public static final String FUNTUBE_YEAR = "_year";
    public static final String FUNTUBE_INTEREST = "_interest";
    public static final String FUNTUBE_RELIGION = "_religion";
    public static final String FUNTUBE_ABOUT = "_about";
    public static final String FUNTUBE_QUOTE = "_qoute";
    public static final String FUNTUBE_JOB = "_job";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        }


    private static final String DATABASE_NAME = "FunTubeApplication.db";
    private static final int DATABASE_VERSION = 1;

    // creation SQLite statement
    private static final String DATABASE_CREATE = "create table " + FUNTUBE
            + "(" + FUNTUBE_ID + " integer primary key autoincrement, "

            + FUNTUBE_UNAME + " text not null,"
            + FUNTUBE_EMAIL + " text not null,"
            + FUNTUBE_PASSWORD + " text not null,"
            + FUNTUBE_FNAME + " text not null,"
            + FUNTUBE_LNAME + " text not null,"
            + FUNTUBE_PHONE + " text not null,"
            + FUNTUBE_COUNTRY + " text not null,"
            + FUNTUBE_GENDER + " text not null,"
            + FUNTUBE_VIDEOPATH + " text not null,"
            + FUNTUBE_BDAY + " text not null,"
            + FUNTUBE_YEAR + " text not null,"
            + FUNTUBE_INTEREST + " text not null,"
            + FUNTUBE_RELIGION + " text not null,"
            + FUNTUBE_ABOUT + " text not null,"
            + FUNTUBE_QUOTE + " text not null,"
            + FUNTUBE_JOB + " text not null)";



    public DatabaseWrapper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (newVersion > oldVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE);
            db.execSQL("DROP TABLE IF EXISTS " + FUNTUBE_UPLOADFILE);
            onCreate(db);
        }


    }

    /**
     * Getting user data from database
     */
    public HashMap<String, String> getUserDetails() {
        HashMap<String, String> user = new HashMap<String, String>();
        String selectQuery = "SELECT  * FROM " + FUNTUBE;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // Move to first row
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("_username", cursor.getString(1));
            user.put("_email", cursor.getString(2));
            user.put("_password", cursor.getString(3));
            user.put("_fname", cursor.getString(4));
            user.put("_lname", cursor.getString(5));
            user.put("_phone", cursor.getString(6));
            user.put("_country", cursor.getString(7));
            user.put("_gender", cursor.getString(8));
            user.put("_videopath", cursor.getString(9));
            user.put("_bday", cursor.getString(10));
            user.put("_year", cursor.getString(11));
            user.put("_interest", cursor.getString(12));
            user.put("_religion", cursor.getString(13));
            user.put("_about", cursor.getString(14));
            user.put("_quote", cursor.getString(15));
            user.put("_job", cursor.getString(16));
        }
        cursor.close();
        db.close();
        // return user
        Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

        return user;
    }
}
tabia
  • 631
  • 2
  • 10
  • 33

1 Answers1

0

It is possible using your way but i have another things that can also help you.

do one thing create public static variable and set your data in this variable. after that from your profile class use this variable like global variable.

like Way no 1.

login.java

public static String UserName = null;
// set user name after login in above variable
UserName = "tempUser";

// using intent go to profile page without passing any extra data
// before call intent check that UserName should not be null;

profile.java

// get user name
TextView username;
username.setText(Login.UserName);

way no.2

Store data in preference.. set preference data form login and get preference data from profile.

Sameer Donga
  • 988
  • 9
  • 24
  • `Login.UserName` should be `login.UserName` (although the class should really be renamed to `Login` to match standards). Apart from that I'd recommend this way. The preference way (2) may be slightly more robust than way 1, and you may find you want to store the username in preferences anyway to prevent the user from having to log in each time. – Peter Gordon Dec 23 '15 at 10:38