public class MainObj {
private static final MainObj instance = new MainObj();
public static MainObj getInstance() {
return instance;
}
public static class User {
public int account_id;
public String user_id;
public String sip_id;
public String username;
public String fullname;
public boolean active;
public boolean status;
public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
Log.e("myphone", account_id+"");
this.account_id = account_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getSip_id() {
return sip_id;
}
public void setSip_id(String sip_id) {
this.sip_id = sip_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
}
我在Android上编写sip应用程序。我有一些关于用户登录的信息。我想做类似的事情。
User user = new User(); Log.i("TAG", user.sip_id);
或
Log.i("TAG", user.getSip_id());
// get user data from table
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM user";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
MainObj.User user = new MainObj.User();
user.setSip_id(cursor.getString(1));
user.setAccount_id(cursor.getString(2));
.... and so on
}
cursor.close();
db.close();
// return user
return user;
}
我不知道如何正确google,因为我已经搜索了大约3个小时。
答案 0 :(得分:2)
只需像这样更改代码,您就可以随处访问数据。
import android.util.Log;
public class User {
private static final User instance = new User();
public static User getInstance() {
return instance;
}
private User() {
super();
}
private int account_id;
private String user_id;
private String sip_id;
private String username;
private String fullname;
private boolean active;
private boolean status;
public int getAccount_id() {
return account_id;
}
public void setAccount_id(int account_id) {
Log.e("myphone", account_id + "");
this.account_id = account_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getSip_id() {
return sip_id;
}
public void setSip_id(String sip_id) {
this.sip_id = sip_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
}
您可以通过访问此类实例
将数据设置为用户obj//get user data from table
public void getUserDetails() {
String selectQuery = "SELECT * FROM user";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
//If you want you can hold the reference in a User obj = User.getInstance(); just to write less code
User.getInstance().setSip_id(cursor.getString(1));
User.getInstance().setAccount_id(cursor.getString(2));
.... and so on
}
cursor.close();
db.close();
}
如果您在获取数据后需要数据,请执行以下操作:
mEditText.setText(User.getInstance().getUsername());
答案 1 :(得分:0)
您的问题出在 getUserDetails 方法中。
首先,创建名为 user 的 HashMap 变量...并在 if 块中创建 MainObj .User 变量也称为用户。
那么......碰撞怎么样?在Java中,变量范围是块。因此,当if块完成时, if 块中创建的用户将被自动删除,返回值为空 HashMap 。
你可以改变这种方法:
// get user data from table
public MainObj.User getUserDetails() {
MainObj.User user = null;
String selectQuery = "SELECT * FROM user";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user = new MainObj.User();
user.setSip_id(cursor.getString(1));
user.setAccount_id(cursor.getString(2));
.... and so on
}
cursor.close();
db.close();
// return user or null if not data
return user;
}
答案 2 :(得分:0)
您可以返回用户详细信息列表。如果您需要从应用程序的任何位置访问它,请将此列表放在Application类中。它维护一个全局应用程序状态。您可以通过调用ChildApplication app =(ChildApplication)context.getApplicationContext();来扩展Application类并在应用程序的任何位置使用该类;
public List<MainObj.User> getUSerDetails(){
List<MainObj.User> userDetails = new ArrayList<MainObj.User>();
String selectQuery = "SELECT * FROM user";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
MainObj.User user = new MainObj.User();
user.setSip_id(cursor.getString(1));
user.setAccount_id(cursor.getString(2));
.... and so on
userDetails.add(user);
}
cursor.close();
db.close();
return userDetails;
}