Java 在Android中以特定格式从数据库检索数据
我试图检索存储在数据库中的数据 我想有它检索在arraylist格式,这是Java 在Android中以特定格式从数据库检索数据,java,android,database,Java,Android,Database,我试图检索存储在数据库中的数据 我想有它检索在arraylist格式,这是 ArrayList (ProfileID, Name) 我有一个数据库处理程序,我正在尝试以下操作: public ArrayList<String> getinviteDetails() { String selectQuery = "SELECT * FROM " + TABLE_DETAILS; SQLiteDatabase db = this.getWritableDataba
ArrayList (ProfileID, Name)
我有一个数据库处理程序,我正在尝试以下操作:
public ArrayList<String> getinviteDetails()
{
String selectQuery = "SELECT * FROM " + TABLE_DETAILS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<String> datalist = new ArrayList<String>();
if (cursor.moveToLast())
{
datalist.add(cursor.getString(0));
datalist.add(cursor.getString(1));
datalist.add(cursor.getString(2));
datalist.add(cursor.getString(3));
}
cursor.close();
db.close();
return datalist;
}
我不知道如何以ArrayList(ProfileID,Name)格式存储它?谁能帮我修一下这个零件吗
谢谢 在ArrayList中不能有键值对。使用HashMap代替它。 正确的方法如下所示
public HashMap<String, String> getinviteDetails() {
HashMap<String, String> datalist = new HashMap<String, String>();
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_DETAILS;
cursor = db.rawQuery(selectQuery, null);
if (cursor != null) {
// Loop all entries and fill datalist
while (cursor.moveToNext()) {
String profileID = cursor.getString(cursor
.getColumnIndex(PROFILE_COLUMN_NAME));
String name = cursor.getString(cursor
.getColumnIndex(NAME_COLUMN_NAME));
datalist.put(profileID, name);
}
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (cursor != null) {
cursor.close();
}
if (db != null) {
db.close();
}
}
return datalist;
}
public HashMap getinviteDetails(){
HashMap datalist=新的HashMap();
SQLiteDatabase db=null;
游标=空;
试一试{
db=this.getWritableDatabase();
String selectQuery=“SELECT*FROM”+表格\详细信息;
cursor=db.rawQuery(selectQuery,null);
如果(光标!=null){
//循环所有条目并填写数据列表
while(cursor.moveToNext()){
String profileID=cursor.getString(cursor
.getColumnIndex(PROFILE_COLUMN_NAME));
字符串名称=cursor.getString(cursor
.getColumnIndex(NAME_COLUMN_NAME));
datalist.put(profileID,name);
}
}
}捕获(例外e){
//TODO:处理异常
}最后{
如果(光标!=null){
cursor.close();
}
如果(db!=null){
db.close();
}
}
返回数据列表;
}
有一个模型类,如下所示:
class ModelProfileClass
{
// Constructor for ProfileID and Name
public ModelProfileClass (String PROFILE_ID, String UNAME)
{
this.profile_ID = PROFILE_ID;
this.uname = UNAME;
}
// getters and setters for ProfileID and Name
//get_PROFILEID(), get_UNAME() , set_PROFILEID(String profileID) , set_UNAME(String uname)
}
在你的主课上
// Create a list of objects of ModelProfileClass
List<ModelProfileClass> profileList= new ArrayList<ModelProfileClass>();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
// For each iteration, making the object null
ModelProfileClass profileObject= null;
do {
// Constructor is invoked in the model class and so, the object values are set.
profileObject= new ModelProfileClass(
cursor.getString(0) ,
cursor.getString(1)
);
// Adding current object to the object array list
profileList.add(profileObject);
} while (cursor.moveToNext()); // Do this till the last record.
cursor.close();
你们需要使用object arraylist你们读过关于OOP的书吗?我并没有键值对。它是Arraylist(用户的ProfileID,用户名)有一个数据结构(类有两个成员变量)并使用它的列表。谢谢。我在datamodel和databasehandler中做了一些更改。但是用你的逻辑。
// Create a list of objects of ModelProfileClass
List<ModelProfileClass> profileList= new ArrayList<ModelProfileClass>();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
// For each iteration, making the object null
ModelProfileClass profileObject= null;
do {
// Constructor is invoked in the model class and so, the object values are set.
profileObject= new ModelProfileClass(
cursor.getString(0) ,
cursor.getString(1)
);
// Adding current object to the object array list
profileList.add(profileObject);
} while (cursor.moveToNext()); // Do this till the last record.
cursor.close();
String profileID = profileList.get(position).get_PROFILEID();
String uName = profileList.get(position).get_UNAME();