我在sqlite中有一个数据库我试图在textview或listiew中显示登录表的名称列。
在 databeaseHAndler 类中,我使用HashMap从表中选择所有用户,但我不知道如何在textview中显示该查询的结果(如果可以使用ashmap )或Listview。
理想情况下,它们应该只是表中的一条记录,这就是为什么我希望它显示在textview中,基本上显示登录到我的应用程序的用户。有谁知道怎么做?
我的 Databasehandler 类:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "u168512214_barzd";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String name, String email, String uid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}
/**
* Getting user data from database
* */
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;
}
/**
* Getting user login status
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
HomeFragment类我要在哪里显示名称:
public class HomeFragment extends ListFragment {
TextView username;
UserFunctions userFunctions;
Button btnViewBars;
Button btnNewBar;
private DatabaseHandler userDetails;
AlertDialogManager alert = new AlertDialogManager();
ArrayList<HashMap<String, String>> nameList;
//会话管理器类 // SessionManager会话;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
btnViewBars = (Button)rootView.findViewById(R.id.btnViewBars);
btnNewBar = (Button)rootView.findViewById(R.id.btnCreateBar);
nameList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> user = userDetails.getUserDetails();
编辑:
/**
* Getting user data from database
* */
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){
Log.i("key_ID","value is: " + cursor.getString(0));
Log.i("key_name","value is: " + cursor.getString(1));
Log.i("key_email","value is: " + cursor.getString(2));
Log.i("key_uid","value is: " + cursor.getString(3));
Log.i("key_created_at","value is: " + cursor.getString(4));
//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;
}
答案 0 :(得分:0)
创建databaseHandler类的对象
DatabaseHandler db = new DatabaseHandler(MainActivity.this);
现在,获取哈希映射中的用户列表
HashMap<String, String> user=db.getUserDetails();
noe遍历hashmap大小,即使用循环条件并从哈希映射中获取值并在文本视图中设置
user.get(i).get("name")