登录后在 textview 中显示 SQLite 数据
Posted
技术标签:
【中文标题】登录后在 textview 中显示 SQLite 数据【英文标题】:Display SQLite data in textview after login 【发布时间】:2018-04-30 22:58:12 【问题描述】:我有一个登录活动、注册活动和个人资料活动。按下登录按钮后,我可以在个人资料活动中显示我的用户名,这是登录后的第三次活动。但是,我无法将名字、姓氏、GRE、托福从数据库显示到个人资料活动。 vl 我如何在个人资料活动的文本视图中查看每个用户的名字、姓氏、gre 和 toefl。
这是我的代码:
这是 SQLiteDB ProfileDatabase.class
public class ProfileDatabaseHelper extends SQLiteOpenHelper
private static final int DATABASE_VERSION= 1;
private static final String DATABASE_NAME="profiles.db";
private static final String TABLE_NAME="profile";
private static final String COLUMN_PROFID="profId";
private static final String COLUMN_FNAME="firstName";
private static final String COLUMN_LNAME="lastName";
private static final String COLUMN_UNAME="userName";
private static final String COLUMN_PASS="pass";
private static final String COLUMN_GRE= "gre";
private static final String COLUMN_TOEFL= "toefl";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table profile (profId integer primary key not null," +
" firstName text not null, lastName text not null, userName text not null, pass text not null, gre integer not null, toefl integer not null);";
public ProfileDatabaseHelper(Context context)
super(context,DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(TABLE_CREATE);
this.db=db;
public void insertProfile(ProfileList pl)
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "Select * from profile";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_PROFID,count);
values.put(COLUMN_FNAME,pl.getFName());
values.put(COLUMN_LNAME,pl.getLName());
values.put(COLUMN_UNAME,pl.getUName());
values.put(COLUMN_PASS,pl.getPass());
values.put(COLUMN_GRE,pl.getGre());
values.put(COLUMN_TOEFL,pl.getToefl());
db.insert(TABLE_NAME, null,values);
db.close();
public String searchPass(String uname)
db = this.getReadableDatabase();
String query = "select username, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "Not found";
if(cursor.moveToFirst())
do
a = cursor.getString(0);
if(a.equals(uname))
b = cursor.getString(1);
break;
while(cursor.moveToNext());
return b;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
这是我的 login.class
public class LoginUser extends AppCompatActivity
ProfileDatabaseHelper helper = new ProfileDatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_user);
public void onBtnClick(View v)
if (v.getId() == R.id.loginButton)
EditText loginName = (EditText)findViewById(R.id.loginUserName);
String loginNameStr = loginName.getText().toString();
EditText password = (EditText)findViewById(R.id.loginPass);
String pass = password.getText().toString();
String passwordLogin = helper.searchPass(loginNameStr);
if(pass.equals(passwordLogin))
Intent i = new Intent(LoginUser.this, ProfileUser.class);
i.putExtra("Username", loginNameStr);
startActivity(i);
else
Toast.makeText(LoginUser.this,"Username and Password don't match",Toast.LENGTH_LONG).show();
if (v.getId() == R.id.signUpButton)
Intent i = new Intent(LoginUser.this, RegisterUser.class);
startActivity(i);
这是显示用户个人资料的第三个活动
public class ProfileUser extends AppCompatActivity
ProfileDatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_user);
String username = getIntent().getStringExtra("Username");
TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
userText.setText(username);
//How canI enter each person personal first name, last name, grey and
toefl
TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
TextView userText = (TextView)findViewById(R.id.userNameDisplayText);
【问题讨论】:
【参考方案1】:您需要编辑您的searchPass(username, password)
方法,使其在username
和password
匹配时返回完整的User
对象,然后在您的IF
条件User user = helper.searchPass(loginNameStr, password);
之前调用此方法
现在您有了包含所有用户详细信息的 user
对象,您需要在检查密码后发送其他内容,如下所示。
if(user.getPassword().equals(passwordLogin))
Intent i = new Intent(LoginUser.this, ProfileUser.class);
i.putExtra("Username", user.getUsername());
i.putExtra("firstName", user.getFirstName());
i.putExtra("lastName", user.getLastName());
i.putExtra("gre", user.getGre());
i.putExtra("toefl", user.getToefl());
startActivity(i);
然后在您的ProfileUser
活动中从intent
检索详细信息,如下所示
String username = getIntent().getStringExtra("Username");
String firstName = getIntent().getStringExtra("firstName");
String lastName = getIntent().getStringExtra("lastName");
String gre = getIntent().getStringExtra("gre");
String toefl = getIntent().getStringExtra("toefl");
【讨论】:
如何在 searchPass 中返回用户对象。 嘿,我尝试添加返回对象,但在 null 对象引用上出现 user.getPassword() 错误。 您应该首先在model
类中添加该字段,并确保在对象中设置其 value
。以上是关于登录后在 textview 中显示 SQLite 数据的主要内容,如果未能解决你的问题,请参考以下文章
求和列 SQLite Android 并在 TextView 上显示 [关闭]