gpt4 book ai didi

java - 来自 SQLite 的数据不显示

转载 作者:行者123 更新时间:2023-11-30 01:49:58 24 4
gpt4 key购买 nike

我有我的数据库助手、模型和主 Activity ,但数据仍然没有显示。我不知道我的代码有什么问题或遗漏了什么。

这是我使用DB Browser for SQLite浏览的数据sqlite

enter image description here

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.example.jathniel.myapplication/databases/";
private static String DB_NAME = "mydoctorfinder_new_migrate.sqlite";
private SQLiteDatabase myDataBase;
private Context myContext = null;
public static String TAG = "tag";

private static final String TABLE_DOCTORS = "doctors";
private static final String KEY_ID = "id";
private static final String KEY_FULLNAME = "full_name";
private static final String KEY_GENDER = "gender";


public DatabaseHelper(Context context) {
super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1);
this.myContext = context;
}

public void createDataBase() throws IOException {
boolean dbExist = this.checkDataBase();
if(!dbExist) {
this.getReadableDatabase();

try {
this.copyDataBase();
} catch (IOException e) {
throw new Error("Error");
}
}
}

public void copyDataBase() throws IOException {
InputStream myInput = this.myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
FileOutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];

int length;
while((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();
}

public boolean checkDataBase() {
SQLiteDatabase checkDB = null;

try {
String e = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(e, (SQLiteDatabase.CursorFactory)null, 0);
} catch (SQLiteException e) {
;
}

if(checkDB != null) {
checkDB.close();
}

return checkDB != null;
}

public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
this.myDataBase = SQLiteDatabase.openDatabase(myPath, (SQLiteDatabase.CursorFactory)null, 0);
}

public synchronized void close() {
if(this.myDataBase != null) {
this.myDataBase.close();
}

super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public void createDatabase() {
}

public List<DoctorModel> getAllDoctorList() {
List<DoctorModel> doctorsArrayList = new ArrayList<DoctorModel>();

String selectQuery = "SELECT * FROM " + TABLE_DOCTORS;
Log.d(TAG, selectQuery);

SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (c.moveToFirst()) {
do {

DoctorModel doctor = new DoctorModel();
doctor.id = c.getInt(c.getColumnIndex(KEY_ID));
doctor.full_name = c.getString(c.getColumnIndex(KEY_FULLNAME));
doctor.gender = c.getString(c.getColumnIndex(KEY_GENDER));

doctorsArrayList.add(doctor);

} while (c.moveToNext());
}

return doctorsArrayList;
}
}

Model class

public class DoctorModel {

public int id;
public String full_name;
public String gender;

public DoctorModel(){

}

public DoctorModel(int id, String full_name, String gender) {
// TODO Auto-generated constructor stub
this.id = id;
this.full_name = full_name;
this.gender = gender;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFull_name() {
return full_name;
}

public void setFull_name(String full_name) {
this.full_name = full_name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}
}

MainActivity

public class MainActivity extends AppCompatActivity {


List<DoctorModel> list = new ArrayList<DoctorModel>();
DatabaseHelper db;
TextView tv;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

db = new DatabaseHelper(getApplicationContext());
tv = (TextView) findViewById(R.id.tv);

print(db.getAllDoctorList());

}

private void print(List<DoctorModel> list) {
// TODO Auto-generated method stub
String value = "";
for(DoctorModel dm : list){
value = value+"id: "+dm.id+", fullname: "+dm.full_name+" Gender: "+dm.gender+"\n";
}
tv.setText(value);
}
}

最佳答案

您忘记在 getAllDoctorList() 下的 doctorArrayList 中添加 DoctorModel 对象

   // looping through all rows and adding to list
if (c.moveToFirst()) {
do {

DoctorModel doctor = new DoctorModel();
doctor.id = c.getInt(c.getColumnIndex(KEY_ID));
doctor.full_name = c.getString(c.getColumnIndex(KEY_FULLNAME));
doctor.gender = c.getString(c.getColumnIndex(KEY_GENDER));

doctorsArrayList.add(doctor); // add Object in ArrayList

} while (c.moveToNext());
}

关于java - 来自 SQLite 的数据不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208111/

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