我想传递来自DatabaseHandler.java的值,让我们说" name"到mainActivity并在textview上显示它。
DatabaseHandler.java
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
MainActivity.java
TextView lblid;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lblid = (TextView) findViewById(R.id.showid);
HashMap<string, string=""> details = getUserDetails();
String nametext = details.getString("name");
lblid.setText(nametext);
}
我在编写正确的代码以使其正常工作时遇到问题,我不断收到#34;令牌上的语法错误&#34;。请帮帮我。 感谢。
答案 0 :(得分:3)
此行导致问题
HashMap<string, string=""> details = getUserDetails();
替换为
HashMap<string, string> details = getUserDetails();
用法exaple
Main.java
public class MainActivity extends Activity {
...
private DatabaseHandler dbh;
...
public void onCreate(Bundle) {
dbh = new DatabaseHandler(...);
...
}
...
HashMap<String, String> details = dbh.getUserDetails();
...
String nametext = details.get("name");