gpt4 book ai didi

java - Android - 如何将数据库中的所有值添加到arraylist

转载 作者:行者123 更新时间:2023-12-01 23:03:37 26 4
gpt4 key购买 nike

我有以下数据库类,我有一个简单的问题... getrecords() 方法仅返回数据库中的名字,即 10 ,我需要的只是将数据库中的所有名称存储到数组列表,即 10 和 100,我该怎么做?我是初学者,所以对这方面的知识不是很多。谢谢您的帮助!

public class Database {

public static final String KEY_ROWID = "ID";
public static final String KEY_NAME = "NAME";
public static final String KEY_HOTNESS = "PLACE";

private static final String DATABASE_NAME = "Database.db";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 6;

private static DbHelper ourHelper;
private final Context ourContext;
private static SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override //I create it here, right?
public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL UNIQUE, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);


db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('10',' Plantation')");
db.execSQL("INSERT INTO " + getDatabaseTable() + "(NAME, PLACE) VALUES('100','NR Plantation')");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + getDatabaseTable());
onCreate(db);
}

public void close(Database database) {
// TODO Auto-generated method stub

}
}



public Database(Context c){
ourContext = c;
}


public Database open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}

public void close() {

ourHelper.close();
}

public static String getDatabaseTable() {
return DATABASE_TABLE;
}

public List<String> getrecords() {
// TODO Auto-generated method stub
List<String> arraylist = new ArrayList<String>();
String[] columns = new String[]{ KEY_NAME };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";



int iName = c.getColumnIndex(KEY_NAME);


for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
arraylist.add(c.getString(iName));

return arraylist;

}

return arraylist;
}

}

最佳答案

Your method should look like this :

public ArrayList<String> getRecords(){
ArrayList<String> data=new ArrayList<String>();
Cursor cursor = db.query(DATABASE_TABLE, new String[]{"column names"},null, null, null, null, null);
String fieldToAdd=null;
while(cursor.moveToNext()){
fieldToAdd=cursor.getString(0);
data.add(fieldToAdd);
}
cursor.close(); // dont forget to close the cursor after operation done
return data;
}

this method will return all field values from database to arraylist

关于java - Android - 如何将数据库中的所有值添加到arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23153750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com