0
我開發一個Android應用程序,當我選擇一個片段,我把那些代碼「OnActivityCreated」,如:如何提高應用性能?
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnAdd = (ImageButton) getActivity().findViewById(R.id.imageButtonTime);
txtTime = (TextView) getActivity().findViewById(R.id.textViewTime);
txtTime.setText("");
listView = (ListView) getActivity().findViewById(R.id.listView);
time_control = new Time_Control(LoginActivity.db.getConnection());
accountID = LoginActivity.Account_Id;
calendar = Calendar.getInstance();
mYear = calendar.get(Calendar.YEAR); // current year
mMonth = calendar.get(Calendar.MONTH); // current month
mDay = calendar.get(Calendar.DAY_OF_MONTH); // current day
txtTime.setText(mDay + "/" + (mMonth + 1) + "/" + mYear);
sDate = mYear + "-" + (mMonth + 1) + "-" + mDay;
LoadList();
LoadList()用於從數據庫中獲取數據,並把它放在列表視圖。 當我選擇片段時,我的應用程序加載非常慢。無論如何,我可以做的是提高性能,比如將上面的代碼放到其他位置。
感謝您的幫助!
編輯:LoadList()代碼:
public ArrayList<Time> LoadTime(int accId, String date) {
ArrayList<Time> arrayList = new ArrayList<Time>();
try {
PreparedStatement query = connection.prepareStatement("exec sp_excutedtask_load ?,?");
query.setInt(1, accId);
query.setString(2, date);
ResultSet resultSet = query.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("ID");
String tagName = resultSet.getString("TagName");
String taskName = resultSet.getString("TaskName");
int esHrs = resultSet.getInt("EsHour");
int esMin = resultSet.getInt("EsMinute");
int acHrs = resultSet.getInt("AcHour");
int acMin = resultSet.getInt("AcMinute");
String start, end;
if (resultSet.getTime("Start") == null) {
start = "";
} else {
start = resultSet.getTime("Start").toString();
}
if (resultSet.getTime("End") == null) {
end = "";
} else {
end = resultSet.getTime("End").toString();
}
int run = resultSet.getInt("Running");
int done = resultSet.getInt("Done");
Time time = new Time(id, tagName, taskName, esHrs, esMin, acHrs, acMin, start, end, run, done);
arrayList.add(time);
}
} catch (SQLException e) {
e.printStackTrace();
}
return arrayList;
}
...我們需要知道LoadList()的代碼來回答這個問題 – EpicPandaForce
加載時間取決於您在LoadList()中使用的邏輯。既然你從數據庫中獲得它,優化你用來獲取數據的查詢,並保持你的SQLiteOpenHelper類爲單身人士 – LvN
編輯我的文章。 – Huy