我创建了我的数据库并将我的xml解析为arraylist以在listview中显示。我试图将该arraylist保存到数据库,但问题是我需要将数据库绑定到我的模型,现在它需要我将模型传递给我需要传递给它的ArrayList。我试图更改数据库中的addEmployee()方法以接受ArrayList,但后来我无法从模型类中获取getter来设置数据。
我已经尝试了几天,我只是完全坚持如何将我的解析数据保存到SQLite数据库中。
//Add new employee
public boolean addEmployee(Employee employee) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, employee.getEmployee_number());
values.put(KEY_FIRST_NAME, employee.getFirst_name());
values.put(KEY_LAST_NAME, employee.getLast_name());
values.put(KEY_PHONE_NUMBER_MOBILE, employee.getPhone_mobile());
values.put(KEY_PHONE_NUMBER_OFFICE, employee.getPhone_office());
values.put(KEY_HAS_DIRECT_REPORTS, employee.getHas_direct_reports());
values.put(KEY_EMAIL, employee.getEmail());
values.put(KEY_COST_CENTER, employee.getCost_center_id());
//Inserting Row
database.insert(TABLE_EMPLOYEE, null, values);
database.close();
return true;
}
public void getXMLData() {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder()
.url(getString(R.string.API_FULL_URL))
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String responseData = response.body().string();
final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
mEmployees.clear();
mEmployees.addAll(employees);
DatabaseHandler databasehandler = new DatabaseHandler(getApplicationContext());
db = databasehandler.getWritableDatabase();
databasehandler.onCreate(db);
databasehandler.addEmployee(employees);
ArrayList<Employee> mTopList = databasehandler.getAllEmployees(); //this is empty
//tell adapter on the UI thread its data changed
runOnUiThread(new Runnable() {
@Override
public void run() {
mTopListViewAdapter.notifyDataSetChanged();
mBottomListViewAdapter.notifyDataSetChanged();
mMangerList.setVisibility(View.VISIBLE);
directReportListView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
}
});
}
});
}
答案 0 :(得分:1)
我尝试更改数据库中的addEmployee()方法以接受ArrayList,但后来我无法从模型类中获取getter来设置数据。
你不需要一个循环吗?
final DatabaseHandler databasehandler = new DatabaseHandler(getApplicationContext());
final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);
for (Employee e : employees) {
databasehandler.addEmployee(e);
}
如果你真的想用列表制作方法,同样的想法......
public boolean addEmployees(List<Employee> employees) {
SQLiteDatabase database = this.getWritableDatabase();
for (Employee e : employees) {
ContentValues values = new ContentValues();
values.put(KEY_ID, e.getEmployee_number());
...
database.insert(TABLE_EMPLOYEE, null, values);
}
database.close();
return true;
}
提示如果要在ListView中显示SQLite数据库,请使用CursorAdapter,然后您不会在内存中混洗ArrayList。