gpt4 book ai didi

java - 如何将ArrayList传递到本地数据库

转载 作者:行者123 更新时间:2023-12-01 08:59:49 24 4
gpt4 key购买 nike

我创建了数据库并将 xml 解析为数组列表以在 ListView 中显示。我正在尝试将该数组列表保存到数据库中,但问题是我需要将数据库绑定(bind)到我的模型,现在当我需要将数组列表传递给它时,它希望我将模型传递给它。我尝试更改数据库中的 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);
}
});
}
});
}

最佳答案

I tried to change the addEmployee() method in the database to accept an ArrayList but then I am not able to get the getters from the model class to set the data.

你不需要一个循环吗?

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。

关于java - 如何将ArrayList传递到本地数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774056/

24 4 0