简体   繁体   中英

Android Hashmap return value from other class

i will like to pass the value from DatabaseHandler.java lets say "name" to mainActivity and show it on 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);
}

I am having trouble writing correct code to make it works, i keep getting "Syntax error on token". Please help me out. Thanks.

this line cause problem

HashMap<string, string=""> details = getUserDetails();

replace it with

HashMap<string, string> details = getUserDetails();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM