2014-02-08 63 views
0

我有一個hashmap方法是;調用hashmap地圖

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

我有一個字符串電子郵件,我想要獲得id = 10的方法的電子郵件。 如何設置電子郵件基本等於該特定字符串; String email = getUserDetails(email where ID = 10)我知道我走了,但你明白了。

+2

你怎麼認爲額外的標籤和一個有些模糊的問題會有幫助嗎? – Mark

+0

'SELECT name,email,uid,created_at FROM my_table WHERE id = 10'? –

+0

http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html – The111

回答

2
public HashMap<String, String> getUserDetails(int id){ 
    HashMap<String,String> user = new HashMap<String,String>(); 
    String selectQuery = "SELECT * FROM " + TABLE_LOGIN + " WHERE id=" + id + ";"; 
    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; 
} 

使用它:

String email = getUserDetails(10).get("email"); 
+0

第一個參數是int,getUserDetails()。get(「email」);作品,但我得到編譯錯誤。 – raklar