登录后在textview中显示SQLite数据

编程入门 行业动态 更新时间:2024-10-27 20:31:42
本文介绍了登录后在textview中显示SQLite数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个登录活动,注册活动和个人资料活动.按下登录按钮后,我可以在配置文件活动中显示我的用户名,这是登录后的第三次活动.但是,我无法显示数据库中的名字,姓氏,GRE,toefl个人资料活动.我如何在个人资料活动的文本视图中查看每个用户的名字,姓氏,gre和toefl.

I have a login activity, registration activity, and profile activity. After I press the login button I'm able to display my username on the profile activity which is the activity third after login.But, I'm not able to display the first name, last name, GRE, toefl from the database to the profile activity. How vl i view each users first name, last name, gre and toefl in a textview in profile activity.

这是我的代码:

这是SQLiteDB ProfileDatabase.class

This is 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

here is my 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); } } }

这是显示用户个人资料的第三项活动

This is the third activity which displays users profile

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); } }

推荐答案

您需要编辑searchPass(username, password)方法,使其在username和password匹配时返回完整的User对象,然后在IF条件User user = helper.searchPass(loginNameStr, password);

You need to edit your searchPass(username, password) method in a way that it should return complete User object when username and password is matched and then call this method before your IF condition User user = helper.searchPass(loginNameStr, password);

现在您有了具有所有用户详细信息的user对象,您需要在检查如下所示的密码后发送其他内容.

Now you have the user object with all user details, you need to send other stuff after checking the password like below .

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检索详细信息

and then retrieve the details from intent in your ProfileUser activity like below

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");

更多推荐

登录后在textview中显示SQLite数据

本文发布于:2023-11-06 04:39:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   textview   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!