Android Studio新手,所以一些帮助会有很长的路要走!
尝试在用户输入电子邮件的位置进行搜索,如果是在数据库中,则会在文本视图中显示。
我认为我的选择是正确的,但这是我的数据库处理程序类中的方法:
public String searchEmail(String email)
{
SQLiteDatabase db = this.getWritableDatabase();
String fName;
String sName;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
fName = c.getString(c.getColumnIndex("FIRSTNAME"));
sName = c.getString(c.getColumnIndex("SURNAME"));
}
c.close();
return email;
}
然后我的搜索:
public void searchFriend(View v) {
EditText emailSearch = (EditText) findViewById(R.id.emailSearch);
String emailSearchString = emailSearch.getText().toString();
myDB.searchEmail(emailSearchString);
//I need to get the first name and surname from the db handler class
TextView firstName = (TextView)findViewById(R.id.firstname);
TextView surname = (TextView)findViewById(R.id.surname);
firstName.setText("");
surname.setText("");
}
我发现很难理解,如果电子邮件匹配,我将如何显示名字和姓氏。
谢谢你们,我爱你
答案 0 :(得分:1)
创建一个新类User(带有getter,setter)并尝试这个:
public User searchEmail(String email) {
SQLiteDatabase db = this.getWritableDatabase();
User u = null;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
u = new User();
u.setfName(c.getString(c.getColumnIndex("FIRSTNAME")));
u.setsName(c.getString(c.getColumnIndex("SURNAME")));
}
c.close();
return u;
}
在活动中显示结果:
public void searchFriend(View v) {
EditText emailSearch = (EditText) findViewById(R.id.emailSearch);
TextView firstName = (TextView)findViewById(R.id.firstname);
TextView surname = (TextView)findViewById(R.id.surname);
String emailSearchString = emailSearch.getText().toString();
User user = myDB.searchEmail(emailSearchString);
firstName.setText(user.getfName());
surname.setText(user.getsName());
}
答案 1 :(得分:0)
创建一个新类用户并尝试此操作:
public User searchEmail(String email)
{
SQLiteDatabase db = this.getWritableDatabase();
User u = null;
String selectQuery = "SELECT FIRSTNAME, SURNAME FROM friends_table WHERE EMAIL=? ";
Cursor c = db.rawQuery(selectQuery, new String[] { email });
if (c.moveToFirst()) {
u = new User();
u.setfName(c.getString(c.getColumnIndex("FIRSTNAME")));
u.setsName(c.getString(c.getColumnIndex("SURNAME")));
}
c.close();
return u;
}