Home > Back-end >  i want to display username in my project using SQLite?
i want to display username in my project using SQLite?

Time:05-18

in my project after user sign up , i want to display the username in next page

so, this line works but with only first user sign up if another user sign up it will display the first username.

displName =findViewById(R.id.displayName);
displName.setText(database.getAllNote().get(0).getUserName()); 

my database

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS "   TABLE_NAME);
    onCreate(db);
    }

    public void openDatabase() {
        db = this.getWritableDatabase();}
      @SuppressLint("Range")
    public List<UserTasks> getAllNote(){
        List<UserTasks> listNote = new ArrayList<>();
        Cursor cur = null;
        db.beginTransaction();
        try{
            cur = db.query(TABLE_NAME, null, null, null, null, null, null, null);
            if(cur != null){
                if(cur.moveToFirst()){
                    do{
                        UserTasks note = new UserTasks();
                        note.setUser_id(cur.getInt(cur.getColumnIndex(UID)));
                        note.setUserName(cur.getString(cur.getColumnIndex(UserName)));
                        note.setPassword(cur.getString(cur.getColumnIndex(Password)));
                        note.setNote(cur.getString(cur.getColumnIndex(Note)));
                        note.setStatus(cur.getInt(cur.getColumnIndex(STATUS)));
                        note.setDate(cur.getString(cur.getColumnIndex(date)));
                        note.setTitle(cur.getString(cur.getColumnIndex(titleChe)));
                        note.setDecs(cur.getString(cur.getColumnIndex(desc)));
                        note.setDay(cur.getString(cur.getColumnIndex(day)));
                        listNote.add(note);
                    }
                    while(cur.moveToNext());
                }
            }
        }
        finally {
            db.endTransaction();
            assert cur != null;
            cur.close();
        }
        return listNote;
    }

how i can fix this small issue? pls help

CodePudding user response:

You always get the first sign up user because you always retrieve table index 0

displName.setText(database.getAllNote().get(0).getUserName());

So I suggest you to use SharedPreference to increment the count of signup user.

When user successfully signup you have to increase the number of user

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
Int countUser = pref.getInt("count_user", 0); // getting Integer

Editor editor = pref.edit();
editor.putInt("count_user", countUser 1);
editor.apply();

After that, change your displName.setText to this:

    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); 
    Int countUser = pref.getInt("count_user", 0); // getting Integer
    displName.setText(database.getAllNote().get(countUser).getUserName());

You will always get latest signup username.

  • Related