在用戶注冊后的專案中,我想在下一頁顯示用戶名
因此,此行有效,但只有第一個用戶注冊,如果另一個用戶注冊,它將顯示第一個用戶名。
displName =findViewById(R.id.displayName);
displName.setText(database.getAllNote().get(0).getUserName());
我的資料庫
@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;
}
我該如何解決這個小問題?請幫忙
uj5u.com熱心網友回復:
你總是得到第一個注冊用戶,因為你總是檢索表索引 0
displName.setText(database.getAllNote().get(0).getUserName());
所以我建議你使用 SharedPreference 來增加注冊用戶的數量。
當用戶成功注冊時,您必須增加用戶數
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();
之后,將您的 displName.setText 更改為:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Int countUser = pref.getInt("count_user", 0); // getting Integer
displName.setText(database.getAllNote().get(countUser).getUserName());
您將始終獲得最新的注冊用戶名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477354.html
